From 13519fa0193ca2ff2ac36717adce3342424364b6 Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Sun, 26 Aug 2012 20:53:55 -0400 Subject: [PATCH] Added code to load the contents of a file to memory on not-windows. --- IO/ImageFile.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/IO/ImageFile.cpp b/IO/ImageFile.cpp index 19d9e75..58324c7 100644 --- a/IO/ImageFile.cpp +++ b/IO/ImageFile.cpp @@ -1,4 +1,8 @@ #include +#include +#include +#include + #include "ImageFile.h" #ifdef PNG_FOUND @@ -10,8 +14,10 @@ ImageFile::ImageFile(const char *filename) : m_FileFormat( DetectFileFormat(filename) ) { unsigned char *rawData = ReadFileData(filename); - LoadImage(rawData); - delete [] rawData; + if(rawData) { + LoadImage(rawData); + delete [] rawData; + } } ImageFile::ImageFile(const char *filename, EImageFileFormat format) : @@ -19,8 +25,10 @@ ImageFile::ImageFile(const char *filename, EImageFileFormat format) : m_PixelData(0) { unsigned char *rawData = ReadFileData(filename); - LoadImage(rawData); - delete [] rawData; + if(rawData) { + LoadImage(rawData); + delete [] rawData; + } } ImageFile::~ImageFile() { @@ -60,10 +68,41 @@ bool ImageFile::LoadImage(const unsigned char *rawImageData) { #ifdef _MSC_VER unsigned char *ImageFile::ReadFileData(const char *filename) { - + //!FIXME! - Actually, implement me + assert(!"Not implemented!"); } #else unsigned char *ImageFile::ReadFileData(const char *filename) { + FILE *fp = fopen(filename, "rb"); + if(!fp) { + fprintf(stderr, "Error opening file for reading: %s\n", filename); + return 0; + } + // Check filesize + long fileSize = 0; + fseek(fp, 0, SEEK_END); + fileSize = ftell(fp); + + // Allocate data for file contents + unsigned char *rawData = new unsigned char[fileSize]; + + // Return stream to beginning of file + fseek(fp, 0, SEEK_SET); + assert(ftell(fp) == 0); + + // Read all of the data + size_t bytesRead = fread(rawData, 1, fileSize, fp); + if(bytesRead != fileSize) { + assert(!"We didn't read as much data as we thought we had!"); + fprintf(stderr, "Internal error: Incorrect file size assumption\n"); + return 0; + } + + // Close the file pointer + fclose(fp); + + // Return the data.. + return rawData; } #endif