infotojson.c 4.0 KB

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