]> git.ozlabs.org Git - ccan/blob - tools/_infotojson/infotojson.c
Revised version of infotojson after comments
[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 static 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         jsonobj = talloc(NULL, struct json);
47         if (!jsonobj)
48                 errx(1, "talloc error");
49                 
50         jsonobj->author = author;
51
52         /* First line should be module name and short description */
53         modulename =  strchr(infofile[0], '-') - infofile[0];
54         
55         jsonobj->module = talloc_strndup(jsonobj, infofile[0], modulename - 1);
56          if (!jsonobj->module)
57                 errx(1, "talloc error");
58                 
59         //jsonobj->module = (char *)palloc(sizeof(char) * (modulename - 1));
60         //strncpy(jsonobj->module, infofile[0], modulename - 1);
61         //jsonobj->module[modulename - 1] = '\0';
62
63         jsonobj->title = infofile[0];
64         jsonobj->desc = &infofile[1];
65         
66         return jsonobj;
67 }
68
69 /*extracting title and description from _info.c files*/
70 static char **extractinfo(char **file)
71 {
72         char **infofile;
73         unsigned int count = 0, j = 0, num_lines = 0;
74         bool printing = false;
75         
76         while (file[num_lines++]);
77         infofile = talloc_array(NULL, char *, num_lines);
78         //(char **) palloc(size * sizeof(char *));
79         
80         for (j = 0; j < num_lines - 1; j++) {
81                 if (streq(file[j], "/**")) {
82                         printing = true;
83                 } 
84                 else if (streq(file[j], " */"))
85                         printing = false;
86                 else if (printing) {
87                         if (strstarts(file[j], " * "))
88                                 infofile[count++] = file[j] + 3;
89                         else if (strstarts(file[j], " *"))
90                                 infofile[count++] = file[j] + 2;
91                         else {
92                                 err(1,"Error in comments structure\n%d",j);
93                                 exit(1);
94                         }
95                 }
96         }
97         infofile[count] = NULL;
98         return infofile;        
99 }
100
101 /*storing json structure to json file*/
102 static int storejsontofile(const struct json *jsonobj, const char *file)
103 {
104         FILE *fp;
105         unsigned int j = 0;
106         fp = fopen(file, "wt");
107         fprintf(fp,"\"Module\":\"%s\",\n",jsonobj->module);
108         fprintf(fp,"\"Title\":\"%s\",\n",jsonobj->title);
109         fprintf(fp,"\"Author\":\"%s\",\n",jsonobj->author);
110         fprintf(fp,"\"Description\":[\n");      
111         for (j = 0; jsonobj->desc[j]; j++)
112                 fprintf(fp,"{\n\"str\":\"%s\"\n},\n",jsonobj->desc[j]);
113         fprintf(fp,"]\n");
114         fclose(fp);
115         return 1;
116 }
117
118 /*storing json structure to db*/
119 static int storejsontodb(const struct json *jsonobj, const char *db)
120 {
121         char *cmd, *query, *desc;
122         sqlite3 *handle;
123         struct db_query *q;
124         
125         handle = db_open(db);
126         query = talloc_asprintf(NULL, "SELECT module from search where module=\"%s\";", jsonobj->module);
127         q = db_query(handle, query);
128         
129         desc = strjoin(NULL,jsonobj->desc,"\n");
130         strreplace(desc, '\'', ' ');
131         if (!q->num_rows)
132                 cmd = talloc_asprintf(NULL, "INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\",'%s\');",
133                         jsonobj->module, jsonobj->author, jsonobj->title, desc);
134         else
135                 cmd = talloc_asprintf(NULL, "UPDATE search set author=\"%s\", title=\"%s\", desc='%s\' where module=\"%s\";",
136                         jsonobj->author, jsonobj->title, desc, jsonobj->module);
137
138         db_command(handle, cmd);        
139         db_close(handle);
140         talloc_free(query);
141         talloc_free(desc);
142         talloc_free(cmd);
143         return 1;
144 }
145
146 int main(int argc, char *argv[])
147 {
148         char *file;
149         char **lines;
150         char **infofile;
151         struct json *jsonobj;
152         
153         talloc_enable_leak_report();
154         if (argc < 4) {
155                 errx(1, "usage: infotojson infofile jsonfile author [sqlitedb]\n");
156                 return 1;
157         }
158                 
159         file = grab_file(NULL, argv[1]);
160         if (!file)
161                 err(1, "Reading file %s", argv[1]);
162
163         lines = strsplit(NULL, file, "\n", NULL);               
164         
165         //extract info from lines
166         infofile = extractinfo(lines);
167         
168         //create json obj
169         jsonobj = createjson(infofile, argv[3]);
170         
171         //store to file
172         storejsontofile(jsonobj, argv[2]);
173         
174         if (argv[4] != NULL)
175                 storejsontodb(jsonobj, argv[4]);
176                 
177         talloc_free(file);
178         talloc_free(jsonobj);
179         talloc_free(lines);
180         talloc_free(infofile);  
181         return 0;
182 }