|
| 1 | +#include "Config.h" |
| 2 | + |
| 3 | +#include <sstream> |
| 4 | +#include <stdexcept> |
| 5 | + |
| 6 | +#include "Util.h" |
| 7 | + |
| 8 | +using std::string; |
| 9 | +using std::stringstream; |
| 10 | +using std::runtime_error; |
| 11 | +using std::istream; |
| 12 | +using std::fstream; |
| 13 | + |
| 14 | +ConfigValue::ConfigValue(const string& value) : value(value) { |
| 15 | + |
| 16 | +} |
| 17 | + |
| 18 | +ConfigValue::operator int() const { |
| 19 | + return fromString<int>(value); |
| 20 | +} |
| 21 | + |
| 22 | +ConfigValue::operator float() const { |
| 23 | + return fromString<float>(value); |
| 24 | +} |
| 25 | + |
| 26 | +ConfigValue::operator string() const { |
| 27 | + return value; |
| 28 | +} |
| 29 | + |
| 30 | +ConfigValue::operator Coordinate() const { |
| 31 | + stringstream stream(value); |
| 32 | + Coordinate ret; |
| 33 | + char c = stream.get(); |
| 34 | + if(c != '(') { |
| 35 | + throw runtime_error("Attempt to read a coordinate out of a non-coordinate value"); |
| 36 | + } |
| 37 | + stream >> ret.first; |
| 38 | + c = stream.get(); |
| 39 | + if(c != ',') { |
| 40 | + throw runtime_error("Attempt to read a coordinate out of a non-coordinate value"); |
| 41 | + } |
| 42 | + stream >> ret.second; |
| 43 | + return ret; |
| 44 | +} |
| 45 | + |
| 46 | +ConfigValue::operator ScreenCoordinate() const { |
| 47 | + stringstream stream(value); |
| 48 | + ScreenCoordinate ret; |
| 49 | + char c = stream.get(); |
| 50 | + if(c != '(') { |
| 51 | + throw runtime_error("Attempt to read a coordinate out of a non-coordinate value"); |
| 52 | + } |
| 53 | + stream >> ret.first; |
| 54 | + c = stream.get(); |
| 55 | + if(c != ',') { |
| 56 | + throw runtime_error("Attempt to read a coordinate out of a non-coordinate value"); |
| 57 | + } |
| 58 | + stream >> ret.second; |
| 59 | + return ret; |
| 60 | +} |
| 61 | + |
| 62 | +Config::Config(istream& source) { |
| 63 | + init(source); |
| 64 | +} |
| 65 | + |
| 66 | +Config::Config(fstream&& source) { |
| 67 | + init(source); |
| 68 | +} |
| 69 | + |
| 70 | +Config::Config(const string& filename) { |
| 71 | + fstream source(filename, fstream::in); |
| 72 | + init(source); |
| 73 | +} |
| 74 | + |
| 75 | +void Config::init(istream& source) { |
| 76 | + string line; |
| 77 | + while(source) { |
| 78 | + std::getline(source, line); |
| 79 | + if(line[0] == '#' || line.find('=') == string::npos) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + string name = line.substr(0, line.find('=')); |
| 83 | + string value = line.substr(line.find('=') + 1); |
| 84 | + values.insert(std::make_pair(name, ConfigValue(value))); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +const ConfigValue& Config::operator[](const string& name) const { |
| 89 | + auto it = values.find(name); |
| 90 | + if(it == values.end()) { |
| 91 | + throw runtime_error("No such key in the config"); |
| 92 | + } else { |
| 93 | + return it->second; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +const Config& getConfigFile(const std::string& filename) { |
| 98 | + static std::map<std::string, Config> configs; |
| 99 | + |
| 100 | + auto it = configs.find(filename); |
| 101 | + if(it == configs.end()) { |
| 102 | + fstream file(filename); |
| 103 | + if(!file) { |
| 104 | + throw runtime_error("Cannot open file " + filename); |
| 105 | + } |
| 106 | + return configs.insert(std::make_pair(filename, Config(file))).first->second; |
| 107 | + } else { |
| 108 | + return it->second; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +const Config& getGraphicsConfig() { |
| 113 | + return getConfigFile("resources/graphics.conf"); |
| 114 | +} |
0 commit comments