]> git.ozlabs.org Git - ccan/blob - tools/_infotojson/infotojson.c
611e0b392acef41b683f75d829a23ccd1b79a374
[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 /* This version adds one byte (for nul term) */
5 static void *grab_file(void *ctx, const char *filename)
6 {
7         unsigned int max = 16384, size = 0;
8         int ret, fd;
9         char *buffer;
10
11         if (streq(filename, "-"))
12                 fd = dup(STDIN_FILENO);
13         else
14                 fd = open(filename, O_RDONLY, 0);
15
16         if (fd < 0)
17                 return NULL;
18
19         buffer = talloc_array(ctx, char, max+1);
20         while ((ret = read(fd, buffer + size, max - size)) > 0) {
21                 size += ret;
22                 if (size == max)
23                         buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
24         }
25         if (ret < 0) {
26                 talloc_free(buffer);
27                 buffer = NULL;
28         } else
29                 buffer[size] = '\0';
30         close(fd);
31         return buffer;
32 }
33
34 /*creating json structure for storing to file/db*/
35 struct json * createjson(char **infofile, char *author)
36 {
37         struct json *jsonobj;
38         unsigned int modulename;
39
40         if(infofile == NULL || author == NULL) {
41                 printf("Error Author or Info file is NULL\n");
42                 exit(1);
43         }
44
45         jsonobj = (struct json *)palloc(sizeof(struct json));
46
47         jsonobj->author = author;
48
49         modulename =  strchr(infofile[0], '-') - infofile[0];
50         jsonobj->module = (char *)palloc(sizeof(char) * (modulename - 1));
51         strncpy(jsonobj->module, infofile[0], modulename - 1);
52         jsonobj->module[modulename - 1] = '\0';
53
54         jsonobj->title = infofile[0];
55         jsonobj->desc = &infofile[1];
56         
57         return jsonobj;
58 }
59
60 /*extracting title and description from _info.c files*/
61 char **extractinfo(char **file)
62 {
63         char **infofile = NULL;
64         unsigned int count = 0, j = 0, size = 0;
65         bool printing = false;
66         
67         while(file[size++]);
68         infofile = (char **) palloc(size * sizeof(char *));
69         
70         for (j = 0; j < size - 1; j++) {
71                 if (streq(file[j], "/**")) {
72                         printing = true;
73                 } 
74                 else if (streq(file[j], " */"))
75                         printing = false;
76                 else if (printing) {
77                         if (strstarts(file[j], " * "))
78                                 infofile[count++] = file[j] + 3;
79                         else if (strstarts(file[j], " *"))
80                                 infofile[count++] = file[j] + 2;
81                         else {
82                                 printf("Error in comments structure\n%d",j);
83                                 exit(1);
84                         }
85                 }
86         }
87         infofile[count] = NULL;
88         return infofile;        
89 }
90
91 /*storing json structure to json file*/
92 int storejsontofile(struct json *jsonobj, char *file)
93 {
94         FILE *fp;
95         unsigned int j = 0;
96         fp = fopen(file, "wt");
97         
98         fprintf(fp,"\"Module\":\"%s\",\n",jsonobj->module);
99         fprintf(fp,"\"Title\":\"%s\",\n",jsonobj->title);
100         fprintf(fp,"\"Author\":\"%s\",\n",jsonobj->author);
101         fprintf(fp,"\"Description\":[\n");      
102         while(jsonobj->desc[j++])
103                 fprintf(fp,"{\n\"str\":\"%s\"\n},\n",jsonobj->desc[j - 1]);
104         fprintf(fp,"]\n");
105         fclose(fp);
106         return 1;
107         
108 }
109
110 /*storing json structure to db*/
111 int storejsontodb(struct json *jsonobj, char *db)
112 {
113         char *cmd, *query;
114         sqlite3 *handle;
115         char *errstr;
116         struct db_query *q;
117         
118         handle = db_open(db);
119         
120         query = aprintf("SELECT module from search where module=\"%s\";", jsonobj->module);
121         q = db_query(handle, query);
122         if (!q->num_rows)
123                 cmd = aprintf("INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\",'%s\');",
124                         jsonobj->module, jsonobj->author, jsonobj->title, strjoin(NULL,jsonobj->desc,"\n"));
125         else
126                 cmd = aprintf("UPDATE search set author=\"%s\", title=\"%s\", desc='%s\' where module=\"%s\";",
127                         jsonobj->author, jsonobj->title, strjoin(NULL,jsonobj->desc,"\n"), jsonobj->module);
128         
129         db_command(handle, cmd);        
130         db_close(handle);
131         return 1;
132 }
133
134 int main(int argc, char *argv[])
135 {
136         char *file;
137         char **lines;
138         char **infofile;
139         
140         struct json *jsonobj = NULL;
141         
142         if(argc < 4) {
143                 printf("usage: infotojson infofile jsonfile author sqlitedb\n");
144                 return 1;
145         }
146                 
147         file = grab_file(NULL, argv[1]);
148         if (!file)
149                 err(1, "Reading file %s", argv[1]);
150
151         lines = strsplit(NULL, file, "\n", NULL);               
152         
153         //extract info from lines
154         infofile = extractinfo(lines);
155         
156         //create json obj
157         jsonobj = createjson(infofile, argv[3]);
158         
159         //store to file
160         storejsontofile(jsonobj, argv[2]);
161         
162         if(argv[4] != NULL)
163                 storejsontodb(jsonobj, argv[4]);
164                 
165         talloc_free(file);
166         return 0;
167 }