From 9cea91975451a7abb6f15e08f7fb6c20efed152c Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Fri, 15 Jul 2005 23:43:22 +0000 Subject: Fixed compiler warnings and added plain text file loading to resource manager. --- src/resources/resourcemanager.cpp | 29 +++++++++++++++++++++++++++++ src/resources/resourcemanager.h | 7 +++++++ 2 files changed, 36 insertions(+) (limited to 'src/resources') diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 50d0f4b8..6ed933e4 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -281,3 +281,32 @@ ResourceManager::loadFile(const std::string &fileName, int &fileSize) return buffer; } + +std::vector +ResourceManager::loadTextFile(const std::string &fileName) +{ + int contentsLength; + char *fileContents = (char*)loadFile(fileName, contentsLength); + std::vector lines; + + if (!fileContents) + { + logger->log("Couldn't load text file: %s", fileName.c_str()); + return lines; + } + + // Reallocate and include terminating 0 character + fileContents = (char*)realloc(fileContents, contentsLength + 1); + fileContents[contentsLength] = '\0'; + + // Tokenize and add each line separately + char *line = strtok(fileContents, "\n"); + while (line != NULL) + { + lines.push_back(line); + line = strtok(NULL, "\n"); + } + + free(fileContents); + return lines; +} diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index e3ad1e94..93c2dd85 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -26,6 +26,7 @@ #include #include +#include #include "resource.h" #include "image.h" #include "music.h" @@ -113,6 +114,12 @@ class ResourceManager void* loadFile(const std::string &fileName, int &fileSize); + /** + * Retrieves the contents of a text file. + */ + std::vector + loadTextFile(const std::string &fileName); + /** * Returns an instance of the class, creating one if it does not * already exist. -- cgit v1.2.3-70-g09d2