Write a mutable yyjson document to a file with pretty printing. */
| 756 | |
| 757 | /* Write a mutable yyjson document to a file with pretty printing. */ |
| 758 | static int write_json_file(const char *path, yyjson_mut_doc *doc) { |
| 759 | /* Ensure parent directory exists */ |
| 760 | char dir[CLI_BUF_1K]; |
| 761 | snprintf(dir, sizeof(dir), "%s", path); |
| 762 | char *last_slash = strrchr(dir, '/'); |
| 763 | if (last_slash) { |
| 764 | *last_slash = '\0'; |
| 765 | mkdirp(dir, DIR_PERMS); |
| 766 | } |
| 767 | |
| 768 | yyjson_write_flag flags = YYJSON_WRITE_PRETTY | YYJSON_WRITE_ESCAPE_UNICODE; |
| 769 | size_t len; |
| 770 | char *json = yyjson_mut_write(doc, flags, &len); |
| 771 | if (!json) { |
| 772 | return CLI_ERR; |
| 773 | } |
| 774 | |
| 775 | FILE *f = fopen(path, "w"); |
| 776 | if (!f) { |
| 777 | free(json); |
| 778 | return CLI_ERR; |
| 779 | } |
| 780 | |
| 781 | size_t written = fwrite(json, CLI_ELEM_SIZE, len, f); |
| 782 | /* Add trailing newline */ |
| 783 | (void)fputc('\n', f); |
| 784 | (void)fclose(f); |
| 785 | free(json); |
| 786 | |
| 787 | return written == len ? 0 : CLI_ERR; |
| 788 | } |
| 789 | |
| 790 | /* ── Editor MCP: Cursor/Windsurf/Gemini (mcpServers key) ──────── */ |
| 791 |
no test coverage detected