about summary refs log tree commit diff
path: root/sourcecodes/k-best/src/arcs2dot.c
blob: 12f4815886d19a751a4405a926ccb18ee6a80f7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "cfg.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define BUFSIZE (1024)

void arcs2dot(char* vdfile, char* arcfile, char* dotfile)
{
  char buffer[BUFSIZE];
  FILE* dotf = strcmp(dotfile,"-") ? fopen(dotfile, "w") : stdout;

  fprintf(dotf, "digraph Bene {\n");
  fprintf(dotf, "  /*@@@BEFORE_ALL@@@*/\n"); 
  
  {/* read vdfile to get names of variables */

    FILE* vdf  = fopen(vdfile, "r");
    int v;
    fprintf(dotf, "  /*@@@BEFORE_NODES@@@*/\n"); 
    for(v=0; NULL != fgets(buffer, BUFSIZE, vdf); ++v){
      char* token = strtok(buffer, "\t\r\n");
      fprintf(dotf, "  V%d [label=\"%s\" /*@@@NODE_ATTS@@@*/];\n", v, token);
    }
    fprintf(dotf, "  /*@@@AFTER_NODES@@@*/\n"); 
    fclose(vdf);
  }

  {

    int from, to;
    FILE* arcf = strcmp(arcfile,"-") ? fopen(arcfile, "r") : stdin;

    fprintf(dotf, "  /*@@@BEFORE_EDGES@@@*/\n"); 
    while(2 == fscanf(arcf, "%d %d", &from, &to)){
      fprintf(dotf, "  V%d -> V%d /*@@@EDGE_ATTS@@@*/;\n", from, to);
    }
    fprintf(dotf, "  /*@@@AFTER_EDGES@@@*/\n"); 

    fclose(arcf);
  }
  
  fprintf(dotf, "  /*@@@AFTER_ALL@@@*/\n"); 
  fprintf(dotf, "}\n"); 
  fclose(dotf);
}

int main(int argc, char* argv[])
{

  if (argc!=4) {
    fprintf(stderr, "Usage: arcs2dot vdfile arcfile dotfile\n");
    return 1;
  }

  arcs2dot(argv[1], argv[2], argv[3]);

  return 0;
}