Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Header
- #pragma once
- #include <map>
- #include <string>
- class Config
- {
- private:
- std::map<std::string, std::string> properties;
- void OutputProperties();
- public:
- Config();
- ~Config();
- bool DoesPropertyExist(std::string name);
- bool GetPropertyAsBool(std::string Name);
- int GetPropertyAsInt(std::string Name);
- std::string GetPropertyAsString(std::string Name);
- void SetPropertyAsBool(std::string Name, bool Value);
- void SetPropertyAsInt(std::string Name, int Value);
- void SetPropertyAsString(std::string Name, std::string Value);
- };
- //CPP
- #include "Config.h"
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <algorithm>
- #include <iterator>
- #include <vector>
- Config::Config()
- {
- properties["SoundVolume"] = "100";
- properties["MusicVolume"] = "100";
- properties["WindowHeight"] = "360";
- properties["WindowWidth"] = "640";
- properties["Fullscreen"] = "true";
- properties["VSync"] = "false";
- std::ifstream iFile("config.txt");
- if (iFile.is_open())
- {
- std::string line;
- while (getline(iFile, line))
- {
- std::vector<std::string> tokens;
- std::istringstream iss(line);
- copy(std::istream_iterator<std::string>(iss),
- std::istream_iterator<std::string>(),
- std::back_inserter(tokens));
- if (tokens.size() == 3 && tokens[0][0] != '/')
- {
- properties[tokens[0]] = tokens[2];
- }
- else if (tokens.size() == 1)
- {
- properties[tokens[0]] = "";
- }
- }
- }
- else
- {
- OutputProperties();
- }
- }
- Config::~Config()
- {
- OutputProperties();
- properties.clear();
- }
- void Config::OutputProperties()
- {
- std::ofstream oFile("config.txt");
- if (oFile.is_open())
- {
- oFile << "/ /" << std::endl;
- oFile << "/ Gemstone Keeper - Version " << GEMSTONE_KEEPER_BUILD << std::endl;
- oFile << "/ Build Date: " << __DATE__ << " - " << __TIME__ << std::endl;
- oFile << "/ Game Developed by Gamepopper. As well as this config. /" << std::endl;
- oFile << "/ /" << std::endl;
- oFile << "/ Make sure you have a ' = ' between property and value. /" << std::endl;
- oFile << "/ /" << std::endl;
- for each (std::pair<std::string, std::string> pair in properties)
- {
- if (pair.second != "")
- {
- oFile << pair.first << " = " << pair.second << std::endl;
- }
- else
- {
- oFile << pair.first << std::endl;
- }
- }
- }
- }
- bool Config::DoesPropertyExist(std::string Name)
- {
- return properties.find(Name) != properties.end();
- }
- bool Config::GetPropertyAsBool(std::string Name)
- {
- return properties[Name] == "true" ? true : false;
- }
- int Config::GetPropertyAsInt(std::string Name)
- {
- return std::stoul(properties[Name]);
- }
- std::string Config::GetPropertyAsString(std::string Name)
- {
- return properties[Name];
- }
- void Config::SetPropertyAsBool(std::string Name, bool Value)
- {
- properties[Name] = Value ? "true" : "false";
- }
- void Config::SetPropertyAsInt(std::string Name, int Value)
- {
- properties[Name] = std::to_string(Value);
- }
- void Config::SetPropertyAsString(std::string Name, std::string Value)
- {
- properties[Name] = Value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement