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.
 
 

74 lines
2.4 KiB

static size_t indexHtmlSize = 0;
static char* indexHtml = NULL;
// @NOTE the assumption that we won't concat more than 1024 bytes at once.
#define sb_concatf(fmt, ...) \
if ((sbc - sbi) < 1024) { \
sbc *= 1.5; \
stringBuffer = realloc(stringBuffer, sbc); \
} \
if ((result = snprintf(stringBuffer + sbi, sbc - sbi, fmt, ##__VA_ARGS__)) > 0) sbi += result; \
else die("fatal error concating to string");
static void outputHtml() {
static int sbi = 0;
static int sbc = 50 * 1024;
static char* stringBuffer = NULL;
if (stringBuffer == NULL) stringBuffer = malloc(sizeof(char) * sbc);
if (indexHtml == NULL) indexHtml = readWholeFile("base-index.html", &indexHtmlSize);
int result = 0;
sb_concatf("%s\n<script>\nwindow.structs = JSON.parse(`[\n", indexHtml);
for (int i = 0; i < numAllStructs; i++) {
struct StructInfo *structInfo = allStructs + i;
sb_concatf("{"
"\"name\":\"%s\","
"\"alias\":\"%s\","
"\"filename\":\"%s\","
"\"lineNumber\":%d,"
"\"lineOffset\":%d,"
"\"size\":%ld,"
"\"declarations\":[",
structInfo->name,
structInfo->alias,
structInfo->filename,
structInfo->lineNumber,
structInfo->lineOffset,
structInfo->size
);
for (int j = 0; j < structInfo->numDeclarations; j++) {
struct Declaration *decl = structInfo->declarations + j;
sb_concatf("{"
"\"type\": \"%s\","
"\"name\": \"%s\","
"\"size\": \"%ld\","
"\"align\": \"%ld\","
"\"isBitfield\": %s}%s",
decl->type,
decl->name,
decl->size,
decl->align,
decl->isBitfield ? "true" : "false",
(j != (structInfo->numDeclarations - 1)) ? "," : "");
}
sb_concatf("]}%s", (i != (numAllStructs - 1)) ? "," : "");
}
sb_concatf("%s", "]`);renderAllStructs();\n</script></body></html>");
// write the index.html file out to disk
FILE* fp = fopen("index.html", "wb");
if (fp == NULL) {
die("failed to open the file index.html");
}
size_t writtenCount = fwrite(stringBuffer, 1, sbi, fp);
fclose(fp);
if (writtenCount != sbi) {
die("wrote only partially");
}
}