]> git.ozlabs.org Git - ccan/commitdiff
Revised version of infotojson after comments
authorgdinesh@csa.iisc.ernet.in <>
Mon, 28 Jul 2008 07:11:34 +0000 (12:41 +0530)
committergdinesh@csa.iisc.ernet.in <>
Mon, 28 Jul 2008 07:11:34 +0000 (12:41 +0530)
tools/_infotojson/database.h
tools/_infotojson/infotojson.c
tools/_infotojson/infotojson.h

index 7081df43e9981a7bc0aebcea97c5d37dbbd5a0f0..f807cbde269b7cc7e3cc9af40222d1b09228d92d 100644 (file)
@@ -1,6 +1,4 @@
 /* Simple SQL-style database ops.  Currently implemented for sqlite3. */
 /* 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.. */
 #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);
 
 /* 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);
 /* Closes database (only called when everything OK). */
 void db_close(void *h);
-
-//#endif /* _UPLOAD_ANALYSIS_DATABASE_H */
index e169fe17c953051c863650310d6c657067a0dfc2..86769bbccc59c1ac215773413dfbae570d930fcb 100644 (file)
@@ -32,24 +32,33 @@ static void *grab_file(void *ctx, const char *filename)
 }
 
 /*creating json structure for storing to file/db*/
 }
 
 /*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;
 
 {
        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);
        }
 
                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;
 
        jsonobj->author = author;
 
+        /* First line should be module name and short description */
        modulename =  strchr(infofile[0], '-') - infofile[0];
        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];
 
        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*/
 }
 
 /*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;
        
        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;
                } 
                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 {
                        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);
                        }
                }
                                exit(1);
                        }
                }
@@ -89,44 +99,47 @@ char **extractinfo(char **file)
 }
 
 /*storing json structure to json 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");
 {
        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");      
        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;
        fprintf(fp,"]\n");
        fclose(fp);
        return 1;
-       
 }
 
 /*storing json structure to db*/
 }
 
 /*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);
        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);
        q = db_query(handle, query);
+       
+       desc = strjoin(NULL,jsonobj->desc,"\n");
+       strreplace(desc, '\'', ' ');
        if (!q->num_rows)
        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
        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);
        db_command(handle, cmd);        
        db_close(handle);
+       talloc_free(query);
+       talloc_free(desc);
+       talloc_free(cmd);
        return 1;
 }
 
        return 1;
 }
 
@@ -135,11 +148,11 @@ int main(int argc, char *argv[])
        char *file;
        char **lines;
        char **infofile;
        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;
        }
                
                return 1;
        }
                
@@ -158,9 +171,12 @@ int main(int argc, char *argv[])
        //store to file
        storejsontofile(jsonobj, argv[2]);
        
        //store to file
        storejsontofile(jsonobj, argv[2]);
        
-       if(argv[4] != NULL)
+       if (argv[4] != NULL)
                storejsontodb(jsonobj, argv[4]);
                
        talloc_free(file);
                storejsontodb(jsonobj, argv[4]);
                
        talloc_free(file);
+       talloc_free(jsonobj);
+       talloc_free(lines);
+       talloc_free(infofile);  
        return 0;
 }
        return 0;
 }
index c3c9658c53fd2831e687a600e694d750aea20af2..632debb32cdaac5872423f280f5040424420cd35 100644 (file)
  };
  
  /* Function for storing json structure to file given struct json*/ 
  };
  
  /* 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*/
 
 /*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*/
 
 /*create json structure*/
-struct json * createjson(char **infofile, char *author);
+static struct json *createjson(char **infofile, char *author);
 
 /*Extract info from file*/
 
 /*Extract info from file*/
-char ** extractinfo(char **file);
+static char **extractinfo(char **file);