| 7985 | } |
| 7986 | |
| 7987 | int cbm_store_adr_update_sections(cbm_store_t *s, const char *project, const char **keys, |
| 7988 | const char **values, int count, cbm_adr_t *out) { |
| 7989 | /* Get existing ADR */ |
| 7990 | cbm_adr_t existing; |
| 7991 | int rc = cbm_store_adr_get(s, project, &existing); |
| 7992 | if (rc != CBM_STORE_OK) { |
| 7993 | store_set_error(s, "no existing ADR to update"); |
| 7994 | return rc; |
| 7995 | } |
| 7996 | |
| 7997 | /* Parse existing sections */ |
| 7998 | cbm_adr_sections_t sections = cbm_adr_parse_sections(existing.content); |
| 7999 | cbm_store_adr_free(&existing); |
| 8000 | |
| 8001 | /* Merge new sections */ |
| 8002 | for (int i = 0; i < count; i++) { |
| 8003 | bool found = false; |
| 8004 | for (int j = 0; j < sections.count; j++) { |
| 8005 | if (strcmp(sections.keys[j], keys[i]) == 0) { |
| 8006 | free(sections.values[j]); |
| 8007 | sections.values[j] = heap_strdup(values[i]); |
| 8008 | found = true; |
| 8009 | break; |
| 8010 | } |
| 8011 | } |
| 8012 | if (!found && sections.count < ST_BUF_16) { |
| 8013 | sections.keys[sections.count] = heap_strdup(keys[i]); |
| 8014 | sections.values[sections.count] = heap_strdup(values[i]); |
| 8015 | sections.count++; |
| 8016 | } |
| 8017 | } |
| 8018 | |
| 8019 | /* Render merged */ |
| 8020 | char *merged = cbm_adr_render(§ions); |
| 8021 | cbm_adr_sections_free(§ions); |
| 8022 | |
| 8023 | /* Check length */ |
| 8024 | if ((int)strlen(merged) > CBM_ADR_MAX_LENGTH) { |
| 8025 | char msg[CBM_SZ_128]; |
| 8026 | snprintf(msg, sizeof(msg), "merged ADR exceeds %d chars (%d chars)", CBM_ADR_MAX_LENGTH, |
| 8027 | (int)strlen(merged)); |
| 8028 | store_set_error(s, msg); |
| 8029 | free(merged); |
| 8030 | return CBM_STORE_ERR; |
| 8031 | } |
| 8032 | |
| 8033 | /* Store merged */ |
| 8034 | rc = cbm_store_adr_store(s, project, merged); |
| 8035 | free(merged); |
| 8036 | if (rc != CBM_STORE_OK) { |
| 8037 | return rc; |
| 8038 | } |
| 8039 | |
| 8040 | return cbm_store_adr_get(s, project, out); |
| 8041 | } |
| 8042 | |
| 8043 | void cbm_store_adr_free(cbm_adr_t *adr) { |
| 8044 | if (!adr) { |
no test coverage detected