infotojson.c 4.4 KB

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