1 /* This extract info from _info.c and create json file and also optionally store to db */
2 #include "infotojson.h"
5 #define streq(a,b) (strcmp((a),(b)) == 0)
7 /* Does A start with B ? */
8 #define strstarts(a,b) (strncmp((a),(b),strlen(b)) == 0)
10 /* This version adds one byte (for nul term) */
11 static void *grab_file(void *ctx, const char *filename)
13 unsigned int max = 16384, size = 0;
17 if (streq(filename, "-"))
18 fd = dup(STDIN_FILENO);
20 fd = open(filename, O_RDONLY, 0);
25 buffer = talloc_array(ctx, char, max+1);
26 while ((ret = read(fd, buffer + size, max - size)) > 0) {
29 buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
40 /* This is a dumb one which copies. We could mangle instead. */
41 static char **split(const char *text)
44 unsigned int max = 64, num = 0;
46 lines = talloc_array(text, char *, max+1);
48 while (*text != '\0') {
49 unsigned int len = strcspn(text, "\n");
50 lines[num] = talloc_array(lines, char, len + 1);
51 memcpy(lines[num], text, len);
52 lines[num][len] = '\0';
55 lines = talloc_realloc(text, lines, char *, max*=2 + 1);
61 /*combin desc into an array to write to db*/
62 static char *combinedesc(char **desc)
64 unsigned int i = 0, size = 0;;
67 for(i = 0; desc[i]; i++)
68 size += strlen(desc[i]);
70 combine = (char *)palloc((size + i)* sizeof(char));
71 strcpy(combine, desc[0]);
73 for(i = 1; desc[i]; i++) {
74 strcat(combine, "\n");
75 strcat(combine, desc[i]);
77 strreplace(combine,'\'',' ');
81 /*creating json structure for storing to file/db*/
82 struct json * createjson(char **infofile, char *author)
85 unsigned int modulename;
87 if(infofile == NULL || author == NULL) {
88 printf("Error Author or Info file is NULL\n");
92 jsonobj = (struct json *)palloc(sizeof(struct json));
94 jsonobj->author = author;
96 modulename = strchr(infofile[0], '-') - infofile[0];
97 jsonobj->module = (char *)palloc(sizeof(char) * (modulename - 1));
98 strncpy(jsonobj->module, infofile[0], modulename - 1);
99 jsonobj->module[modulename - 1] = '\0';
101 jsonobj->title = infofile[0];
102 jsonobj->desc = &infofile[1];
107 /*extracting title and description from _info.c files*/
108 char **extractinfo(char **file)
110 char **infofile = NULL;
111 unsigned int count = 0, j = 0, size = 0;
112 bool printing = false;
115 infofile = (char **) palloc(size * sizeof(char *));
117 for (j = 0; j < size - 1; j++) {
118 if (streq(file[j], "/**")) {
121 else if (streq(file[j], " */"))
124 if (strstarts(file[j], " * "))
125 infofile[count++] = file[j] + 3;
126 else if (strstarts(file[j], " *"))
127 infofile[count++] = file[j] + 2;
129 printf("Error in comments structure\n%d",j);
134 infofile[count] = NULL;
138 /*storing json structure to json file*/
139 int storejsontofile(struct json *jsonobj, char *file)
143 fp = fopen(file, "wt");
145 fprintf(fp,"\"Module\":\"%s\",\n",jsonobj->module);
146 fprintf(fp,"\"Title\":\"%s\",\n",jsonobj->title);
147 fprintf(fp,"\"Author\":\"%s\",\n",jsonobj->author);
148 fprintf(fp,"\"Description\":[\n");
149 while(jsonobj->desc[j++])
150 fprintf(fp,"{\n\"str\":\"%s\"\n},\n",jsonobj->desc[j - 1]);
157 /*storing json structure to db*/
158 int storejsontodb(struct json *jsonobj, char *db)
165 handle = db_open(db);
167 query = aprintf("SELECT module from search where module=\"%s\";", jsonobj->module);
168 q = db_query(handle, query);
170 cmd = aprintf("INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\",'%s\');",
171 jsonobj->module, jsonobj->author, jsonobj->title, combinedesc(jsonobj->desc));
173 cmd = aprintf("UPDATE search set author=\"%s\", title=\"%s\", desc='%s\' where module=\"%s\";",
174 jsonobj->author, jsonobj->title, combinedesc(jsonobj->desc), jsonobj->module);
176 db_command(handle, cmd);
181 int main(int argc, char *argv[])
187 struct json *jsonobj = NULL;
190 printf("usage: infotojson infofile jsonfile author sqlitedb\n");
194 file = grab_file(NULL, argv[1]);
196 err(1, "Reading file %s", argv[1]);
200 //extract info from lines
201 infofile = extractinfo(lines);
204 jsonobj = createjson(infofile, argv[3]);
207 storejsontofile(jsonobj, argv[2]);
210 storejsontodb(jsonobj, argv[4]);