visualize the data structures in a C program
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.4 KiB

2 years ago
  1. static size_t indexHtmlSize = 0;
  2. static char* indexHtml = NULL;
  3. #define sb_concatf(fmt, ...) \
  4. if ((sbc - sbi) < 1024) { \
  5. sbc *= 1.5; \
  6. stringBuffer = realloc(stringBuffer, sbc); \
  7. } \
  8. if ((result = snprintf(stringBuffer + sbi, sbc - sbi, fmt, ##__VA_ARGS__)) > 0) sbi += result; \
  9. else die("fatal error concating to string");
  10. static void outputHtml() {
  11. static int sbi = 0;
  12. static int sbc = 50 * 1024;
  13. static char* stringBuffer = NULL;
  14. if (stringBuffer == NULL) stringBuffer = malloc(sizeof(char) * sbc);
  15. if (indexHtml == NULL) indexHtml = readWholeFile("base-index.html", &indexHtmlSize);
  16. int result = 0;
  17. sb_concatf("%s", indexHtml);
  18. for (int i = 0; i < numAllStructs; i++) {
  19. struct StructInfo *structInfo = allStructs + i;
  20. printStructInfo(structInfo);
  21. ssize_t byteCounter = 0;
  22. sb_concatf(
  23. "<div class='struct-info'>"
  24. "<label class='struct-info-header'>struct %s - alias: %s</label>"
  25. "<div class='struct-info-byte-row'>"
  26. , structInfo->name, structInfo->alias);
  27. for (int d = 0; d < structInfo->numDeclarations; d++) {
  28. struct Declaration *decl = structInfo->declarations + d;
  29. bool truncate32 = false;
  30. if (decl->size > 32) {
  31. truncate32 = true;
  32. }
  33. sb_concatf("%s", "<div class='struct-info-bytegroup'>");
  34. bool first = (d % 2) == 0;
  35. const char* positionClass = first ? "struct-info-declaration-top" : "struct-info-declaration-bottom";
  36. if (decl->size == -1) {
  37. sb_concatf(
  38. "<div class='struct-info-byte struct-info-byte-unknown'>?"
  39. "<div class='struct-info-declaration %s'>"
  40. "%s <span class='struct-info-declaration-name'>%s</span>"
  41. "</div>"
  42. "</div>"
  43. "<div class='struct-info-byte struct-info-byte-ellipsis'>...</div>"
  44. , positionClass, decl->type, decl->name);
  45. } else {
  46. for (int b = 0; b < decl->size; b++) {
  47. if (b == 0) {
  48. sb_concatf(
  49. "<div class='struct-info-byte struct-info-byte-first'>"
  50. "<div class='struct-info-declaration %s'>"
  51. "%s <span class='struct-info-declaration-name'>%s</span>"
  52. "</div>"
  53. "</div>"
  54. , positionClass, decl->type, decl->name);
  55. } else if (truncate32 && b == 32) {
  56. sb_concatf("<div class='struct-info-byte struct-info-byte-counted-ellipsis'>...[%ld]</div>", decl->size - b);
  57. break;
  58. } else {
  59. sb_concatf("%s", "<div class='struct-info-byte'></div>");
  60. }
  61. }
  62. }
  63. sb_concatf("%s", "</div>");
  64. }
  65. sb_concatf("%s", "</div></div>");
  66. }
  67. // don't forget the closing body and html tags
  68. sb_concatf("%s", "</body></html>");
  69. // write the index.html file out to disk
  70. FILE* fp = fopen("index.html", "wb");
  71. if (fp == NULL) {
  72. die("failed to open the file index.html");
  73. }
  74. size_t writtenCount = fwrite(stringBuffer, 1, sbi, fp);
  75. fclose(fp);
  76. if (writtenCount != sbi) {
  77. die("wrote only partially");
  78. }
  79. }