Revised version of infotojson after comments

parent e927b4bd
/* Simple SQL-style database ops. Currently implemented for sqlite3. */
//#ifndef _UPLOAD_ANALYSIS_DATABASE_H
//#define _UPLOAD_ANALYSIS_DATABASE_H
#include <stdbool.h>
/* Returns handle to the database.. */
......@@ -18,13 +16,5 @@ struct db_query *db_query(void *h, const char *query);
/* Runs command (CREATE TABLE/INSERT) */
void db_command(void *h, const char *command);
/* Starts transaction. Doesn't need to nest. */
//void db_transaction_start(void *h);
/* Finishes transaction, or rolls it back and caller needs to start again. */
//bool db_transaction_finish(void *h);
/* Closes database (only called when everything OK). */
void db_close(void *h);
//#endif /* _UPLOAD_ANALYSIS_DATABASE_H */
......@@ -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;
}
......@@ -25,13 +25,13 @@
};
/* Function for storing json structure to file given struct json*/
int storejsontofile(struct json *jsonobj, char *jsonfile);
static int storejsontofile(const struct json *jsonobj, const char *jsonfile);
/*Function to store in database*/
int storejsontodb(struct json *jsonobj, char *db);
static int storejsontodb(const struct json *jsonobj, const char *db);
/*create json structure*/
struct json * createjson(char **infofile, char *author);
static struct json *createjson(char **infofile, char *author);
/*Extract info from file*/
char ** extractinfo(char **file);
static char **extractinfo(char **file);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment