diff --git a/source/code/sago/SagoDataHolder.cpp b/source/code/sago/SagoDataHolder.cpp index abe3c4b..9fc5a5f 100644 --- a/source/code/sago/SagoDataHolder.cpp +++ b/source/code/sago/SagoDataHolder.cpp @@ -31,6 +31,7 @@ SOFTWARE. #include #include #include "SagoMiscSdl2.hpp" +#include "SagoMisc.hpp" #if PHYSFS_VER_MAJOR < 3 #define PHYSFS_readBytes(X,Y,Z) PHYSFS_read(X,Y,1,Z) @@ -115,20 +116,12 @@ SDL_Texture* SagoDataHolder::getTexturePtr(const std::string& textureName) const if (!PHYSFS_exists(path.c_str())) { sago::SagoFatalErrorF("getTextureFailed - Texture does not exist: %s", path.c_str()); } - PHYSFS_file* myfile = PHYSFS_openRead(path.c_str()); - unsigned int m_size = PHYSFS_fileLength(myfile); - std::unique_ptr m_data(new char[m_size]); - int length_read = PHYSFS_readBytes (myfile, m_data.get(), m_size); - if (length_read != (int)m_size) { - PHYSFS_close(myfile); - std::cerr << "Error: Corrupt data file: " << path << "\n"; - return ret; - } - PHYSFS_close(myfile); + unsigned int m_size = 0; + std::unique_ptr m_data; + ReadBytesFromFile(path.c_str(), m_data, m_size); SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size); //The above might fail an return null. if (!rw) { - PHYSFS_close(myfile); std::cerr << "Error. Corrupt data file!\n"; return NULL; } @@ -157,22 +150,14 @@ TTF_Font* SagoDataHolder::getFontPtr(const std::string& fontName, int ptsize) co std::cerr << "getFontPtr - Font does not exists: " << path << "\n"; return ret; } - PHYSFS_file* myfile = PHYSFS_openRead(path.c_str()); - unsigned int m_size = PHYSFS_fileLength(myfile); - std::unique_ptr m_data(new char[m_size]); - int length_read = PHYSFS_readBytes (myfile, m_data.get(), m_size); - if (length_read != (int)m_size) { - PHYSFS_close(myfile); - std::cerr << "Error: Corrupt data file: " << path << "\n"; - return ret; - } - PHYSFS_close(myfile); + unsigned int m_size = 0; + std::unique_ptr m_data; + ReadBytesFromFile(path.c_str(), m_data, m_size); SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size); //The above might fail an return null. if (!rw) { - PHYSFS_close(myfile); std::cerr << "Error: Corrupt data file!\n"; return ret; } @@ -200,21 +185,13 @@ Mix_Music* SagoDataHolder::getMusicPtr(const std::string& musicName) const { std::cerr << "getMusicPtr - Music file does not exists: " << path << "\n"; return ret; } - PHYSFS_file* myfile = PHYSFS_openRead(path.c_str()); - unsigned int m_size = PHYSFS_fileLength(myfile); - std::unique_ptr m_data(new char[m_size]); - int length_read = PHYSFS_readBytes (myfile, m_data.get(), m_size); - if (length_read != (int)m_size) { - PHYSFS_close(myfile); - std::cerr << "Error: Corrupt data file: " << path << "\n"; - return ret; - } - PHYSFS_close(myfile); + unsigned int m_size = 0; + std::unique_ptr m_data; + ReadBytesFromFile(path.c_str(), m_data, m_size); SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size); //The above might fail an return null. if (!rw) { - PHYSFS_close(myfile); std::cerr << "Error. Corrupt data file!\n"; return NULL; } @@ -243,21 +220,13 @@ Mix_Chunk* SagoDataHolder::getSoundPtr(const std::string& soundName) const { std::cerr << "getSoundPtr - Sound file does not exists: " << path << "\n"; return ret; } - PHYSFS_file* myfile = PHYSFS_openRead(path.c_str()); - unsigned int m_size = PHYSFS_fileLength(myfile); - std::unique_ptr m_data(new char[m_size]); - int length_read = PHYSFS_readBytes (myfile, m_data.get(), m_size); - if (length_read != (int)m_size) { - PHYSFS_close(myfile); - std::cerr << "Error: Corrupt data file: " << path << "\n"; - return ret; - } - PHYSFS_close(myfile); + unsigned int m_size = 0; + std::unique_ptr m_data; + ReadBytesFromFile(path.c_str(), m_data, m_size); SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size); //The above might fail an return null. if (!rw) { - PHYSFS_close(myfile); std::cerr << "Error. Corrupt data file!\n"; return NULL; } diff --git a/source/code/sago/SagoMisc.cpp b/source/code/sago/SagoMisc.cpp index 536234e..6942cd1 100644 --- a/source/code/sago/SagoMisc.cpp +++ b/source/code/sago/SagoMisc.cpp @@ -27,7 +27,6 @@ SOFTWARE. #include #include #include -#include #if PHYSFS_VER_MAJOR < 3 #define PHYSFS_readBytes(X,Y,Z) PHYSFS_read(X,Y,1,Z) @@ -55,11 +54,11 @@ bool FileExists(const char* filename) { return PHYSFS_exists(filename); } -std::string GetFileContent(const char* filename) { - string ret; +void ReadBytesFromFile(const char* filename, std::unique_ptr& dest, unsigned int& bytes) { + bytes = 0; if (!PHYSFS_exists(filename)) { - cerr << "GetFileContent - File does not exists: " << filename << "\n"; - return ret; + cerr << "ReadBytesFromFile - File does not exists: " << filename << "\n"; + return; } PHYSFS_file* myfile = PHYSFS_openRead(filename); unsigned int m_size = PHYSFS_fileLength(myfile); @@ -68,10 +67,23 @@ std::string GetFileContent(const char* filename) { if (length_read != (int)m_size) { PHYSFS_close(myfile); cerr << "Error: Curropt data file: " << filename << "\n"; - return ret; + return; } PHYSFS_close(myfile); - //Now create a std::string + std::swap(m_data, dest); + bytes = m_size; +} + +std::string GetFileContent(const char* filename) { + string ret; + if (!PHYSFS_exists(filename)) { + cerr << "GetFileContent - File does not exists: " << filename << "\n"; + return ret; + } + unsigned int m_size = 0; + std::unique_ptr m_data; + ReadBytesFromFile(filename, m_data, m_size); + //Now create a std::string ret = string(m_data.get(), m_data.get()+m_size); return ret; } diff --git a/source/code/sago/SagoMisc.hpp b/source/code/sago/SagoMisc.hpp index 4c3db91..347bc75 100644 --- a/source/code/sago/SagoMisc.hpp +++ b/source/code/sago/SagoMisc.hpp @@ -27,6 +27,7 @@ SOFTWARE. #include #include +#include namespace sago { @@ -42,6 +43,16 @@ namespace sago { * Reads an entire file into memory. * PHYSFS must be setup before hand * @param filename The file to read + * @param dest The unique pointer in which the bytes will be written + * @param bytes Number of bytes written + * @return The content of the file. If empty either the file was empty, did not exist or could not be opened + */ + void ReadBytesFromFile(const char* filename, std::unique_ptr& dest, unsigned int& bytes); + + /** + * Reads an entire file into memory. + * PHYSFS must be setup before hand + * @param filename The file to read * @return The content of the file. If empty either the file was empty, did not exist or could not be opened */ std::string GetFileContent(const char* filename);