file loading: LoadFileText reimplementation
This commit is contained in:
parent
f0486a172e
commit
8a643c92bb
|
|
@ -2438,9 +2438,9 @@ static unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead
|
||||||
|
|
||||||
if (ferror(file))
|
if (ferror(file))
|
||||||
{
|
{
|
||||||
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
|
||||||
RL_FREE(data);
|
RL_FREE(data);
|
||||||
data = NULL;
|
data = NULL;
|
||||||
|
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
40
src/utils.c
40
src/utils.c
|
|
@ -210,9 +210,9 @@ unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
|
||||||
|
|
||||||
if (ferror(file))
|
if (ferror(file))
|
||||||
{
|
{
|
||||||
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
|
||||||
RL_FREE(data);
|
RL_FREE(data);
|
||||||
data = NULL;
|
data = NULL;
|
||||||
|
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -334,28 +334,32 @@ char *LoadFileText(const char *fileName)
|
||||||
|
|
||||||
if (file != NULL)
|
if (file != NULL)
|
||||||
{
|
{
|
||||||
// WARNING: When reading a file as 'text' file,
|
size_t charsRead = 0, charsAllocated = 0;
|
||||||
// text mode causes carriage return-linefeed translation...
|
|
||||||
// ...but using fseek() should return correct byte-offset
|
|
||||||
fseek(file, 0, SEEK_END);
|
|
||||||
unsigned int size = (unsigned int)ftell(file);
|
|
||||||
fseek(file, 0, SEEK_SET);
|
|
||||||
|
|
||||||
if (size > 0)
|
while (!feof(file))
|
||||||
{
|
{
|
||||||
text = (char *)RL_MALLOC((size + 1)*sizeof(char));
|
// TODO: Should granuality of forward allocation/reading be assumed to be 32
|
||||||
unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
|
// or user should be given the ability to set it in config?
|
||||||
|
if (charsAllocated <= charsRead) text = (char *)RL_REALLOC(text, charsAllocated += 32);
|
||||||
|
|
||||||
// WARNING: \r\n is converted to \n on reading, so,
|
// NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
|
||||||
// read bytes count gets reduced by the number of lines
|
// NOTE: Platform specific newlines are handled in implementation specific manner
|
||||||
if (count < size) text = RL_REALLOC(text, count + 1);
|
charsRead += fread(&text[charsRead], sizeof(char), 32, file);
|
||||||
|
|
||||||
// Zero-terminate the string
|
if (ferror(file))
|
||||||
text[count] = '\0';
|
{
|
||||||
|
RL_FREE(text);
|
||||||
TRACELOG(LOG_INFO, "FILEIO: [%s] Text file loaded successfully", fileName);
|
text = NULL;
|
||||||
|
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (text != NULL)
|
||||||
|
{
|
||||||
|
if (charsRead == charsAllocated) text = (char *)RL_REALLOC(text, charsAllocated += 1);
|
||||||
|
text[charsRead] = '\0';
|
||||||
|
TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
|
||||||
}
|
}
|
||||||
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read text file", fileName);
|
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user