| 11111 | } |
| 11112 | |
| 11113 | char *cbm_adr_render(const cbm_adr_sections_t *sections) { |
| 11114 | if (!sections || sections->count == 0) { |
| 11115 | return heap_strdup(""); |
| 11116 | } |
| 11117 | |
| 11118 | char buf[ST_MAX_DEGREE * ST_GROWTH] = ""; |
| 11119 | int pos = 0; |
| 11120 | bool rendered[ST_MAX_SECTIONS] = {false}; |
| 11121 | |
| 11122 | /* Canonical sections first, in order */ |
| 11123 | for (int c = 0; c < canonical_section_count; c++) { |
| 11124 | for (int i = 0; i < sections->count; i++) { |
| 11125 | if (rendered[i]) { |
| 11126 | continue; |
| 11127 | } |
| 11128 | if (strcmp(sections->keys[i], canonical_sections[c]) == 0) { |
| 11129 | pos = adr_render_section(buf, (int)sizeof(buf), pos, sections->keys[i], |
| 11130 | sections->values[i]); |
| 11131 | rendered[i] = true; |
| 11132 | break; |
| 11133 | } |
| 11134 | } |
| 11135 | } |
| 11136 | |
| 11137 | /* Non-canonical sections alphabetically */ |
| 11138 | int extra[ST_BUF_16]; |
| 11139 | int nextra = 0; |
| 11140 | for (int i = 0; i < sections->count; i++) { |
| 11141 | if (!rendered[i]) { |
| 11142 | extra[nextra++] = i; |
| 11143 | } |
| 11144 | } |
| 11145 | for (int i = SKIP_ONE; i < nextra; i++) { |
| 11146 | int j = i; |
| 11147 | while (j > 0 && strcmp(sections->keys[extra[j]], sections->keys[extra[j - SKIP_ONE]]) < 0) { |
| 11148 | int tmp = extra[j]; |
| 11149 | extra[j] = extra[j - SKIP_ONE]; |
| 11150 | extra[j - SKIP_ONE] = tmp; |
| 11151 | j--; |
| 11152 | } |
| 11153 | } |
| 11154 | for (int i = 0; i < nextra; i++) { |
| 11155 | int idx = extra[i]; |
| 11156 | pos = adr_render_section(buf, (int)sizeof(buf), pos, sections->keys[idx], |
| 11157 | sections->values[idx]); |
| 11158 | } |
| 11159 | |
| 11160 | return heap_strdup(buf); |
| 11161 | } |
| 11162 | |
| 11163 | int cbm_adr_validate_content(const char *content, char *errbuf, int errbuf_size) { |
| 11164 | cbm_adr_sections_t sections = cbm_adr_parse_sections(content); |
no test coverage detected