infotojson.c 4.9 KB

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