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

Function read_file

src/pipeline/pass_semantic.c:40–71  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

38}
39
40static char *read_file(const char *path, int *out_len) {
41 *out_len = -1; /* unknown/read failure; zero is a proven empty file */
42 FILE *f = cbm_fopen(path, "rb");
43 if (!f) {
44 return NULL;
45 }
46 (void)fseek(f, 0, SEEK_END);
47 long size = ftell(f);
48 (void)fseek(f, 0, SEEK_SET);
49 if (size <= 0 || size > cbm_max_file_bytes()) { /* generous, env-configurable cap (B4) */
50 if (size == 0) {
51 *out_len = 0;
52 }
53 (void)fclose(f);
54 return NULL;
55 }
56 /* +pad: tree-sitter lexer lookahead reads past EOF; keep it in-bounds */
57 enum { CBM_TS_LOOKAHEAD_PAD = 16 };
58 char *buf = cbm_alloc(CBM_MEM_CLASS_SEMANTIC, (size_t)size + CBM_TS_LOOKAHEAD_PAD);
59 if (!buf) {
60 (void)fclose(f);
61 return NULL;
62 }
63 size_t nread = fread(buf, SKIP_ONE, size, f);
64 (void)fclose(f);
65 if (nread > (size_t)size) {
66 nread = (size_t)size;
67 }
68 memset(buf + nread, 0, CBM_TS_LOOKAHEAD_PAD);
69 *out_len = (int)nread;
70 return buf;
71}
72
73static const char *itoa_log(int val) {
74 enum { RING_BUF_COUNT = 4, RING_BUF_MASK = 3 };

Callers 1

sem_get_or_extractFunction · 0.70

Calls 4

cbm_fopenFunction · 0.85
cbm_max_file_bytesFunction · 0.85
cbm_allocFunction · 0.85
freadFunction · 0.85

Tested by

no test coverage detected