MCPcopy Create free account
hub / github.com/captain-amygdala/pistorm / LoadFileText

Function LoadFileText

raylib_pi4_test/utils.c:273–322  ·  view source on GitHub ↗

Load text data from file, returns a '\0' terminated string NOTE: text chars array should be freed manually

Source from the content-addressed store, hash-verified

271// Load text data from file, returns a '\0' terminated string
272// NOTE: text chars array should be freed manually
273char *LoadFileText(const char *fileName)
274{
275 char *text = NULL;
276
277 if (fileName != NULL)
278 {
279 if (loadFileText)
280 {
281 text = loadFileText(fileName);
282 return text;
283 }
284#if defined(SUPPORT_STANDARD_FILEIO)
285 FILE *file = fopen(fileName, "rt");
286
287 if (file != NULL)
288 {
289 // WARNING: When reading a file as 'text' file,
290 // text mode causes carriage return-linefeed translation...
291 // ...but using fseek() should return correct byte-offset
292 fseek(file, 0, SEEK_END);
293 unsigned int size = (unsigned int)ftell(file);
294 fseek(file, 0, SEEK_SET);
295
296 if (size > 0)
297 {
298 text = (char *)RL_MALLOC((size + 1)*sizeof(char));
299 unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
300
301 // WARNING: \r\n is converted to \n on reading, so,
302 // read bytes count gets reduced by the number of lines
303 if (count < size) text = RL_REALLOC(text, count + 1);
304
305 // Zero-terminate the string
306 text[count] = '\0';
307
308 TRACELOG(LOG_INFO, "FILEIO: [%s] Text file loaded successfully", fileName);
309 }
310 else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read text file", fileName);
311
312 fclose(file);
313 }
314 else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
315#else
316 TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
317#endif
318 }
319 else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
320
321 return text;
322}
323
324// Unload file text data allocated by LoadFileText()
325void UnloadFileText(char *text)

Callers 3

LoadOBJFunction · 0.70
LoadBMFontFunction · 0.70
LoadShaderFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected