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, const char *author, const char *directory)
8 unsigned int modulename;
10 if (infofile == NULL || author == NULL) {
11 printf("Error Author or Info file is NULL\n");
15 jsonobj = talloc(NULL, struct json);
17 errx(1, "talloc error");
19 jsonobj->author = talloc_strdup(jsonobj, author);
21 /* First line should be module name and short description */
22 modulename = strchr(infofile[0], '-') - infofile[0];
24 jsonobj->module = talloc_strndup(jsonobj, infofile[0], modulename - 1);
26 errx(1, "talloc error");
28 jsonobj->title = infofile[0];
29 jsonobj->desc = &infofile[1];
30 jsonobj->depends = get_deps(jsonobj, directory);
34 /*extracting title and description from _info.c files*/
35 static char **extractinfo(char **file)
38 unsigned int count = 0, j = 0, num_lines = 0;
39 bool printing = false;
41 while (file[num_lines++]);
42 infofile = talloc_array(NULL, char *, num_lines);
44 for (j = 0; j < num_lines - 1; j++) {
45 if (streq(file[j], "/**")) {
48 else if (streq(file[j], " */"))
51 if (strstarts(file[j], " * "))
52 infofile[count++] = file[j] + 3;
53 else if (strstarts(file[j], " *"))
54 infofile[count++] = file[j] + 2;
56 err(1,"Error in comments structure\n%d",j);
61 infofile[count] = NULL;
65 /*storing json structure to json file*/
66 static int storejsontofile(const struct json *jsonobj, const char *file)
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);
75 fprintf(fp,"\"Dependencies\":[\n");
76 for (j = 0; jsonobj->depends[j]; j++)
77 fprintf(fp,"{\n\"depends\":\"%s\"\n},\n",jsonobj->depends[j]);
81 fprintf(fp,"\"Description\":[\n");
82 for (j = 0; jsonobj->desc[j]; j++)
83 fprintf(fp,"{\n\"str\":\"%s\"\n},\n",jsonobj->desc[j]);
89 /*storing json structure to db*/
90 static int storejsontodb(const struct json *jsonobj, const char *db)
92 char *cmd, *query, *desc, *depends;
97 query = talloc_asprintf(NULL, "SELECT module from search where module=\"%s\";", jsonobj->module);
98 q = db_query(handle, query);
100 desc = strjoin(NULL, jsonobj->desc,"\n");
101 strreplace(desc, '\'', ' ');
103 depends = strjoin(NULL, jsonobj->depends,"\n");
105 cmd = talloc_asprintf(NULL, "INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\", \'%s\', \'%s\', 0);",
106 jsonobj->module, jsonobj->author, jsonobj->title, depends, desc);
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);
111 db_command(handle, cmd);
113 talloc_free(depends);
120 int main(int argc, char *argv[])
125 struct json *jsonobj;
127 talloc_enable_leak_report();
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");
132 file = grab_file(NULL, argv[2]);
134 err(1, "Reading file %s", argv[2]);
136 lines = strsplit(NULL, file, "\n", NULL);
138 //extract info from lines
139 infofile = extractinfo(lines);
142 jsonobj = createjson(infofile, argv[4], argv[1]);
145 storejsontofile(jsonobj, argv[3]);
148 storejsontodb(jsonobj, argv[5]);
151 talloc_free(jsonobj);
153 talloc_free(infofile);