Advertisement
Guest User

Example Config Class

a guest
Sep 16th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. //Header
  2. #pragma once
  3. #include <map>
  4. #include <string>
  5.  
  6. class Config
  7. {
  8. private:
  9.     std::map<std::string, std::string> properties;
  10.     void OutputProperties();
  11.  
  12. public:
  13.     Config();
  14.     ~Config();
  15.  
  16.     bool DoesPropertyExist(std::string name);
  17.  
  18.     bool GetPropertyAsBool(std::string Name);
  19.     int GetPropertyAsInt(std::string Name);
  20.     std::string GetPropertyAsString(std::string Name);
  21.  
  22.     void SetPropertyAsBool(std::string Name, bool Value);
  23.     void SetPropertyAsInt(std::string Name, int Value);
  24.     void SetPropertyAsString(std::string Name, std::string Value);
  25. };
  26.  
  27. //CPP
  28. #include "Config.h"
  29. #include <iostream>
  30. #include <fstream>
  31. #include <sstream>
  32. #include <algorithm>
  33. #include <iterator>
  34. #include <vector>
  35.  
  36. Config::Config()
  37. {
  38.     properties["SoundVolume"] = "100";
  39.     properties["MusicVolume"] = "100";
  40.     properties["WindowHeight"] = "360";
  41.     properties["WindowWidth"] = "640";
  42.     properties["Fullscreen"] = "true";
  43.     properties["VSync"] = "false";
  44.  
  45.     std::ifstream iFile("config.txt");
  46.  
  47.     if (iFile.is_open())
  48.     {
  49.         std::string line;
  50.         while (getline(iFile, line))
  51.         {
  52.             std::vector<std::string> tokens;
  53.             std::istringstream iss(line);
  54.             copy(std::istream_iterator<std::string>(iss),
  55.                 std::istream_iterator<std::string>(),
  56.                 std::back_inserter(tokens));
  57.  
  58.             if (tokens.size() == 3 && tokens[0][0] != '/')
  59.             {
  60.                 properties[tokens[0]] = tokens[2];
  61.             }
  62.             else if (tokens.size() == 1)
  63.             {
  64.                 properties[tokens[0]] = "";
  65.             }
  66.         }
  67.     }
  68.     else
  69.     {
  70.         OutputProperties();
  71.     }
  72. }
  73.  
  74.  
  75. Config::~Config()
  76. {
  77.     OutputProperties();
  78.     properties.clear();
  79. }
  80.  
  81. void Config::OutputProperties()
  82. {
  83.     std::ofstream oFile("config.txt");
  84.  
  85.     if (oFile.is_open())
  86.     {
  87.         oFile << "/                                                        /" << std::endl;
  88.         oFile << "/ Gemstone Keeper - Version " << GEMSTONE_KEEPER_BUILD      << std::endl;
  89.         oFile << "/ Build Date: " << __DATE__ << " - " << __TIME__            << std::endl;
  90.         oFile << "/ Game Developed by Gamepopper. As well as this config.  /" << std::endl;
  91.         oFile << "/                                                        /" << std::endl;
  92.         oFile << "/ Make sure you have a ' = ' between property and value. /" << std::endl;
  93.         oFile << "/                                                        /" << std::endl;
  94.  
  95.         for each (std::pair<std::string, std::string> pair in properties)
  96.         {
  97.             if (pair.second != "")
  98.             {
  99.                 oFile << pair.first << " = " << pair.second << std::endl;
  100.             }
  101.             else
  102.             {
  103.                 oFile << pair.first << std::endl;
  104.             }
  105.         }
  106.     }
  107. }
  108.  
  109. bool Config::DoesPropertyExist(std::string Name)
  110. {
  111.     return properties.find(Name) != properties.end();
  112. }
  113.  
  114. bool Config::GetPropertyAsBool(std::string Name)
  115. {
  116.     return properties[Name] == "true" ? true : false;
  117. }
  118.  
  119. int Config::GetPropertyAsInt(std::string Name)
  120. {
  121.     return std::stoul(properties[Name]);
  122. }
  123.  
  124. std::string Config::GetPropertyAsString(std::string Name)
  125. {
  126.     return properties[Name];
  127. }
  128.  
  129. void Config::SetPropertyAsBool(std::string Name, bool Value)
  130. {
  131.     properties[Name] = Value ? "true" : "false";
  132. }
  133.  
  134. void Config::SetPropertyAsInt(std::string Name, int Value)
  135. {
  136.     properties[Name] = std::to_string(Value);
  137. }
  138.  
  139. void Config::SetPropertyAsString(std::string Name, std::string Value)
  140. {
  141.     properties[Name] = Value;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement