diff options
author | Bertram <yohanndotferreiraatorange.fr> | 2010-04-25 21:59:29 +0200 |
---|---|---|
committer | Bertram <yohanndotferreiraatorange.fr> | 2010-04-25 21:59:29 +0200 |
commit | ad8033f54c25e6ac1ab1a1e505cce326da870f40 (patch) | |
tree | 9ecc2c1ccff8ebb8a9cb5a71ed7eba96ebca9af1 /src/game-server/resourcemanager.cpp | |
parent | 4d1cfb2379f6222b3cdceba90480121d39536081 (diff) | |
download | manaserv-ad8033f54c25e6ac1ab1a1e505cce326da870f40.tar.gz manaserv-ad8033f54c25e6ac1ab1a1e505cce326da870f40.tar.bz2 manaserv-ad8033f54c25e6ac1ab1a1e505cce326da870f40.tar.xz manaserv-ad8033f54c25e6ac1ab1a1e505cce326da870f40.zip |
Added a Byte Order Mask (BOM) filter to the ResourceManager.
Reviewed by: Jaxad0127
Diffstat (limited to 'src/game-server/resourcemanager.cpp')
-rw-r--r-- | src/game-server/resourcemanager.cpp | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/game-server/resourcemanager.cpp b/src/game-server/resourcemanager.cpp index 328680e1..06958e7e 100644 --- a/src/game-server/resourcemanager.cpp +++ b/src/game-server/resourcemanager.cpp @@ -115,7 +115,8 @@ bool ResourceManager::exists(const std::string &path) return PHYSFS_exists(path.c_str()); } -char *ResourceManager::loadFile(const std::string &fileName, int &fileSize) +char *ResourceManager::loadFile(const std::string &fileName, int &fileSize, + bool removeBOM) { // Attempt to open the specified file using PhysicsFS PHYSFS_file* file = PHYSFS_openRead(fileName.c_str()); @@ -131,6 +132,30 @@ char *ResourceManager::loadFile(const std::string &fileName, int &fileSize) // Get the size of the file fileSize = PHYSFS_fileLength(file); + if (removeBOM) + { + // Inspired by BOMstrip from: + // Peter Pentchev, 2008, public domain. + const std::string utf8Bom = "\xef\xbb\xbf"; + char bomBuffer[utf8Bom.length()]; + PHYSFS_read(file, bomBuffer, 1, utf8Bom.length()); + + std::istringstream iss(std::string(bomBuffer, utf8Bom.length())); + std::string line; + + // if we find a BOM, then we remove it from the buffer + if (std::getline(iss, line) && !line.substr(0, 3).compare(utf8Bom)) + { + LOG_INFO("Found a Byte Order Mask (BOM) in '" << fileName); + fileSize = fileSize - utf8Bom.length(); + } + else + { + // No BOM, we get back to the file start. + PHYSFS_seek(file, 0); + } + } + // Allocate memory and load the file char *buffer = (char *)malloc(fileSize + 1); if (PHYSFS_read(file, buffer, 1, fileSize) != fileSize) @@ -144,7 +169,7 @@ char *ResourceManager::loadFile(const std::string &fileName, int &fileSize) // Close the file and let the user deallocate the memory PHYSFS_close(file); - // Add a trailing nul character, so that the file can be used as a string + // Add a trailing null character, so that the file can be used as a string buffer[fileSize] = 0; return buffer; } |