Skip to content
Snippets Groups Projects
Commit b16759b5 authored by Alfred Burgess's avatar Alfred Burgess
Browse files

Created very basic system for reading a basic config file

parent 6144dfc6
Branches
No related merge requests found
test1: test
test2: tedfh
#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__
...@@ -15,6 +15,7 @@ enum GenerationMethod ...@@ -15,6 +15,7 @@ enum GenerationMethod
#include "world-hex-2d.h" #include "world-hex-2d.h"
#include "world-map-3d.h" #include "world-map-3d.h"
#include "chunk.h" #include "chunk.h"
#include "config-parser.h"
#include "game-engine.h" #include "game-engine.h"
#include "logger.h" #include "logger.h"
#include "entities/entity-generic.h" #include "entities/entity-generic.h"
......
...@@ -61,6 +61,7 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp ${INC_DIR}/*.h ...@@ -61,6 +61,7 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp ${INC_DIR}/*.h
clean: clean:
rm -rf $(BIN_DIR)/* $(BUILD_DIR)/* test_runnr rm -rf $(BIN_DIR)/* $(BUILD_DIR)/* test_runnr
rm -rf ./*~ ./*.swp rm -rf ./*~ ./*.swp
rm -rf *~
run_tests: $(TEST_OBJ_FILES) run_tests: $(TEST_OBJ_FILES)
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(INC_DIRS) -o ${TEST_TARGET} $^ $(CXX) $(CXXFLAGS) $(LDFLAGS) $(INC_DIRS) -o ${TEST_TARGET} $^
......
#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;
}
...@@ -12,6 +12,8 @@ int main() { ...@@ -12,6 +12,8 @@ int main() {
SaveManager saveManager; SaveManager saveManager;
state = Initialise; state = Initialise;
ConfigParser configParser;
ConfigMap userSettings = configParser.userSettings();
EntityManager entityManager; EntityManager entityManager;
// Seed the random number generator // Seed the random number generator
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment