file loading: ditch SEEK_END strategy for telling stream len
This commit is contained in:
parent
d7a0b46006
commit
f0486a172e
26
src/raudio.c
26
src/raudio.c
|
|
@ -2425,24 +2425,26 @@ static unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead
|
||||||
|
|
||||||
if (file != NULL)
|
if (file != NULL)
|
||||||
{
|
{
|
||||||
// WARNING: On binary streams SEEK_END could not be found,
|
size_t bytesAllocated = 0;
|
||||||
// using fseek() and ftell() could not work in some (rare) cases
|
|
||||||
fseek(file, 0, SEEK_END);
|
|
||||||
int size = ftell(file);
|
|
||||||
fseek(file, 0, SEEK_SET);
|
|
||||||
|
|
||||||
if (size > 0)
|
while (!feof(file))
|
||||||
{
|
{
|
||||||
data = (unsigned char *)RL_MALLOC(size*sizeof(unsigned char));
|
// TODO: Should granuality of forward allocation/reading be assumed to be 32
|
||||||
|
// or user should be given the ability to set it in config?
|
||||||
|
if (bytesAllocated <= *bytesRead) data = (unsigned char *)RL_REALLOC(data, bytesAllocated += 32);
|
||||||
|
|
||||||
// NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
|
// NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
|
||||||
unsigned int count = (unsigned int)fread(data, sizeof(unsigned char), size, file);
|
*bytesRead += fread(&data[*bytesRead], sizeof(unsigned char), 32, file);
|
||||||
*bytesRead = count;
|
|
||||||
|
|
||||||
if (count != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded", fileName);
|
if (ferror(file))
|
||||||
else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
|
{
|
||||||
|
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
||||||
|
RL_FREE(data);
|
||||||
|
data = NULL;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
}
|
||||||
|
if (data != NULL) TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
26
src/utils.c
26
src/utils.c
|
|
@ -197,24 +197,26 @@ unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
|
||||||
|
|
||||||
if (file != NULL)
|
if (file != NULL)
|
||||||
{
|
{
|
||||||
// WARNING: On binary streams SEEK_END could not be found,
|
size_t bytesAllocated = 0;
|
||||||
// using fseek() and ftell() could not work in some (rare) cases
|
|
||||||
fseek(file, 0, SEEK_END);
|
|
||||||
int size = ftell(file);
|
|
||||||
fseek(file, 0, SEEK_SET);
|
|
||||||
|
|
||||||
if (size > 0)
|
while (!feof(file))
|
||||||
{
|
{
|
||||||
data = (unsigned char *)RL_MALLOC(size*sizeof(unsigned char));
|
// TODO: Should granuality of forward allocation/reading be assumed to be 32
|
||||||
|
// or user should be given the ability to set it in config?
|
||||||
|
if (bytesAllocated <= *bytesRead) data = (unsigned char *)RL_REALLOC(data, bytesAllocated += 32);
|
||||||
|
|
||||||
// NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
|
// NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
|
||||||
unsigned int count = (unsigned int)fread(data, sizeof(unsigned char), size, file);
|
*bytesRead += fread(&data[*bytesRead], sizeof(unsigned char), 32, file);
|
||||||
*bytesRead = count;
|
|
||||||
|
|
||||||
if (count != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded", fileName);
|
if (ferror(file))
|
||||||
else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
|
{
|
||||||
|
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
||||||
|
RL_FREE(data);
|
||||||
|
data = NULL;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
}
|
||||||
|
if (data != NULL) TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user