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

static size_t indexHtmlSize = 0;
static char* indexHtml = NULL;
#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", indexHtml);
for (int i = 0; i < numAllStructs; i++) {
struct StructInfo *structInfo = allStructs + i;
printStructInfo(structInfo);
ssize_t byteCounter = 0;
sb_concatf(
"<div class='struct-info'>"
"<label class='struct-info-header'>struct %s - alias: %s</label>"
"<div class='struct-info-byte-row'>"
, structInfo->name, structInfo->alias);
for (int d = 0; d < structInfo->numDeclarations; d++) {
struct Declaration *decl = structInfo->declarations + d;
bool truncate32 = false;
if (decl->size > 32) {
truncate32 = true;
}
sb_concatf("%s", "<div class='struct-info-bytegroup'>");
bool first = (d % 2) == 0;
const char* positionClass = first ? "struct-info-declaration-top" : "struct-info-declaration-bottom";
if (decl->size == -1) {
sb_concatf(
"<div class='struct-info-byte struct-info-byte-unknown'>?"
"<div class='struct-info-declaration %s'>"
"%s <span class='struct-info-declaration-name'>%s</span>"
"</div>"
"</div>"
"<div class='struct-info-byte struct-info-byte-ellipsis'>...</div>"
, positionClass, decl->type, decl->name);
} else {
for (int b = 0; b < decl->size; b++) {
if (b == 0) {
sb_concatf(
"<div class='struct-info-byte struct-info-byte-first'>"
"<div class='struct-info-declaration %s'>"
"%s <span class='struct-info-declaration-name'>%s</span>"
"</div>"
"</div>"
, positionClass, decl->type, decl->name);
} else if (truncate32 && b == 32) {
sb_concatf("<div class='struct-info-byte struct-info-byte-counted-ellipsis'>...[%ld]</div>", decl->size - b);
break;
} else {
sb_concatf("%s", "<div class='struct-info-byte'></div>");
}
}
}
sb_concatf("%s", "</div>");
}
sb_concatf("%s", "</div></div>");
}
// don't forget the closing body and html tags
sb_concatf("%s", "</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");
}
}