| 8196 | } |
| 8197 | |
| 8198 | int cbm_store_adr_update_sections(cbm_store_t *s, const char *project, const char **keys, |
| 8199 | const char **values, int count, cbm_adr_t *out) { |
| 8200 | /* Get existing ADR */ |
| 8201 | cbm_adr_t existing; |
| 8202 | int rc = cbm_store_adr_get(s, project, &existing); |
| 8203 | if (rc != CBM_STORE_OK) { |
| 8204 | store_set_error(s, "no existing ADR to update"); |
| 8205 | return rc; |
| 8206 | } |
| 8207 | |
| 8208 | /* Parse existing sections */ |
| 8209 | cbm_adr_sections_t sections = cbm_adr_parse_sections(existing.content); |
| 8210 | cbm_store_adr_free(&existing); |
| 8211 | |
| 8212 | /* Merge new sections */ |
| 8213 | for (int i = 0; i < count; i++) { |
| 8214 | bool found = false; |
| 8215 | for (int j = 0; j < sections.count; j++) { |
| 8216 | if (strcmp(sections.keys[j], keys[i]) == 0) { |
| 8217 | free(sections.values[j]); |
| 8218 | sections.values[j] = heap_strdup(values[i]); |
| 8219 | found = true; |
| 8220 | break; |
| 8221 | } |
| 8222 | } |
| 8223 | if (!found && sections.count < ST_BUF_16) { |
| 8224 | sections.keys[sections.count] = heap_strdup(keys[i]); |
| 8225 | sections.values[sections.count] = heap_strdup(values[i]); |
| 8226 | sections.count++; |
| 8227 | } |
| 8228 | } |
| 8229 | |
| 8230 | /* Render merged */ |
| 8231 | char *merged = cbm_adr_render(§ions); |
| 8232 | cbm_adr_sections_free(§ions); |
| 8233 | |
| 8234 | /* Check length */ |
| 8235 | if ((int)strlen(merged) > CBM_ADR_MAX_LENGTH) { |
| 8236 | char msg[CBM_SZ_128]; |
| 8237 | snprintf(msg, sizeof(msg), "merged ADR exceeds %d chars (%d chars)", CBM_ADR_MAX_LENGTH, |
| 8238 | (int)strlen(merged)); |
| 8239 | store_set_error(s, msg); |
| 8240 | free(merged); |
| 8241 | return CBM_STORE_ERR; |
| 8242 | } |
| 8243 | |
| 8244 | /* Store merged */ |
| 8245 | rc = cbm_store_adr_store(s, project, merged); |
| 8246 | free(merged); |
| 8247 | if (rc != CBM_STORE_OK) { |
| 8248 | return rc; |
| 8249 | } |
| 8250 | |
| 8251 | return cbm_store_adr_get(s, project, out); |
| 8252 | } |
| 8253 | |
| 8254 | void cbm_store_adr_free(cbm_adr_t *adr) { |
| 8255 | if (!adr) { |
no test coverage detected