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

Function make_map

tests/test_path_alias.c:23–70  ·  view source on GitHub ↗

Build a path alias map programmatically (no file I/O), respecting the * specificity ordering invariant the loader establishes via qsort. */

Source from the content-addressed store, hash-verified

21/* Build a path alias map programmatically (no file I/O), respecting the
22 * specificity ordering invariant the loader establishes via qsort. */
23static cbm_path_alias_map_t *make_map(const char *base_url, int count, ...) {
24 cbm_path_alias_map_t *map = calloc(1, sizeof(*map));
25 map->base_url = base_url ? strdup(base_url) : NULL;
26 map->entries = calloc((size_t)count, sizeof(cbm_path_alias_t));
27 map->count = count;
28
29 va_list args;
30 va_start(args, count);
31 for (int i = 0; i < count; i++) {
32 const char *alias_pattern = va_arg(args, const char *);
33 const char *target_pattern = va_arg(args, const char *);
34 const char *star = strchr(alias_pattern, '*');
35 if (star) {
36 map->entries[i].has_wildcard = true;
37 map->entries[i].alias_prefix =
38 cbm_strndup(alias_pattern, (size_t)(star - alias_pattern));
39 map->entries[i].alias_suffix = strdup(star + 1);
40 } else {
41 map->entries[i].has_wildcard = false;
42 map->entries[i].alias_prefix = strdup(alias_pattern);
43 map->entries[i].alias_suffix = strdup("");
44 }
45 const char *tstar = strchr(target_pattern, '*');
46 if (tstar) {
47 map->entries[i].target_prefix =
48 cbm_strndup(target_pattern, (size_t)(tstar - target_pattern));
49 map->entries[i].target_suffix = strdup(tstar + 1);
50 } else {
51 map->entries[i].target_prefix = strdup(target_pattern);
52 map->entries[i].target_suffix = strdup("");
53 }
54 }
55 va_end(args);
56
57 /* Mimic the loader's specificity sort. */
58 for (int i = 0; i < count - 1; i++) {
59 for (int j = i + 1; j < count; j++) {
60 size_t li = strlen(map->entries[i].alias_prefix);
61 size_t lj = strlen(map->entries[j].alias_prefix);
62 if (lj > li) {
63 cbm_path_alias_t tmp = map->entries[i];
64 map->entries[i] = map->entries[j];
65 map->entries[j] = tmp;
66 }
67 }
68 }
69 return map;
70}
71
72/* The map produced by make_map() owns all heap memory the same way the
73 * loader does, so the public free routine on the wrapping collection

Callers 1

test_path_alias.cFile · 0.85

Calls 1

cbm_strndupFunction · 0.85

Tested by

no test coverage detected