From 356e238fa4c1461f6fe69b626253c49ebed989db Mon Sep 17 00:00:00 2001 From: Alfred Burgess <aburgess@ucc.gu.uwa.edu.au> Date: Fri, 8 Dec 2023 14:54:35 +0800 Subject: [PATCH] Config now reads file on start-up and has a method to output to standard IO stream --- include/config-parser.h | 6 ++++++ src/config-parser.cpp | 25 ++++++++++++++++++++----- src/main.cpp | 3 ++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/include/config-parser.h b/include/config-parser.h index 41bef02..3f0b9d9 100644 --- a/include/config-parser.h +++ b/include/config-parser.h @@ -13,6 +13,8 @@ typedef std::map<std::string,std::string> ConfigMap; class ConfigParser { + friend std::ostream& operator<<( std::ostream& os, const ConfigParser& obj ); + public: static ConfigParser UserSettings(); @@ -20,9 +22,13 @@ class ConfigParser ConfigMap userSettings(){ return this->readFile( "settings.cfg" ); } + void set( std::string key, std::string value ); + std::string get( std::string key ); + std::string toString() const; private: + std::string _path; ConfigParser( std::string path ); const std::string CONFIG_DIRECTORY = "config"; const char DELIMINATOR = ':'; diff --git a/src/config-parser.cpp b/src/config-parser.cpp index 58df3c1..e6266e0 100644 --- a/src/config-parser.cpp +++ b/src/config-parser.cpp @@ -6,19 +6,27 @@ ConfigParser ConfigParser::UserSettings() return parser; } +void ConfigParser::set( std::string key, std::string value ) + { this->_config[key] = value; } + +std::string ConfigParser::get( std::string key ) + { return this->_config[key]; } std::string ConfigParser::toString() const { - std::string str = ""; + std::string str = "Config File: "+this->_path+"\n"; for( const auto [key, value] : this->_config ) { str += "["+key+"] "+value+"\n"; } return str; } -std::ostream& operator<<( std::ostream& os, const ConfigParser& obj ) { return os << obj.toString(); } + ConfigParser::ConfigParser( std::string path ) - : _logger( Logger::instance() ) -{ this->_logger.info( "Creating config parser" ); } + : _path(path), _logger( Logger::instance() ) +{ + this->_logger.info( "Creating config parser" ); + this->readFile( path ); +} ConfigParser::~ConfigParser() { this->_logger.info( "Closing config parser" ); } @@ -46,7 +54,9 @@ ConfigMap ConfigParser::readFile( std::string path ) std::string key = this->getKey( line ); std::string value = this->getKey( line ); if( ! key.empty() && ! value.empty() ) - { confMap.insert( keyPair(key, value) ); } + { confMap[key] = value; } + std::cout << key << "," << value << std::endl; + // { confMap.insert( keyPair(key, value) ); } } } } else @@ -82,3 +92,8 @@ std::string ConfigParser::getValue( std::string line ) } return value; } + +std::ostream& operator<<( std::ostream& os, const ConfigParser& obj ) +{ + return os << obj.toString(); +} diff --git a/src/main.cpp b/src/main.cpp index 36a585e..6f24692 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,7 +60,8 @@ int main() { _frame++; } - // std::cout << userSettings; + userSettings.set( "exit", "teset" ); + std::cout << userSettings; // Clean-up while( entities.size() > 0 ) -- GitLab