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 static 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));
46 jsonobj = talloc(NULL, struct json);
48 errx(1, "talloc error");
50 jsonobj->author = author;
52 /* First line should be module name and short description */
53 modulename = strchr(infofile[0], '-') - infofile[0];
55 jsonobj->module = talloc_strndup(jsonobj, infofile[0], modulename - 1);
57 errx(1, "talloc error");
59 //jsonobj->module = (char *)palloc(sizeof(char) * (modulename - 1));
60 //strncpy(jsonobj->module, infofile[0], modulename - 1);
61 //jsonobj->module[modulename - 1] = '\0';
63 jsonobj->title = infofile[0];
64 jsonobj->desc = &infofile[1];
69 /*extracting title and description from _info.c files*/
70 static char **extractinfo(char **file)
73 unsigned int count = 0, j = 0, num_lines = 0;
74 bool printing = false;
76 while (file[num_lines++]);
77 infofile = talloc_array(NULL, char *, num_lines);
78 //(char **) palloc(size * sizeof(char *));
80 for (j = 0; j < num_lines - 1; j++) {
81 if (streq(file[j], "/**")) {
84 else if (streq(file[j], " */"))
87 if (strstarts(file[j], " * "))
88 infofile[count++] = file[j] + 3;
89 else if (strstarts(file[j], " *"))
90 infofile[count++] = file[j] + 2;
92 err(1,"Error in comments structure\n%d",j);
97 infofile[count] = NULL;
101 /*storing json structure to json file*/
102 static int storejsontofile(const struct json *jsonobj, const char *file)
106 fp = fopen(file, "wt");
107 fprintf(fp,"\"Module\":\"%s\",\n",jsonobj->module);
108 fprintf(fp,"\"Title\":\"%s\",\n",jsonobj->title);
109 fprintf(fp,"\"Author\":\"%s\",\n",jsonobj->author);
110 fprintf(fp,"\"Description\":[\n");
111 for (j = 0; jsonobj->desc[j]; j++)
112 fprintf(fp,"{\n\"str\":\"%s\"\n},\n",jsonobj->desc[j]);
118 /*storing json structure to db*/
119 static int storejsontodb(const struct json *jsonobj, const char *db)
121 char *cmd, *query, *desc;
125 handle = db_open(db);
126 query = talloc_asprintf(NULL, "SELECT module from search where module=\"%s\";", jsonobj->module);
127 q = db_query(handle, query);
129 desc = strjoin(NULL,jsonobj->desc,"\n");
130 strreplace(desc, '\'', ' ');
132 cmd = talloc_asprintf(NULL, "INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\",'%s\');",
133 jsonobj->module, jsonobj->author, jsonobj->title, desc);
135 cmd = talloc_asprintf(NULL, "UPDATE search set author=\"%s\", title=\"%s\", desc='%s\' where module=\"%s\";",
136 jsonobj->author, jsonobj->title, desc, jsonobj->module);
138 db_command(handle, cmd);
146 int main(int argc, char *argv[])
151 struct json *jsonobj;
153 talloc_enable_leak_report();
155 errx(1, "usage: infotojson infofile jsonfile author [sqlitedb]\n");
159 file = grab_file(NULL, argv[1]);
161 err(1, "Reading file %s", argv[1]);
163 lines = strsplit(NULL, file, "\n", NULL);
165 //extract info from lines
166 infofile = extractinfo(lines);
169 jsonobj = createjson(infofile, argv[3]);
172 storejsontofile(jsonobj, argv[2]);
175 storejsontodb(jsonobj, argv[4]);
178 talloc_free(jsonobj);
180 talloc_free(infofile);