From bfb9d87c95f622c6a21fb3e1f86e2b42fd90675f Mon Sep 17 00:00:00 2001 From: jakcron Date: Sun, 15 Apr 2018 10:38:58 +0800 Subject: [PATCH] [fnd] Add ResourceFileReader. This reads simple = files. --- lib/libfnd/include/fnd/ResourceFileReader.h | 22 +++++++ lib/libfnd/source/ResourceFileReader.cpp | 64 +++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 lib/libfnd/include/fnd/ResourceFileReader.h create mode 100644 lib/libfnd/source/ResourceFileReader.cpp diff --git a/lib/libfnd/include/fnd/ResourceFileReader.h b/lib/libfnd/include/fnd/ResourceFileReader.h new file mode 100644 index 0000000..ed9eb05 --- /dev/null +++ b/lib/libfnd/include/fnd/ResourceFileReader.h @@ -0,0 +1,22 @@ +#pragma once +#include +#include + +namespace fnd +{ + class ResourceFileReader + { + public: + ResourceFileReader(); + + void processFile(const std::string& path); + + bool doesExist(const std::string& key) const; + const std::string& operator[](const std::string& key); + + private: + const std::string kModuleName = "ResourceFileReader"; + + std::map mResources; + }; +} diff --git a/lib/libfnd/source/ResourceFileReader.cpp b/lib/libfnd/source/ResourceFileReader.cpp new file mode 100644 index 0000000..956dde6 --- /dev/null +++ b/lib/libfnd/source/ResourceFileReader.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include + +fnd::ResourceFileReader::ResourceFileReader() +{ +} + +inline bool isNotPrintable(char chr) { return isprint(chr) == false; } + +void fnd::ResourceFileReader::processFile(const std::string& path) +{ + std::ifstream file; + file.open(path, std::ios::in); + if (file.is_open() == false) + { + throw fnd::Exception(kModuleName, "Failed to open file"); + } + + std::string line, key, value; + while (std::getline(file, line)) + { + // read up to comment line + if (line.find(";") != std::string::npos) + line = line.substr(0, line.find(";")); + + // change chars to lower string + std::transform(line.begin(), line.end(), line.begin(), ::tolower); + + // strip whitespace + line.erase(std::remove(line.begin(), line.end(), ' '), line.end()); + + // strip nonprintable + line.erase(std::remove_if(line.begin(), line.end(), isNotPrintable), line.end()); + + // skip lines that don't have '=' + if (line.find("=") == std::string::npos) + continue; + + key = line.substr(0,line.find("=")); + value = line.substr(line.find("=")+1); + + // skip if key or value is empty + if (key.empty() || value.empty()) + continue; + + //std::cout << "[" + key + "]=(" + value + ")" << std::endl; + + mResources[key] = value; + } + file.close(); +} + +bool fnd::ResourceFileReader::doesExist(const std::string& key) const +{ + return (mResources.find(key) != mResources.end()); +} + +const std::string& fnd::ResourceFileReader::operator[](const std::string& key) +{ + return mResources[key]; +} \ No newline at end of file