Clean up
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include "ConfigFile.h"
|
||||
#include "../Utilities/Operations.h"
|
||||
|
||||
namespace TLAC::FileSystem
|
||||
{
|
||||
ConfigFile::ConfigFile(const std::string &path) : TextFile(path)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ConfigFile::ConfigFile(const std::string &directory, const std::string &file) : TextFile(directory, file)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool ConfigFile::TryGetValue(const std::string &key, std::string **value)
|
||||
{
|
||||
auto pair = ConfigMap.find(key);
|
||||
bool found = pair != ConfigMap.end();
|
||||
|
||||
*value = found ? new std::string(pair->second) : nullptr;
|
||||
return found;
|
||||
}
|
||||
|
||||
int ConfigFile::GetIntegerValue(const std::string& key)
|
||||
{
|
||||
auto pair = ConfigMap.find(key);
|
||||
bool found = pair != ConfigMap.end();
|
||||
|
||||
return found ? atoi(pair->second.c_str()) : 0;
|
||||
}
|
||||
|
||||
bool ConfigFile::GetBooleanValue(const std::string& key)
|
||||
{
|
||||
auto pair = ConfigMap.find(key);
|
||||
bool found = pair != ConfigMap.end();
|
||||
|
||||
return found ? pair->second == "true" : false;
|
||||
}
|
||||
|
||||
float ConfigFile::GetFloatValue(const std::string & key)
|
||||
{
|
||||
auto pair = ConfigMap.find(key);
|
||||
bool found = pair != ConfigMap.end();
|
||||
|
||||
return found ? (float)atof(pair->second.c_str()) : 0.0f;
|
||||
}
|
||||
|
||||
void ConfigFile::Parse(std::ifstream &fileStream)
|
||||
{
|
||||
std::string line;
|
||||
|
||||
while (std::getline(fileStream, line))
|
||||
{
|
||||
if (IsComment(line))
|
||||
continue;
|
||||
|
||||
auto splitline = Utilities::Split(line, "=");
|
||||
|
||||
for (auto &line : splitline)
|
||||
Utilities::Trim(line);
|
||||
|
||||
ConfigMap.insert(std::make_pair(splitline[0], splitline[1]));
|
||||
}
|
||||
}
|
||||
|
||||
bool ConfigFile::IsComment(const std::string &line)
|
||||
{
|
||||
return line.size() <= 0 || line[0] == '#' || line[0] == '[' || line._Starts_with("//");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "TextFile.h"
|
||||
#include <unordered_map>
|
||||
|
||||
namespace TLAC::FileSystem
|
||||
{
|
||||
class ConfigFile : public TextFile
|
||||
{
|
||||
public:
|
||||
ConfigFile(const std::string &path);
|
||||
ConfigFile(const std::string &directory, const std::string &file);
|
||||
|
||||
std::unordered_map<std::string, std::string> ConfigMap;
|
||||
|
||||
bool TryGetValue(const std::string &key, std::string **value);
|
||||
int GetIntegerValue(const std::string& key);
|
||||
bool GetBooleanValue(const std::string& key);
|
||||
float GetFloatValue(const std::string& key);
|
||||
|
||||
protected:
|
||||
virtual void Parse(std::ifstream &fileStream) override;
|
||||
|
||||
private:
|
||||
bool IsComment(const std::string &line);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "TextFile.h"
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace TLAC::FileSystem
|
||||
{
|
||||
TextFile::TextFile(const std::string &path)
|
||||
{
|
||||
FileName = path;
|
||||
}
|
||||
|
||||
TextFile::TextFile(const std::string &directory, const std::string &file)
|
||||
{
|
||||
auto fullPath = directory + "/" + file;
|
||||
FileName = fullPath;
|
||||
}
|
||||
|
||||
TextFile::~TextFile()
|
||||
{
|
||||
}
|
||||
|
||||
bool TextFile::OpenRead()
|
||||
{
|
||||
fs::path configPath = fs::u8path(FileName);
|
||||
|
||||
if (!fs::exists(configPath))
|
||||
return false;
|
||||
|
||||
std::ifstream fileStream(configPath.wstring().c_str());
|
||||
|
||||
if (!fileStream.is_open())
|
||||
return false;
|
||||
|
||||
Parse(fileStream);
|
||||
|
||||
fileStream.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
namespace TLAC::FileSystem
|
||||
{
|
||||
class TextFile
|
||||
{
|
||||
public:
|
||||
std::string FileName;
|
||||
|
||||
TextFile(const std::string &path);
|
||||
TextFile(const std::string &directory, const std::string &file);
|
||||
~TextFile();
|
||||
|
||||
bool OpenRead();
|
||||
|
||||
protected:
|
||||
virtual void Parse(std::ifstream &fileStream) = 0;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user