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