MCPcopy Create free account
hub / github.com/WolfireGames/overgrowth / StackLoadText

Function StackLoadText

Source/XML/xml_helper.cpp:176–213  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

174}
175
176uint8_t *StackLoadText(const char *path, size_t *size_out) {
177 uint8_t *mem = NULL;
178 long file_size = 0;
179 char abs_path[kPathSize];
180 bool retry = true;
181 if (FindFilePath(path, abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) != -1) {
182 FILE *file = my_fopen(abs_path, "rb");
183 if (file) {
184 fseek(file, 0, SEEK_END);
185 file_size = ftell(file);
186 if (file_size > 0) {
187 rewind(file);
188
189 LOG_ASSERT(file_size < (1024 * 1024));
190
191 mem = (uint8_t *)alloc.stack.Alloc(file_size + 1);
192 if (mem) {
193 size_t count = fread(mem, 1, file_size, file);
194 if ((long)count == file_size) {
195 mem[file_size] = '\0';
196 } else {
197 LOGE << "Did not read expected amount of data from file: " << abs_path << std::endl;
198 alloc.stack.Free(mem);
199 mem = NULL;
200 }
201 } else {
202 LOGF << "Could not allocate " << file_size + 1 << " bytes on stack for file " << abs_path << std::endl;
203 }
204 }
205 fclose(file);
206 }
207 }
208
209 if (size_out) {
210 *size_out = file_size;
211 }
212 return mem;
213}
214
215bool LoadText(void *&mem, const char *path) {
216 FILE *file = my_fopen(path, "rb");

Callers 1

LoadMethod · 0.85

Calls 4

FindFilePathFunction · 0.85
my_fopenFunction · 0.85
AllocMethod · 0.45
FreeMethod · 0.45

Tested by

no test coverage detected