1 /* This extract info from _info.c and create json file and also optionally store to db */
2 #include "infotojson.h"
4 /*creating json structure for storing to file/db*/
5 static struct json *createjson(char **infofile, char *author)
8 unsigned int modulename;
10 if (infofile == NULL || author == NULL) {
11 printf("Error Author or Info file is NULL\n");
15 //jsonobj = (struct json *)palloc(sizeof(struct json));
16 jsonobj = talloc(NULL, struct json);
18 errx(1, "talloc error");
20 jsonobj->author = author;
22 /* First line should be module name and short description */
23 modulename = strchr(infofile[0], '-') - infofile[0];
25 jsonobj->module = talloc_strndup(jsonobj, infofile[0], modulename - 1);
27 errx(1, "talloc error");
29 jsonobj->title = infofile[0];
30 jsonobj->desc = &infofile[1];
35 /*extracting title and description from _info.c files*/
36 static char **extractinfo(char **file)
39 unsigned int count = 0, j = 0, num_lines = 0;
40 bool printing = false;
42 while (file[num_lines++]);
43 infofile = talloc_array(NULL, char *, num_lines);
44 //(char **) palloc(size * sizeof(char *));
46 for (j = 0; j < num_lines - 1; j++) {
47 if (streq(file[j], "/**")) {
50 else if (streq(file[j], " */"))
53 if (strstarts(file[j], " * "))
54 infofile[count++] = file[j] + 3;
55 else if (strstarts(file[j], " *"))
56 infofile[count++] = file[j] + 2;
58 err(1,"Error in comments structure\n%d",j);
63 infofile[count] = NULL;
67 /*storing json structure to json file*/
68 static int storejsontofile(const struct json *jsonobj, const char *file)
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]);
84 /*storing json structure to db*/
85 static int storejsontodb(const struct json *jsonobj, const char *db)
87 char *cmd, *query, *desc;
92 query = talloc_asprintf(NULL, "SELECT module from search where module=\"%s\";", jsonobj->module);
93 q = db_query(handle, query);
95 desc = strjoin(NULL,jsonobj->desc,"\n");
96 strreplace(desc, '\'', ' ');
98 cmd = talloc_asprintf(NULL, "INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\",'%s\');",
99 jsonobj->module, jsonobj->author, jsonobj->title, desc);
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);
104 db_command(handle, cmd);
112 int main(int argc, char *argv[])
117 struct json *jsonobj;
119 talloc_enable_leak_report();
121 errx(1, "usage: infotojson infofile jsonfile author [sqlitedb]\n");
125 file = grab_file(NULL, argv[1]);
127 err(1, "Reading file %s", argv[1]);
129 lines = strsplit(NULL, file, "\n", NULL);
131 //extract info from lines
132 infofile = extractinfo(lines);
135 jsonobj = createjson(infofile, argv[3]);
138 storejsontofile(jsonobj, argv[2]);
141 storejsontodb(jsonobj, argv[4]);
144 talloc_free(jsonobj);
146 talloc_free(infofile);