Rebuild existing with its managed span rewritten around `owned` (install) * or removed (uninstall), keeping everything the span holds that is not * ours. `owned` is the block on install and the declaration of our tables on * removal. */
| 2230 | * ours. `owned` is the block on install and the declaration of our tables on |
| 2231 | * removal. */ |
| 2232 | static int toml_managed_rewrite(const char *existing, size_t existing_len, |
| 2233 | const toml_line_t *begin_line, const toml_line_t *end_line, |
| 2234 | const char *begin_marker, const char *end_marker, const char *owned, |
| 2235 | size_t owned_len, int install, toml_buffer_t *output) { |
| 2236 | toml_managed_span_t span = { |
| 2237 | .data = existing, |
| 2238 | .span_end = end_line->start, |
| 2239 | .owned = owned, |
| 2240 | .owned_len = owned_len, |
| 2241 | }; |
| 2242 | size_t tables = 0U; |
| 2243 | size_t tail_lead_end = existing_len; |
| 2244 | if (toml_section_end(owned, 0U, owned_len, &span.owned_first) != TOML_EDIT_OK || |
| 2245 | toml_declared_table(&span, 0U, &tables, NULL) != TOML_EDIT_OK || |
| 2246 | toml_section_end(existing, begin_line->full_end, span.span_end, &span.lead_end) != |
| 2247 | TOML_EDIT_OK || |
| 2248 | toml_section_end(existing, end_line->full_end, existing_len, &tail_lead_end) != |
| 2249 | TOML_EDIT_OK) { |
| 2250 | return TOML_EDIT_ERR; |
| 2251 | } |
| 2252 | toml_buffer_t moved = {0}; |
| 2253 | size_t foreign = 0U; |
| 2254 | int last_kind = TOML_SPAN_LEAD; |
| 2255 | if (toml_managed_span_pass(&span, TOML_SPAN_FOREIGN, 0U, &moved, &foreign, &last_kind) != |
| 2256 | TOML_EDIT_OK || |
| 2257 | (foreign != 0U && last_kind != TOML_SPAN_FOREIGN && |
| 2258 | toml_lines_significant(existing, end_line->full_end, tail_lead_end))) { |
| 2259 | toml_buffer_dispose(&moved); |
| 2260 | return TOML_EDIT_ERR; |
| 2261 | } |
| 2262 | |
| 2263 | size_t prefix_len = begin_line->start; |
| 2264 | if (prefix_len == 0U && existing_len >= 3U && (unsigned char)existing[0] == 0xefU && |
| 2265 | (unsigned char)existing[1] == 0xbbU && (unsigned char)existing[2] == 0xbfU) { |
| 2266 | prefix_len = 3U; |
| 2267 | } |
| 2268 | const char *newline = toml_newline_style(existing, existing_len); |
| 2269 | int keep_lead = !toml_lines_significant(owned, 0U, span.owned_first); |
| 2270 | size_t block_start = 0U; |
| 2271 | size_t block_end = 0U; |
| 2272 | int result = toml_buffer_append(output, existing, prefix_len) == TOML_EDIT_OK && |
| 2273 | (!keep_lead || |
| 2274 | toml_buffer_append(output, existing + begin_line->full_end, |
| 2275 | span.lead_end - begin_line->full_end) == TOML_EDIT_OK) |
| 2276 | ? TOML_EDIT_OK |
| 2277 | : TOML_EDIT_ERR; |
| 2278 | if (result == TOML_EDIT_OK && install) { |
| 2279 | block_start = output->len; |
| 2280 | result = |
| 2281 | toml_buffer_append_cstr(output, begin_marker) == TOML_EDIT_OK && |
| 2282 | toml_buffer_append_cstr(output, newline) == TOML_EDIT_OK && |
| 2283 | toml_append_managed_tables(&span, tables, newline, output) == TOML_EDIT_OK && |
| 2284 | toml_buffer_append_cstr(output, end_marker) == TOML_EDIT_OK && |
| 2285 | toml_buffer_append_cstr(output, newline) == TOML_EDIT_OK |
| 2286 | ? TOML_EDIT_OK |
| 2287 | : TOML_EDIT_ERR; |
| 2288 | block_end = output->len; |
| 2289 | } |
no test coverage detected