mirror of
https://github.com/jakcron/nstool
synced 2024-12-24 21:51:14 +00:00
[fnd] Add ResourceFileReader. This reads simple <key>=<value> files.
This commit is contained in:
parent
88bcc8ce6d
commit
bfb9d87c95
2 changed files with 86 additions and 0 deletions
22
lib/libfnd/include/fnd/ResourceFileReader.h
Normal file
22
lib/libfnd/include/fnd/ResourceFileReader.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
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<std::string,std::string> mResources;
|
||||
};
|
||||
}
|
64
lib/libfnd/source/ResourceFileReader.cpp
Normal file
64
lib/libfnd/source/ResourceFileReader.cpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
#include <fnd/types.h>
|
||||
#include <fnd/ResourceFileReader.h>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
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];
|
||||
}
|
Loading…
Reference in a new issue