]> git.ozlabs.org Git - ccan/blob - tools/_infotojson/infotojson.c
e169fe17c953051c863650310d6c657067a0dfc2
[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         struct db_query *q;
116         
117         handle = db_open(db);
118         
119         query = aprintf("SELECT module from search where module=\"%s\";", jsonobj->module);
120         q = db_query(handle, query);
121         if (!q->num_rows)
122                 cmd = aprintf("INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\",'%s\');",
123                         jsonobj->module, jsonobj->author, jsonobj->title, strjoin(NULL,jsonobj->desc,"\n"));
124         else
125                 cmd = aprintf("UPDATE search set author=\"%s\", title=\"%s\", desc='%s\' where module=\"%s\";",
126                         jsonobj->author, jsonobj->title, strjoin(NULL,jsonobj->desc,"\n"), jsonobj->module);
127         
128         db_command(handle, cmd);        
129         db_close(handle);
130         return 1;
131 }
132
133 int main(int argc, char *argv[])
134 {
135         char *file;
136         char **lines;
137         char **infofile;
138         
139         struct json *jsonobj = NULL;
140         
141         if(argc < 4) {
142                 printf("usage: infotojson infofile jsonfile author sqlitedb\n");
143                 return 1;
144         }
145                 
146         file = grab_file(NULL, argv[1]);
147         if (!file)
148                 err(1, "Reading file %s", argv[1]);
149
150         lines = strsplit(NULL, file, "\n", NULL);               
151         
152         //extract info from lines
153         infofile = extractinfo(lines);
154         
155         //create json obj
156         jsonobj = createjson(infofile, argv[3]);
157         
158         //store to file
159         storejsontofile(jsonobj, argv[2]);
160         
161         if(argv[4] != NULL)
162                 storejsontodb(jsonobj, argv[4]);
163                 
164         talloc_free(file);
165         return 0;
166 }