1 /* This extract info from _info.c and create json file and also optionally store to db */
2 #include "infotojson.h"
4 /* This version adds one byte (for nul term) */
5 static void *grab_file(void *ctx, const char *filename)
7 unsigned int max = 16384, size = 0;
11 if (streq(filename, "-"))
12 fd = dup(STDIN_FILENO);
14 fd = open(filename, O_RDONLY, 0);
19 buffer = talloc_array(ctx, char, max+1);
20 while ((ret = read(fd, buffer + size, max - size)) > 0) {
23 buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
34 /*creating json structure for storing to file/db*/
35 struct json * createjson(char **infofile, char *author)
38 unsigned int modulename;
40 if(infofile == NULL || author == NULL) {
41 printf("Error Author or Info file is NULL\n");
45 jsonobj = (struct json *)palloc(sizeof(struct json));
47 jsonobj->author = author;
49 modulename = strchr(infofile[0], '-') - infofile[0];
50 jsonobj->module = (char *)palloc(sizeof(char) * (modulename - 1));
51 strncpy(jsonobj->module, infofile[0], modulename - 1);
52 jsonobj->module[modulename - 1] = '\0';
54 jsonobj->title = infofile[0];
55 jsonobj->desc = &infofile[1];
60 /*extracting title and description from _info.c files*/
61 char **extractinfo(char **file)
63 char **infofile = NULL;
64 unsigned int count = 0, j = 0, size = 0;
65 bool printing = false;
68 infofile = (char **) palloc(size * sizeof(char *));
70 for (j = 0; j < size - 1; j++) {
71 if (streq(file[j], "/**")) {
74 else if (streq(file[j], " */"))
77 if (strstarts(file[j], " * "))
78 infofile[count++] = file[j] + 3;
79 else if (strstarts(file[j], " *"))
80 infofile[count++] = file[j] + 2;
82 printf("Error in comments structure\n%d",j);
87 infofile[count] = NULL;
91 /*storing json structure to json file*/
92 int storejsontofile(struct json *jsonobj, char *file)
96 fp = fopen(file, "wt");
98 fprintf(fp,"\"Module\":\"%s\",\n",jsonobj->module);
99 fprintf(fp,"\"Title\":\"%s\",\n",jsonobj->title);
100 fprintf(fp,"\"Author\":\"%s\",\n",jsonobj->author);
101 fprintf(fp,"\"Description\":[\n");
102 while(jsonobj->desc[j++])
103 fprintf(fp,"{\n\"str\":\"%s\"\n},\n",jsonobj->desc[j - 1]);
110 /*storing json structure to db*/
111 int storejsontodb(struct json *jsonobj, char *db)
118 handle = db_open(db);
120 query = aprintf("SELECT module from search where module=\"%s\";", jsonobj->module);
121 q = db_query(handle, query);
123 cmd = aprintf("INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\",'%s\');",
124 jsonobj->module, jsonobj->author, jsonobj->title, strjoin(NULL,jsonobj->desc,"\n"));
126 cmd = aprintf("UPDATE search set author=\"%s\", title=\"%s\", desc='%s\' where module=\"%s\";",
127 jsonobj->author, jsonobj->title, strjoin(NULL,jsonobj->desc,"\n"), jsonobj->module);
129 db_command(handle, cmd);
134 int main(int argc, char *argv[])
140 struct json *jsonobj = NULL;
143 printf("usage: infotojson infofile jsonfile author sqlitedb\n");
147 file = grab_file(NULL, argv[1]);
149 err(1, "Reading file %s", argv[1]);
151 lines = strsplit(NULL, file, "\n", NULL);
153 //extract info from lines
154 infofile = extractinfo(lines);
157 jsonobj = createjson(infofile, argv[3]);
160 storejsontofile(jsonobj, argv[2]);
163 storejsontodb(jsonobj, argv[4]);