make_readme.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #define STB_DEFINE
  2. #include "../stb.h"
  3. int main(int argc, char **argv)
  4. {
  5. int i;
  6. int hlen, flen, listlen, total_lines = 0;
  7. char *header = stb_file("README.header.md", &hlen); // stb_file - read file into malloc()ed buffer
  8. char *footer = stb_file("README.footer.md", &flen); // stb_file - read file into malloc()ed buffer
  9. char **list = stb_stringfile("README.list", &listlen); // stb_stringfile - read file lines into malloced array of strings
  10. FILE *f = fopen("../README.md", "wb");
  11. fprintf(f, "<!--- THIS FILE IS AUTOMATICALLY GENERATED, DO NOT CHANGE IT BY HAND --->\r\n\r\n");
  12. fwrite(header, 1, hlen, f);
  13. for (i=0; i < listlen; ++i) {
  14. int num,j;
  15. char **tokens = stb_tokens_stripwhite(list[i], "|", &num); // stb_tokens -- tokenize string into malloced array of strings
  16. int num_lines;
  17. char **lines = stb_stringfile(stb_sprintf("../%s", tokens[0]), &num_lines);
  18. char *s1, *s2,*s3;
  19. if (lines == NULL) stb_fatal("Couldn't open '%s'", tokens[0]);
  20. s1 = strchr(lines[0], '-');
  21. if (!s1) stb_fatal("Couldn't find '-' before version number in %s", tokens[0]); // stb_fatal -- print error message & exit
  22. s2 = strchr(s1+2, '-');
  23. if (!s2) stb_fatal("Couldn't find '-' after version number in %s", tokens[0]); // stb_fatal -- print error message & exit
  24. *s2 = 0;
  25. s1 += 1;
  26. s1 = stb_trimwhite(s1); // stb_trimwhite -- advance pointer to after whitespace & delete trailing whitespace
  27. if (*s1 == 'v') ++s1;
  28. s3 = tokens[0];
  29. stb_trimwhite(s3);
  30. fprintf(f, "**[");
  31. if (strlen(s3) < 21) {
  32. fprintf(f, "%s", tokens[0]);
  33. } else {
  34. char buffer[256];
  35. strncpy(buffer, s3, 18);
  36. buffer[18] = 0;
  37. strcat(buffer, "...");
  38. fprintf(f, "%s", buffer);
  39. }
  40. fprintf(f, "](%s)**", tokens[0]);
  41. fprintf(f, " | %s", s1);
  42. s1 = stb_trimwhite(tokens[1]); // stb_trimwhite -- advance pointer to after whitespace & delete trailing whitespace
  43. s2 = stb_dupreplace(s1, " ", "&nbsp;"); // stb_dupreplace -- search & replace string and malloc result
  44. fprintf(f, " | %s", s2);
  45. free(s2);
  46. fprintf(f, " | %d", num_lines);
  47. total_lines += num_lines;
  48. for (j=2; j < num; ++j)
  49. fprintf(f, " | %s", tokens[j]);
  50. fprintf(f, "\r\n");
  51. }
  52. fprintf(f, "\r\n");
  53. fprintf(f, "Total libraries: %d\r\n", listlen);
  54. fprintf(f, "Total lines of C code: %d\r\n\r\n", total_lines);
  55. fwrite(footer, 1, flen, f);
  56. fclose(f);
  57. return 0;
  58. }