| 8179 | } |
| 8180 | |
| 8181 | int cbm_store_adr_update_sections(cbm_store_t *s, const char *project, const char **keys, |
| 8182 | const char **values, int count, cbm_adr_t *out) { |
| 8183 | /* Get existing ADR */ |
| 8184 | cbm_adr_t existing; |
| 8185 | int rc = cbm_store_adr_get(s, project, &existing); |
| 8186 | if (rc != CBM_STORE_OK) { |
| 8187 | store_set_error(s, "no existing ADR to update"); |
| 8188 | return rc; |
| 8189 | } |
| 8190 | |
| 8191 | /* Parse existing sections */ |
| 8192 | cbm_adr_sections_t sections = cbm_adr_parse_sections(existing.content); |
| 8193 | cbm_store_adr_free(&existing); |
| 8194 | |
| 8195 | /* Merge new sections */ |
| 8196 | for (int i = 0; i < count; i++) { |
| 8197 | bool found = false; |
| 8198 | for (int j = 0; j < sections.count; j++) { |
| 8199 | if (strcmp(sections.keys[j], keys[i]) == 0) { |
| 8200 | free(sections.values[j]); |
| 8201 | sections.values[j] = heap_strdup(values[i]); |
| 8202 | found = true; |
| 8203 | break; |
| 8204 | } |
| 8205 | } |
| 8206 | if (!found && sections.count < ST_BUF_16) { |
| 8207 | sections.keys[sections.count] = heap_strdup(keys[i]); |
| 8208 | sections.values[sections.count] = heap_strdup(values[i]); |
| 8209 | sections.count++; |
| 8210 | } |
| 8211 | } |
| 8212 | |
| 8213 | /* Render merged */ |
| 8214 | char *merged = cbm_adr_render(§ions); |
| 8215 | cbm_adr_sections_free(§ions); |
| 8216 | |
| 8217 | /* Check length */ |
| 8218 | if ((int)strlen(merged) > CBM_ADR_MAX_LENGTH) { |
| 8219 | char msg[CBM_SZ_128]; |
| 8220 | snprintf(msg, sizeof(msg), "merged ADR exceeds %d chars (%d chars)", CBM_ADR_MAX_LENGTH, |
| 8221 | (int)strlen(merged)); |
| 8222 | store_set_error(s, msg); |
| 8223 | free(merged); |
| 8224 | return CBM_STORE_ERR; |
| 8225 | } |
| 8226 | |
| 8227 | /* Store merged */ |
| 8228 | rc = cbm_store_adr_store(s, project, merged); |
| 8229 | free(merged); |
| 8230 | if (rc != CBM_STORE_OK) { |
| 8231 | return rc; |
| 8232 | } |
| 8233 | |
| 8234 | return cbm_store_adr_get(s, project, out); |
| 8235 | } |
| 8236 | |
| 8237 | void cbm_store_adr_free(cbm_adr_t *adr) { |
| 8238 | if (!adr) { |
no test coverage detected