diff --git a/config/settings.cfg b/config/settings.cfg new file mode 100644 index 0000000000000000000000000000000000000000..82f24127d8e2d3e78be977a2bd1c25070a13770c --- /dev/null +++ b/config/settings.cfg @@ -0,0 +1,2 @@ +test1: test +test2: tedfh diff --git a/include/config-parser.h b/include/config-parser.h new file mode 100644 index 0000000000000000000000000000000000000000..53c8cc01b5068f189c3fb7deaea6c8e505586c5b --- /dev/null +++ b/include/config-parser.h @@ -0,0 +1,35 @@ +#ifndef __CONFIG_PARSER_H__ +#define __CONFIG_PARSER_H__ + +#include <string> +#include <iostream> +#include <filesystem> +#include <map> + +#include "logger.h" + +typedef std::pair<std::string,std::string> keyPair; +typedef std::map<std::string,std::string> ConfigMap; + +class ConfigParser +{ + public: + ConfigParser(); + ~ConfigParser(); + + ConfigMap userSettings(){ return this->readFile( "settings.cfg" ); } + + private: + const std::string CONFIG_DIRECTORY = "config"; + const char DELIMINATOR = ':'; + Logger& _logger; + + ConfigMap readFile( std::string path ); + // void writeConfigFile( std::map settings ); + + std::string getKey( std::string line ); + std::string getValue( std::string line ); +}; + +#endif // __CONFIG_PARSER_H__ + diff --git a/include/main.h b/include/main.h index 506b94a5c742314d84d9d929efb7432fa51d0426..2e355ca5f010fcd9e78992cb752c12c305a775d0 100644 --- a/include/main.h +++ b/include/main.h @@ -15,6 +15,7 @@ enum GenerationMethod #include "world-hex-2d.h" #include "world-map-3d.h" #include "chunk.h" +#include "config-parser.h" #include "game-engine.h" #include "logger.h" #include "entities/entity-generic.h" diff --git a/makefile b/makefile index a00133d48af6deca1730666732013fc6854425ba..dda3443183dd7ac592383a469e9057040cf393b6 100644 --- a/makefile +++ b/makefile @@ -61,6 +61,7 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp ${INC_DIR}/*.h clean: rm -rf $(BIN_DIR)/* $(BUILD_DIR)/* test_runnr rm -rf ./*~ ./*.swp + rm -rf *~ run_tests: $(TEST_OBJ_FILES) $(CXX) $(CXXFLAGS) $(LDFLAGS) $(INC_DIRS) -o ${TEST_TARGET} $^ diff --git a/src/config-parser.cpp b/src/config-parser.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8781bfd488ae68c3b4fa8e43847677edf79d49a2 --- /dev/null +++ b/src/config-parser.cpp @@ -0,0 +1,64 @@ +#include "config-parser.h" + +ConfigParser::ConfigParser() + : _logger( Logger::instance() ) +{ this->_logger.info( "Creating config parser" ); } + +ConfigParser::~ConfigParser() +{ + this->_logger.info( "Closing config parser" ); +} + + +ConfigMap ConfigParser::readFile( std::string path ) +{ + ConfigMap confMap; + if( ! std::filesystem::is_directory( CONFIG_DIRECTORY ) ) + { + this->_logger.warning( "Unable to read config directory" ); + return confMap; + } + + std::ifstream file( CONFIG_DIRECTORY+"/"+path ); + if( file.is_open() ) + { + std::string line; + while( std::getline(file, line) ) + { + if( ! line.empty() ) + { + std::string key = this->getKey( line ); + std::string value = this->getKey( line ); + std::cout << "[" << key << "] " << value << std::endl; + if( ! key.empty() && ! value.empty() ) + { confMap.insert( keyPair(key, value) ); } + } + } + } else + { this->_logger.error( "Unable to load specified configuration file:\n"+path ); } + + return confMap; +} + +std::string ConfigParser::getKey( std::string line ) +{ + std::string key = ""; + for( char ch : line ) + { + if( ch == DELIMINATOR ) { break; } + else if ( ! std::isspace(ch) ) { key.push_back( ch ) ; } + } + return key; +} + +std::string ConfigParser::getValue( std::string line ) +{ + std::string value = ""; + bool hasFound = false; + for( char ch : line ) + { + if( ch == DELIMINATOR ) { break; } + else if ( hasFound ) { value.push_back( ch ) ; } + } + return value; +} diff --git a/src/main.cpp b/src/main.cpp index 494c2ebac50467a7982a7a171ee4d70631b61d03..9d425ac51b8d32dbaffa3280631fcea63ed1ca52 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,8 @@ int main() { SaveManager saveManager; state = Initialise; + ConfigParser configParser; + ConfigMap userSettings = configParser.userSettings(); EntityManager entityManager; // Seed the random number generator