]> git.ozlabs.org Git - ccan/blob - tools/_infotojson/infotojson.c
dd90dc2afaec3510c6ee1cfcd7fd3037b3f7b57c
[ccan] / tools / _infotojson / infotojson.c
1 /* This extract info from _info.c and create json file and also optionally store to db */
2 #include "infotojson.h"
3
4 /* Is A == B ? */
5 #define streq(a,b) (strcmp((a),(b)) == 0)
6
7 /* Does A start with B ? */
8 #define strstarts(a,b) (strncmp((a),(b),strlen(b)) == 0)
9
10 /* This version adds one byte (for nul term) */
11 static void *grab_file(void *ctx, const char *filename)
12 {
13         unsigned int max = 16384, size = 0;
14         int ret, fd;
15         char *buffer;
16
17         if (streq(filename, "-"))
18                 fd = dup(STDIN_FILENO);
19         else
20                 fd = open(filename, O_RDONLY, 0);
21
22         if (fd < 0)
23                 return NULL;
24
25         buffer = talloc_array(ctx, char, max+1);
26         while ((ret = read(fd, buffer + size, max - size)) > 0) {
27                 size += ret;
28                 if (size == max)
29                         buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
30         }
31         if (ret < 0) {
32                 talloc_free(buffer);
33                 buffer = NULL;
34         } else
35                 buffer[size] = '\0';
36         close(fd);
37         return buffer;
38 }
39
40 /* This is a dumb one which copies.  We could mangle instead. */
41 static char **split(const char *text)
42 {
43         char **lines = NULL;
44         unsigned int max = 64, num = 0;
45
46         lines = talloc_array(text, char *, max+1);
47
48         while (*text != '\0') {
49                 unsigned int len = strcspn(text, "\n");
50                 lines[num] = talloc_array(lines, char, len + 1);
51                 memcpy(lines[num], text, len);
52                 lines[num][len] = '\0';
53                 text += len + 1;
54                 if (++num == max)
55                         lines = talloc_realloc(text, lines, char *, max*=2 + 1);
56         }
57         lines[num] = NULL;
58         return lines;
59 }
60
61 /*combin desc into an array to write to db*/
62 static char *combinedesc(char **desc)
63 {
64         unsigned int i = 0, size = 0;;
65         char *combine;
66         
67         for(i = 0; desc[i]; i++)
68                 size += strlen(desc[i]);
69
70         combine = (char *)palloc((size + i)* sizeof(char));
71         strcpy(combine, desc[0]);
72
73         for(i = 1; desc[i]; i++) {
74                 strcat(combine, "\n");
75                 strcat(combine, desc[i]);
76         }
77         strreplace(combine,'\'',' ');
78         return combine;
79 }
80
81 /*creating json structure for storing to file/db*/
82 struct json * createjson(char **infofile, char *author)
83 {
84         struct json *jsonobj;
85         unsigned int modulename;
86
87         if(infofile == NULL || author == NULL) {
88                 printf("Error Author or Info file is NULL\n");
89                 exit(1);
90         }
91
92         jsonobj = (struct json *)palloc(sizeof(struct json));
93
94         jsonobj->author = author;
95
96         modulename =  strchr(infofile[0], '-') - infofile[0];
97         jsonobj->module = (char *)palloc(sizeof(char) * (modulename - 1));
98         strncpy(jsonobj->module, infofile[0], modulename - 1);
99         jsonobj->module[modulename - 1] = '\0';
100
101         jsonobj->title = infofile[0];
102         jsonobj->desc = &infofile[1];
103         
104         return jsonobj;
105 }
106
107 /*extracting title and description from _info.c files*/
108 char **extractinfo(char **file)
109 {
110         char **infofile = NULL;
111         unsigned int count = 0, j = 0, size = 0;
112         bool printing = false;
113         
114         while(file[size++]);
115         infofile = (char **) palloc(size * sizeof(char *));
116         
117         for (j = 0; j < size - 1; j++) {
118                 if (streq(file[j], "/**")) {
119                         printing = true;
120                 } 
121                 else if (streq(file[j], " */"))
122                         printing = false;
123                 else if (printing) {
124                         if (strstarts(file[j], " * "))
125                                 infofile[count++] = file[j] + 3;
126                         else if (strstarts(file[j], " *"))
127                                 infofile[count++] = file[j] + 2;
128                         else {
129                                 printf("Error in comments structure\n%d",j);
130                                 exit(1);
131                         }
132                 }
133         }
134         infofile[count] = NULL;
135         return infofile;        
136 }
137
138 /*storing json structure to json file*/
139 int storejsontofile(struct json *jsonobj, char *file)
140 {
141         FILE *fp;
142         unsigned int j = 0;
143         fp = fopen(file, "wt");
144         
145         fprintf(fp,"\"Module\":\"%s\",\n",jsonobj->module);
146         fprintf(fp,"\"Title\":\"%s\",\n",jsonobj->title);
147         fprintf(fp,"\"Author\":\"%s\",\n",jsonobj->author);
148         fprintf(fp,"\"Description\":[\n");      
149         while(jsonobj->desc[j++])
150                 fprintf(fp,"{\n\"str\":\"%s\"\n},\n",jsonobj->desc[j - 1]);
151         fprintf(fp,"]\n");
152         fclose(fp);
153         return 1;
154         
155 }
156
157 /*storing json structure to db*/
158 int storejsontodb(struct json *jsonobj, char *db)
159 {
160         char *cmd, *query;
161         sqlite3 *handle;
162         char *errstr;
163         struct db_query *q;
164         
165         handle = db_open(db);
166         
167         query = aprintf("SELECT module from search where module=\"%s\";", jsonobj->module);
168         q = db_query(handle, query);
169         if (!q->num_rows)
170                 cmd = aprintf("INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\",'%s\');",
171                         jsonobj->module, jsonobj->author, jsonobj->title, combinedesc(jsonobj->desc));
172         else
173                 cmd = aprintf("UPDATE search set author=\"%s\", title=\"%s\", desc='%s\' where module=\"%s\";",
174                         jsonobj->author, jsonobj->title, combinedesc(jsonobj->desc), jsonobj->module);
175         
176         db_command(handle, cmd);        
177         db_close(handle);
178         return 1;
179 }
180
181 int main(int argc, char *argv[])
182 {
183         char *file;
184         char **lines;
185         char **infofile;
186         
187         struct json *jsonobj = NULL;
188         
189         if(argc < 4) {
190                 printf("usage: infotojson infofile jsonfile author sqlitedb\n");
191                 return 1;
192         }
193                 
194         file = grab_file(NULL, argv[1]);
195         if (!file)
196                 err(1, "Reading file %s", argv[1]);
197
198         lines = split(file);            
199         
200         //extract info from lines
201         infofile = extractinfo(lines);
202         
203         //create json obj
204         jsonobj = createjson(infofile, argv[3]);
205         
206         //store to file
207         storejsontofile(jsonobj, argv[2]);
208         
209         if(argv[4] != NULL)
210                 storejsontodb(jsonobj, argv[4]);
211                 
212         talloc_free(file);
213         return 0;
214 }