From 3cf17ac088b9c60d595b908f90f1b1a96c5ec6bd Mon Sep 17 00:00:00 2001 From: Ronald Caesar Date: Sat, 17 Jan 2026 02:06:36 -0400 Subject: [PATCH] tools: fix CDoc ordering of types Fixes CDoc ordering the sidebar and main page types in alphabetical order. Only the sidebar should be sorted and the main page should be rendered the way its header file was parsed, top to bottom. Signed-off-by: Ronald Caesar --- include/bal_engine.h | 2 +- tools/cdoc.c | 49 ++++++++++++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/include/bal_engine.h b/include/bal_engine.h index b6ee49d..18a67c6 100644 --- a/include/bal_engine.h +++ b/include/bal_engine.h @@ -90,7 +90,7 @@ BAL_ALIGNED(64) typedef struct /// /// # Errors /// -/// Returns [`BAL_ERROR_INVALID_ARGUMENT` if the pointers are `NULL`. +/// Returns [`BAL_ERROR_INVALID_ARGUMENT`] if the pointers are `NULL`. /// /// Returns [`BAL_ERROR_ALLOCATION_FAILED`] if the allocator cannot fulfill the /// request. diff --git a/tools/cdoc.c b/tools/cdoc.c index 535096b..7ea2b93 100644 --- a/tools/cdoc.c +++ b/tools/cdoc.c @@ -589,24 +589,47 @@ void write_common_head(FILE* f, const char* title) { fprintf(f, ""); } +int compare_item_ptrs(const void* a, const void* b) { + const DocItem* da = *(const DocItem**)a; + const DocItem* db = *(const DocItem**)b; + + if (!da->name && !db->name) return 0; + if (!da->name) return 1; + if (!db->name) return -1; + + return strcmp(da->name, db->name); +} + void render_sidebar_section(FILE* f, FileContext* ctx, ItemKind kind, const char* title) { - int found = 0; - // Check if any items of this kind exist + size_t count = 0; + // Count items of this kind for(size_t i=0; icount; i++) { if (ctx->items[i].kind == kind) { - found = 1; - break; + count++; } } - // If found, print the header and the links - if (found) { - fprintf(f, "

%s

", title); - for(size_t i=0; icount; i++) { - if (ctx->items[i].kind == kind) { - fprintf(f, "%s", ctx->items[i].anchor_id, ctx->items[i].name); - } + + if (count == 0) return; + + // Create a temporary array of pointers + DocItem** ptrs = malloc(sizeof(DocItem*) * count); + size_t idx = 0; + for(size_t i=0; icount; i++) { + if (ctx->items[i].kind == kind) { + ptrs[idx++] = &ctx->items[i]; } } + + // Sort the pointers alphabetically for the sidebar + qsort(ptrs, count, sizeof(DocItem*), compare_item_ptrs); + + // Render sorted links + fprintf(f, "

%s

", title); + for(size_t i=0; i%s", ptrs[i]->anchor_id, ptrs[i]->name); + } + + free(ptrs); } void generate_file_html(ProjectContext* proj, FileContext* ctx, const char* out_dir) { @@ -860,10 +883,6 @@ int main(int argc, char** argv) { clang_disposeTranslationUnit(unit); } - for (size_t i = 0; i < proj.count; i++) { - qsort(proj.files[i].items, proj.files[i].count, sizeof(DocItem), compare_items); - } - printf("Generating HTML in '%s'...\n", out_dir); #ifdef _WIN32