summaryrefslogtreecommitdiff
path: root/misc/svgmap/paths-to-edges.c
blob: 7e485b1309dab7a509c39b161ea07967695889c3 (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct link {
 char *a;
 char *b;
 int count;
};

struct link **get_links(char ***data) {
 int i,j,k=0;
 for(i=0;data[i];i++) {
  for(j=0;data[i][j];j++) {
   k++;//this is overkill because I can't remove dedups that won't need 
       //new space allocated.
  }
 }
 struct link **link=calloc(k+1,sizeof(struct link *));
 for(i=0;data[i];i++) {
  for(j=0;data[i][j+1];j++) {
   for(k=0;link[k];k++) {
    if(!strcmp(link[k]->a,data[i][j]) && !strcmp(link[k]->b,data[i][j+1]) ||
       !strcmp(link[k]->b,data[i][j]) && !strcmp(link[k]->a,data[i][j+1])) {
     break;
    }
   }
   if(!link[k]) {
    link[k]=malloc(sizeof(struct link));
    link[k]->a=strdup(data[i][j]);
    link[k]->b=strdup(data[i][j+1]);
    link[k]->count=1;
   } else {
    link[k]->count++;
   }
  }
 }
 return link;
}

char *recurse_readline(int chars);

char **recurse_readfile(int lines) {
 char **malloced;
 char *line=recurse_readline(0);
 if(feof(stdin)) {
  malloced=malloc(sizeof(char *) * (lines+1));
  malloced[lines]=0;
  return malloced;
 }
 malloced=recurse_readfile(lines+1);
 malloced[lines]=line;
 return malloced;
}

char *recurse_readline(int chars) {
 char *malloced;
 short in=fgetc(stdin);
 if(in == '\n' || in == -1) {
  malloced=malloc(chars+2);
  malloced[chars]='\n';
  malloced[chars+1]=0;
  return malloced;
 }
 malloced=recurse_readline(chars+1);
 malloced[chars]=in;
 return malloced;
}

char ***lines_to_data(char **lines) {
 char ***data;
 int i,j,k,n,m;
 for(i=0;lines[i];i++);
 data=malloc(sizeof(char **) * i+1);
 data[i]=0;

 for(i=0;lines[i];i++) {
  k=0;
  for(j=0;lines[i][j];j++) {
   if(lines[i][j]==' ' || lines[i][j] == '\n' || lines[i][j] == '\0') k++;
  }
  data[i]=malloc(sizeof(char *) * k+1);
  data[i+1]=0;
  data[i][k]=0;
 }
 for(i=0;lines[i];i++) {
  k=0;
  n=0;
  for(j=0;lines[i][j];j++) {
   if(lines[i][j]==' ' || lines[i][j]=='\n' || lines[i][j]=='\0') {
    data[i][n]=strndup(lines[i]+k,j-k);
    data[i][n][j-k]=0;
    n++;
    k=j+1;
   }
  }
 }
 return data;
}

int main() {
 int i,j;
 char **lines;
 char ***data;
 struct link **link;
 lines=recurse_readfile(0);
/* for(i=0;lines[i];i++) {
  printf("lines[%d]=\"%s\"\n",i,lines[i]);
 }*/
 data=lines_to_data(lines);
/* for(i=0;data[i];i++) {
  for(j=0;data[i][j];j++) {
   printf("data[%d][%d]='%s'\n",i,j,data[i][j]);
  }
  printf("\n");
 }
*/
 link=get_links(data);
 //printf("//graph generated by paths-to-edges to DOT format\n");
 //printf("graph anonet {\n");
 for(i=0;link[i];i++) {
  //printf("%s -> %s (%d)\n",link[i]->a,link[i]->b,link[i]->count);
  //printf("%s -- %s;\n",link[i]->a,link[i]->b,link[i]->count);
  printf("%s %s\n",link[i]->a,link[i]->b);
 }
 //printf("}\n");

 return 0;
}