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