MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / md_sections

Function md_sections

internal/cbm/doclink_md.c:1721–1763  ·  view source on GitHub ↗

The file's sections: its Section definitions by start line. False only when * memory ran out (a file without headings has none, and that is no failure). */

Source from the content-addressed store, hash-verified

1719/* The file's sections: its Section definitions by start line. False only when
1720 * memory ran out (a file without headings has none, and that is no failure). */
1721static bool md_sections(CBMExtractCtx *ctx, CBMArena *a, md_section_t **out, int *count) {
1722 const CBMDefArray *defs = &ctx->result->defs;
1723 int n = 0;
1724 for (int i = 0; i < defs->count; i++) {
1725 const CBMDefinition *d = &defs->items[i];
1726 n += d->label && d->qualified_name && strcmp(d->label, "Section") == 0;
1727 }
1728 *out = NULL;
1729 *count = 0;
1730 if (n == 0) {
1731 return true;
1732 }
1733 md_section_t *secs = (md_section_t *)cbm_arena_alloc(a, (size_t)n * sizeof(*secs));
1734 if (!secs) {
1735 return false;
1736 }
1737 for (int i = 0; i < defs->count; i++) {
1738 const CBMDefinition *d = &defs->items[i];
1739 if (d->label && d->qualified_name && strcmp(d->label, "Section") == 0) {
1740 secs[(*count)++] =
1741 (md_section_t){.line = d->start_line, .qn = d->qualified_name, .shared_qn = false};
1742 }
1743 }
1744 /* repeated qualified names, found through a copy sorted by name */
1745 const md_section_t **by_qn =
1746 (const md_section_t **)cbm_arena_alloc(a, (size_t)n * sizeof(*by_qn));
1747 if (!by_qn) {
1748 return false;
1749 }
1750 for (int i = 0; i < *count; i++) {
1751 by_qn[i] = &secs[i];
1752 }
1753 qsort(by_qn, (size_t)*count, sizeof(*by_qn), md_section_ptr_qn_cmp);
1754 for (int i = SKIP_ONE; i < *count; i++) {
1755 if (strcmp(by_qn[i]->qn, by_qn[i - SKIP_ONE]->qn) == 0) {
1756 ((md_section_t *)by_qn[i])->shared_qn = true;
1757 ((md_section_t *)by_qn[i - SKIP_ONE])->shared_qn = true;
1758 }
1759 }
1760 qsort(secs, (size_t)*count, sizeof(*secs), md_section_cmp);
1761 *out = secs;
1762 return true;
1763}
1764
1765void cbm_doclink_md_scan_file(CBMExtractCtx *ctx) {
1766 if (!ctx || !ctx->result || !ctx->source || ctx->source_len <= 0) {

Callers 1

cbm_doclink_md_scan_fileFunction · 0.85

Calls 1

cbm_arena_allocFunction · 0.70

Tested by

no test coverage detected