X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2F_infotojson%2Finfotojson.c;fp=tools%2F_infotojson%2Finfotojson.c;h=86769bbccc59c1ac215773413dfbae570d930fcb;hp=e169fe17c953051c863650310d6c657067a0dfc2;hb=b772f4d9252df33303c142e5750c24ca211ccfc2;hpb=e927b4bd14d50c6f3bb3ba5e658a80f9ee4ee27a diff --git a/tools/_infotojson/infotojson.c b/tools/_infotojson/infotojson.c index e169fe17..86769bbc 100644 --- a/tools/_infotojson/infotojson.c +++ b/tools/_infotojson/infotojson.c @@ -32,24 +32,33 @@ static void *grab_file(void *ctx, const char *filename) } /*creating json structure for storing to file/db*/ -struct json * createjson(char **infofile, char *author) +static struct json *createjson(char **infofile, char *author) { struct json *jsonobj; unsigned int modulename; - if(infofile == NULL || author == NULL) { + if (infofile == NULL || author == NULL) { printf("Error Author or Info file is NULL\n"); exit(1); } - jsonobj = (struct json *)palloc(sizeof(struct json)); - + //jsonobj = (struct json *)palloc(sizeof(struct json)); + jsonobj = talloc(NULL, struct json); + if (!jsonobj) + errx(1, "talloc error"); + jsonobj->author = author; + /* First line should be module name and short description */ modulename = strchr(infofile[0], '-') - infofile[0]; - jsonobj->module = (char *)palloc(sizeof(char) * (modulename - 1)); - strncpy(jsonobj->module, infofile[0], modulename - 1); - jsonobj->module[modulename - 1] = '\0'; + + jsonobj->module = talloc_strndup(jsonobj, infofile[0], modulename - 1); + if (!jsonobj->module) + errx(1, "talloc error"); + + //jsonobj->module = (char *)palloc(sizeof(char) * (modulename - 1)); + //strncpy(jsonobj->module, infofile[0], modulename - 1); + //jsonobj->module[modulename - 1] = '\0'; jsonobj->title = infofile[0]; jsonobj->desc = &infofile[1]; @@ -58,16 +67,17 @@ struct json * createjson(char **infofile, char *author) } /*extracting title and description from _info.c files*/ -char **extractinfo(char **file) +static char **extractinfo(char **file) { - char **infofile = NULL; - unsigned int count = 0, j = 0, size = 0; + char **infofile; + unsigned int count = 0, j = 0, num_lines = 0; bool printing = false; - while(file[size++]); - infofile = (char **) palloc(size * sizeof(char *)); + while (file[num_lines++]); + infofile = talloc_array(NULL, char *, num_lines); + //(char **) palloc(size * sizeof(char *)); - for (j = 0; j < size - 1; j++) { + for (j = 0; j < num_lines - 1; j++) { if (streq(file[j], "/**")) { printing = true; } @@ -79,7 +89,7 @@ char **extractinfo(char **file) else if (strstarts(file[j], " *")) infofile[count++] = file[j] + 2; else { - printf("Error in comments structure\n%d",j); + err(1,"Error in comments structure\n%d",j); exit(1); } } @@ -89,44 +99,47 @@ char **extractinfo(char **file) } /*storing json structure to json file*/ -int storejsontofile(struct json *jsonobj, char *file) +static int storejsontofile(const struct json *jsonobj, const char *file) { FILE *fp; unsigned int j = 0; fp = fopen(file, "wt"); - fprintf(fp,"\"Module\":\"%s\",\n",jsonobj->module); fprintf(fp,"\"Title\":\"%s\",\n",jsonobj->title); fprintf(fp,"\"Author\":\"%s\",\n",jsonobj->author); fprintf(fp,"\"Description\":[\n"); - while(jsonobj->desc[j++]) - fprintf(fp,"{\n\"str\":\"%s\"\n},\n",jsonobj->desc[j - 1]); + for (j = 0; jsonobj->desc[j]; j++) + fprintf(fp,"{\n\"str\":\"%s\"\n},\n",jsonobj->desc[j]); fprintf(fp,"]\n"); fclose(fp); return 1; - } /*storing json structure to db*/ -int storejsontodb(struct json *jsonobj, char *db) +static int storejsontodb(const struct json *jsonobj, const char *db) { - char *cmd, *query; + char *cmd, *query, *desc; sqlite3 *handle; struct db_query *q; handle = db_open(db); - - query = aprintf("SELECT module from search where module=\"%s\";", jsonobj->module); + query = talloc_asprintf(NULL, "SELECT module from search where module=\"%s\";", jsonobj->module); q = db_query(handle, query); + + desc = strjoin(NULL,jsonobj->desc,"\n"); + strreplace(desc, '\'', ' '); if (!q->num_rows) - cmd = aprintf("INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\",'%s\');", - jsonobj->module, jsonobj->author, jsonobj->title, strjoin(NULL,jsonobj->desc,"\n")); + cmd = talloc_asprintf(NULL, "INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\",'%s\');", + jsonobj->module, jsonobj->author, jsonobj->title, desc); else - cmd = aprintf("UPDATE search set author=\"%s\", title=\"%s\", desc='%s\' where module=\"%s\";", - jsonobj->author, jsonobj->title, strjoin(NULL,jsonobj->desc,"\n"), jsonobj->module); - + cmd = talloc_asprintf(NULL, "UPDATE search set author=\"%s\", title=\"%s\", desc='%s\' where module=\"%s\";", + jsonobj->author, jsonobj->title, desc, jsonobj->module); + db_command(handle, cmd); db_close(handle); + talloc_free(query); + talloc_free(desc); + talloc_free(cmd); return 1; } @@ -135,11 +148,11 @@ int main(int argc, char *argv[]) char *file; char **lines; char **infofile; + struct json *jsonobj; - struct json *jsonobj = NULL; - - if(argc < 4) { - printf("usage: infotojson infofile jsonfile author sqlitedb\n"); + talloc_enable_leak_report(); + if (argc < 4) { + errx(1, "usage: infotojson infofile jsonfile author [sqlitedb]\n"); return 1; } @@ -158,9 +171,12 @@ int main(int argc, char *argv[]) //store to file storejsontofile(jsonobj, argv[2]); - if(argv[4] != NULL) + if (argv[4] != NULL) storejsontodb(jsonobj, argv[4]); talloc_free(file); + talloc_free(jsonobj); + talloc_free(lines); + talloc_free(infofile); return 0; }