Read a JSON file into a yyjson document. Returns NULL on error. */
| 720 | |
| 721 | /* Read a JSON file into a yyjson document. Returns NULL on error. */ |
| 722 | static yyjson_doc *read_json_file(const char *path) { |
| 723 | FILE *f = fopen(path, "r"); |
| 724 | if (!f) { |
| 725 | return NULL; |
| 726 | } |
| 727 | |
| 728 | (void)fseek(f, 0, SEEK_END); |
| 729 | long size = ftell(f); |
| 730 | (void)fseek(f, 0, SEEK_SET); |
| 731 | |
| 732 | if (size <= 0 || size > (long)CLI_MB_10 * CLI_MB_FACTOR) { |
| 733 | (void)fclose(f); |
| 734 | return NULL; |
| 735 | } |
| 736 | |
| 737 | char *buf = malloc((size_t)size + CLI_SKIP_ONE); |
| 738 | if (!buf) { |
| 739 | (void)fclose(f); |
| 740 | return NULL; |
| 741 | } |
| 742 | |
| 743 | size_t nread = fread(buf, CLI_ELEM_SIZE, (size_t)size, f); |
| 744 | (void)fclose(f); |
| 745 | if (nread > (size_t)size) { |
| 746 | nread = (size_t)size; |
| 747 | } |
| 748 | buf[nread] = '\0'; |
| 749 | |
| 750 | /* Allow JSONC (comments + trailing commas) — Zed settings.json uses this format */ |
| 751 | yyjson_read_flag flags = YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS; |
| 752 | yyjson_doc *doc = yyjson_read(buf, nread, flags); |
| 753 | free(buf); |
| 754 | return doc; |
| 755 | } |
| 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) { |
no outgoing calls
no test coverage detected