MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / config_read_file

Function config_read_file

src/ui/config.c:46–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

44/* ── Load ────────────────────────────────────────────────────── */
45
46static char *config_read_file(const char *path, size_t *length_out, bool *opened_out) {
47 if (length_out) {
48 *length_out = 0;
49 }
50 if (opened_out) {
51 *opened_out = false;
52 }
53 if (!path || !length_out || !opened_out) {
54 return NULL;
55 }
56#ifdef _WIN32
57 wchar_t *wide_path = cbm_path_to_wide(path);
58 if (!wide_path) {
59 return NULL;
60 }
61 HANDLE file = CreateFileW(wide_path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL,
62 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
63 free(wide_path);
64 if (file == INVALID_HANDLE_VALUE) {
65 return NULL;
66 }
67 *opened_out = true;
68 LARGE_INTEGER size;
69 if (!GetFileSizeEx(file, &size) || size.QuadPart <= 0 || size.QuadPart > 4096) {
70 (void)CloseHandle(file);
71 return NULL;
72 }
73 size_t length = (size_t)size.QuadPart;
74 char *buffer = malloc(length + 1U);
75 DWORD read_length = 0;
76 bool read_ok = buffer && ReadFile(file, buffer, (DWORD)length, &read_length, NULL) &&
77 read_length == (DWORD)length;
78 (void)CloseHandle(file);
79 if (!read_ok) {
80 free(buffer);
81 return NULL;
82 }
83#else
84 FILE *file = cbm_fopen(path, "rb");
85 if (!file) {
86 return NULL;
87 }
88 *opened_out = true;
89 if (fseek(file, 0, SEEK_END) != 0) {
90 (void)fclose(file);
91 return NULL;
92 }
93 long file_length = ftell(file);
94 if (file_length <= 0 || file_length > 4096 || fseek(file, 0, SEEK_SET) != 0) {
95 (void)fclose(file);
96 return NULL;
97 }
98 size_t length = (size_t)file_length;
99 char *buffer = malloc(length + 1U);
100 bool read_ok = buffer && fread(buffer, 1, length, file) == length;
101 (void)fclose(file);
102 if (!read_ok) {
103 free(buffer);

Callers 1

Calls 3

cbm_path_to_wideFunction · 0.85
cbm_fopenFunction · 0.85
freadFunction · 0.85

Tested by

no test coverage detected