Reviewed fread() usage around the code

This commit is contained in:
raysan5 2017-02-11 23:17:56 +01:00
parent b4988777ef
commit afcd748fdf
4 changed files with 20 additions and 15 deletions

View File

@ -1114,7 +1114,7 @@ static Wave LoadWAV(const char *fileName)
wave.data = malloc(wavData.subChunkSize);
// Read in the sound data into the soundData variable
fread(wave.data, 1, wavData.subChunkSize, wavFile);
fread(wave.data, wavData.subChunkSize, 1, wavFile);
// Store wave parameters
wave.sampleRate = wavFormat.sampleRate;

View File

@ -1025,14 +1025,14 @@ int StorageLoadValue(int position)
{
// Get file size
fseek(storageFile, 0, SEEK_END);
int fileSize = ftell(storageFile); // Size in bytes
int fileSize = ftell(storageFile); // Size in bytes
rewind(storageFile);
if (fileSize < (position*4)) TraceLog(WARNING, "Storage position could not be found");
else
{
fseek(storageFile, (position*4), SEEK_SET);
fread(&value, 1, 4, storageFile);
fread(&value, 4, 1, storageFile); // Read 1 element of 4 bytes size
}
fclose(storageFile);

View File

@ -928,6 +928,8 @@ static SpriteFont LoadBMFont(const char *fileName)
// TODO: Review texture packing method and generation (use oversampling)
static SpriteFont LoadTTF(const char *fileName, int fontSize, int charsCount, int *fontChars)
{
#define MAX_TTF_SIZE 16 // Maximum ttf file size in MB
// NOTE: Font texture size is predicted (being as much conservative as possible)
// Predictive method consist of supposing same number of chars by line-column (sqrtf)
// and a maximum character width of 3/4 of fontSize... it worked ok with all my tests...
@ -938,7 +940,7 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize, int charsCount, in
TraceLog(INFO, "TTF spritefont loading: Predicted texture size: %ix%i", textureSize, textureSize);
unsigned char *ttfBuffer = (unsigned char *)malloc(1 << 25);
unsigned char *ttfBuffer = (unsigned char *)malloc(MAX_TTF_SIZE*1024*1024);
unsigned char *dataBitmap = (unsigned char *)malloc(textureSize*textureSize*sizeof(unsigned char)); // One channel bitmap returned!
stbtt_bakedchar *charData = (stbtt_bakedchar *)malloc(sizeof(stbtt_bakedchar)*charsCount);
@ -952,7 +954,8 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize, int charsCount, in
return font;
}
fread(ttfBuffer, 1, 1 << 25, ttfFile);
// NOTE: We try reading up to 16 MB of elements of 1 byte
fread(ttfBuffer, 1, MAX_TTF_SIZE*1024*1024, ttfFile);
if (fontChars[0] != 32) TraceLog(WARNING, "TTF spritefont loading: first character is not SPACE(32) character");

View File

@ -238,7 +238,9 @@ Image LoadImageRaw(const char *fileName, int width, int height, int format, int
default: TraceLog(WARNING, "Image format not suported"); break;
}
int bytes = fread(image.data, size, 1, rawFile);
// NOTE: fread() returns num read elements instead of bytes,
// to get bytes we need to read (1 byte size, elements) instead of (x byte size, 1 element)
int bytes = fread(image.data, 1, size, rawFile);
// Check if data has been read successfully
if (bytes < size)
@ -1591,7 +1593,7 @@ static Image LoadDDS(const char *fileName)
// Verify the type of file
char filecode[4];
fread(filecode, 1, 4, ddsFile);
fread(filecode, 4, 1, ddsFile);
if (strncmp(filecode, "DDS ", 4) != 0)
{
@ -1690,17 +1692,17 @@ static Image LoadDDS(const char *fileName)
}
else if (((ddsHeader.ddspf.flags == 0x04) || (ddsHeader.ddspf.flags == 0x05)) && (ddsHeader.ddspf.fourCC > 0)) // Compressed
{
int bufsize;
int size; // DDS image data size
// Calculate data size, including all mipmaps
if (ddsHeader.mipmapCount > 1) bufsize = ddsHeader.pitchOrLinearSize*2;
else bufsize = ddsHeader.pitchOrLinearSize;
if (ddsHeader.mipmapCount > 1) size = ddsHeader.pitchOrLinearSize*2;
else size = ddsHeader.pitchOrLinearSize;
TraceLog(DEBUG, "Pitch or linear size: %i", ddsHeader.pitchOrLinearSize);
image.data = (unsigned char*)malloc(bufsize*sizeof(unsigned char));
image.data = (unsigned char*)malloc(size*sizeof(unsigned char));
fread(image.data, 1, bufsize, ddsFile);
fread(image.data, size, 1, ddsFile);
image.mipmaps = ddsHeader.mipmapCount;
@ -1803,7 +1805,7 @@ static Image LoadPKM(const char *fileName)
image.data = (unsigned char*)malloc(size*sizeof(unsigned char));
fread(image.data, 1, size, pkmFile);
fread(image.data, size, 1, pkmFile);
if (pkmHeader.format == 0) image.format = COMPRESSED_ETC1_RGB;
else if (pkmHeader.format == 1) image.format = COMPRESSED_ETC2_RGB;
@ -1888,7 +1890,7 @@ static Image LoadKTX(const char *fileName)
if (ktxHeader.keyValueDataSize > 0)
{
for (int i = 0; i < ktxHeader.keyValueDataSize; i++) fread(&unused, 1, 1, ktxFile);
for (int i = 0; i < ktxHeader.keyValueDataSize; i++) fread(&unused, sizeof(unsigned char), 1, ktxFile);
}
int dataSize;
@ -1896,7 +1898,7 @@ static Image LoadKTX(const char *fileName)
image.data = (unsigned char*)malloc(dataSize*sizeof(unsigned char));
fread(image.data, 1, dataSize, ktxFile);
fread(image.data, dataSize, 1, ktxFile);
if (ktxHeader.glInternalFormat == 0x8D64) image.format = COMPRESSED_ETC1_RGB;
else if (ktxHeader.glInternalFormat == 0x9274) image.format = COMPRESSED_ETC2_RGB;