infotojson.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* This extract info from _info.c and create json file and also optionally store to db */
  2. #include "infotojson.h"
  3. /*creating json structure for storing to file/db*/
  4. static struct json *createjson(char **infofile, char *author)
  5. {
  6. struct json *jsonobj;
  7. unsigned int modulename;
  8. if (infofile == NULL || author == NULL) {
  9. printf("Error Author or Info file is NULL\n");
  10. exit(1);
  11. }
  12. //jsonobj = (struct json *)palloc(sizeof(struct json));
  13. jsonobj = talloc(NULL, struct json);
  14. if (!jsonobj)
  15. errx(1, "talloc error");
  16. jsonobj->author = author;
  17. /* First line should be module name and short description */
  18. modulename = strchr(infofile[0], '-') - infofile[0];
  19. jsonobj->module = talloc_strndup(jsonobj, infofile[0], modulename - 1);
  20. if (!jsonobj->module)
  21. errx(1, "talloc error");
  22. jsonobj->title = infofile[0];
  23. jsonobj->desc = &infofile[1];
  24. return jsonobj;
  25. }
  26. /*extracting title and description from _info.c files*/
  27. static char **extractinfo(char **file)
  28. {
  29. char **infofile;
  30. unsigned int count = 0, j = 0, num_lines = 0;
  31. bool printing = false;
  32. while (file[num_lines++]);
  33. infofile = talloc_array(NULL, char *, num_lines);
  34. //(char **) palloc(size * sizeof(char *));
  35. for (j = 0; j < num_lines - 1; j++) {
  36. if (streq(file[j], "/**")) {
  37. printing = true;
  38. }
  39. else if (streq(file[j], " */"))
  40. printing = false;
  41. else if (printing) {
  42. if (strstarts(file[j], " * "))
  43. infofile[count++] = file[j] + 3;
  44. else if (strstarts(file[j], " *"))
  45. infofile[count++] = file[j] + 2;
  46. else {
  47. err(1,"Error in comments structure\n%d",j);
  48. exit(1);
  49. }
  50. }
  51. }
  52. infofile[count] = NULL;
  53. return infofile;
  54. }
  55. /*storing json structure to json file*/
  56. static int storejsontofile(const struct json *jsonobj, const char *file)
  57. {
  58. FILE *fp;
  59. unsigned int j = 0;
  60. fp = fopen(file, "wt");
  61. fprintf(fp,"\"Module\":\"%s\",\n",jsonobj->module);
  62. fprintf(fp,"\"Title\":\"%s\",\n",jsonobj->title);
  63. fprintf(fp,"\"Author\":\"%s\",\n",jsonobj->author);
  64. fprintf(fp,"\"Description\":[\n");
  65. for (j = 0; jsonobj->desc[j]; j++)
  66. fprintf(fp,"{\n\"str\":\"%s\"\n},\n",jsonobj->desc[j]);
  67. fprintf(fp,"]\n");
  68. fclose(fp);
  69. return 1;
  70. }
  71. /*storing json structure to db*/
  72. static int storejsontodb(const struct json *jsonobj, const char *db)
  73. {
  74. char *cmd, *query, *desc;
  75. sqlite3 *handle;
  76. struct db_query *q;
  77. handle = db_open(db);
  78. query = talloc_asprintf(NULL, "SELECT module from search where module=\"%s\";", jsonobj->module);
  79. q = db_query(handle, query);
  80. desc = strjoin(NULL,jsonobj->desc,"\n");
  81. strreplace(desc, '\'', ' ');
  82. if (!q->num_rows)
  83. cmd = talloc_asprintf(NULL, "INSERT INTO search VALUES(\"%s\",\"%s\",\"%s\",'%s\');",
  84. jsonobj->module, jsonobj->author, jsonobj->title, desc);
  85. else
  86. cmd = talloc_asprintf(NULL, "UPDATE search set author=\"%s\", title=\"%s\", desc='%s\' where module=\"%s\";",
  87. jsonobj->author, jsonobj->title, desc, jsonobj->module);
  88. db_command(handle, cmd);
  89. db_close(handle);
  90. talloc_free(query);
  91. talloc_free(desc);
  92. talloc_free(cmd);
  93. return 1;
  94. }
  95. int main(int argc, char *argv[])
  96. {
  97. char *file;
  98. char **lines;
  99. char **infofile;
  100. struct json *jsonobj;
  101. talloc_enable_leak_report();
  102. if (argc < 4) {
  103. errx(1, "usage: infotojson infofile jsonfile author [sqlitedb]\n");
  104. return 1;
  105. }
  106. file = grab_file(NULL, argv[1]);
  107. if (!file)
  108. err(1, "Reading file %s", argv[1]);
  109. lines = strsplit(NULL, file, "\n", NULL);
  110. //extract info from lines
  111. infofile = extractinfo(lines);
  112. //create json obj
  113. jsonobj = createjson(infofile, argv[3]);
  114. //store to file
  115. storejsontofile(jsonobj, argv[2]);
  116. if (argv[4] != NULL)
  117. storejsontodb(jsonobj, argv[4]);
  118. talloc_free(file);
  119. talloc_free(jsonobj);
  120. talloc_free(lines);
  121. talloc_free(infofile);
  122. return 0;
  123. }