1 // File: Config_PropManager.cpp
2 // Created: 13 Aug 2014
3 // Author: Vitaly SMETANNIKOV
5 #include "Config_PropManager.h"
7 std::vector<int> stringToRGB(const std::string& theColor);
8 int stringToInteger(const std::string& theInt);
9 double stringToDouble(const std::string& theDouble);
11 Config_Properties Config_PropManager::myProps;
13 Config_Prop* Config_PropManager::registerProp(const std::string& theSection, const std::string& theName,
14 const std::string& theTitle, Config_Prop::PropType theType,
15 const std::string& theDefaultValue)
17 Config_Prop* aProp = findProp(theSection, theName);
20 if (aProp->value() == "") {
21 aProp->setValue(theDefaultValue);
23 if (aProp->defaultValue() == "") {
24 aProp->setDefaultValue(theDefaultValue);
26 if (aProp->type() == Config_Prop::Disabled) {
27 aProp->setType(theType);
28 aProp->setTitle(theTitle);
32 aProp = new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue);
33 myProps.push_back(aProp);
38 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
40 Config_Properties::const_iterator aIt;
41 for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) {
42 Config_Prop* aProp = (*aIt);
43 if ((aProp->section() == theSection) && (aProp->name() == theName))
49 Config_Properties Config_PropManager::getProperties()
51 Config_Properties aRes;
52 Config_Properties::const_iterator aIt;
53 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
54 Config_Prop* aProp = (*aIt);
55 if (aProp->type() != Config_Prop::Disabled)
56 aRes.push_back(aProp);
61 std::list<std::string> Config_PropManager::getSections()
63 // Return only non disabled sections
64 std::list<std::string> aSections;
65 Config_Properties::const_iterator aIt;
66 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
67 const Config_Prop* aProp = (*aIt);
68 if (aProp->type() != Config_Prop::Disabled)
69 aSections.push_back(aProp->section());
75 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
77 Config_Properties aRes;
78 Config_Properties::iterator aIt;
79 for (aIt = myProps.begin(); aIt != myProps.end(); aIt++) {
80 Config_Prop* aProp = (*aIt);
81 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
82 aRes.push_back(aProp);
87 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName,
88 const std::string& theDefault)
90 Config_Properties aProps = getProperties(theSection);
91 Config_Properties::const_iterator aIt;
92 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
93 Config_Prop* aProp = (*aIt);
94 if (aProp->name() == theName)
95 return aProp->value();
100 std::vector<int> Config_PropManager::color(const std::string& theSection,
101 const std::string& theName,
102 const std::string& theDefault)
104 std::string aStr = string(theSection, theName, theDefault);
105 return stringToRGB(aStr);
108 int Config_PropManager::integer(const std::string& theSection, const std::string& theName,
109 const std::string& theDefault)
111 std::string aStr = string(theSection, theName, theDefault);
112 return stringToInteger(aStr);
115 double Config_PropManager::real(const std::string& theSection, const std::string& theName,
116 const std::string& theDefault)
118 std::string aStr = string(theSection, theName, theDefault);
119 return stringToDouble(aStr);
122 std::vector<int> stringToRGB(const std::string& theColor)
124 std::vector<int> aRes(3);
126 if (theColor[0] == '#') {
131 aBuf[0] = theColor[1];
132 aBuf[1] = theColor[2];
133 aRes[0] = strtol(aBuf, &aP, 16);
135 aBuf[0] = theColor[3];
136 aBuf[1] = theColor[4];
137 aRes[1] = strtol(aBuf, &aP, 16);
139 aBuf[0] = theColor[5];
140 aBuf[1] = theColor[6];
141 aRes[2] = strtol(aBuf, &aP, 16);
143 int aPos = theColor.find(",");
146 std::size_t length = theColor.copy(aBuf, aPos, 0);
148 aRes[0] = atoi(aBuf);
151 int aNPos = theColor.find(",", aPos + 1);
152 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
154 aRes[1] = atoi(aBuf);
157 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
159 aRes[2] = atoi(aBuf);
164 int stringToInteger(const std::string& theInt)
166 return atoi(theInt.c_str());
169 double stringToDouble(const std::string& theDouble)
172 return strtod(theDouble.c_str(), &p);