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 bool Config_PropManager::registerProp(const std::string& theSection, const std::string& theName,
14 const std::string& theTitle, Config_Prop::PropType theType,
15 const std::string& theValue)
17 Config_Prop* aProp = findProp(theSection, theName);
19 if (aProp->type() == Config_Prop::Disabled) {
20 aProp->setType(theType);
21 aProp->setTitle(theTitle);
26 aProp = new Config_Prop(theSection, theName, theTitle, theType, theValue);
27 myProps.push_back(aProp);
31 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
33 Config_Properties::const_iterator aIt;
34 for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) {
35 Config_Prop* aProp = (*aIt);
36 if ((aProp->section() == theSection) && (aProp->name() == theName))
42 Config_Properties Config_PropManager::getProperties()
44 Config_Properties aRes;
45 Config_Properties::const_iterator aIt;
46 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
47 Config_Prop* aProp = (*aIt);
48 if (aProp->type() != Config_Prop::Disabled)
49 aRes.push_back(aProp);
54 std::list<std::string> Config_PropManager::getSections()
56 // Return only non disabled sections
57 std::list<std::string> aSections;
58 Config_Properties::const_iterator aIt;
59 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
60 const Config_Prop* aProp = (*aIt);
61 if (aProp->type() != Config_Prop::Disabled)
62 aSections.push_back(aProp->section());
68 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
70 Config_Properties aRes;
71 Config_Properties::iterator aIt;
72 for (aIt = myProps.begin(); aIt != myProps.end(); aIt++) {
73 Config_Prop* aProp = (*aIt);
74 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
75 aRes.push_back(aProp);
80 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName,
81 const std::string& theDefault)
83 Config_Properties aProps = getProperties(theSection);
84 Config_Properties::const_iterator aIt;
85 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
86 Config_Prop* aProp = (*aIt);
87 if (aProp->name() == theName)
88 return aProp->value();
93 std::vector<int> Config_PropManager::color(const std::string& theSection,
94 const std::string& theName,
95 const std::string& theDefault)
97 std::string aStr = string(theSection, theName, theDefault);
98 return stringToRGB(aStr);
101 int Config_PropManager::integer(const std::string& theSection, const std::string& theName,
102 const std::string& theDefault)
104 std::string aStr = string(theSection, theName, theDefault);
105 return stringToInteger(aStr);
108 double Config_PropManager::real(const std::string& theSection, const std::string& theName,
109 const std::string& theDefault)
111 std::string aStr = string(theSection, theName, theDefault);
112 return stringToDouble(aStr);
115 std::vector<int> stringToRGB(const std::string& theColor)
117 std::vector<int> aRes(3);
119 if (theColor[0] == '#') {
124 aBuf[0] = theColor[1];
125 aBuf[1] = theColor[2];
126 aRes[0] = strtol(aBuf, &aP, 16);
128 aBuf[0] = theColor[3];
129 aBuf[1] = theColor[4];
130 aRes[1] = strtol(aBuf, &aP, 16);
132 aBuf[0] = theColor[5];
133 aBuf[1] = theColor[6];
134 aRes[2] = strtol(aBuf, &aP, 16);
136 int aPos = theColor.find(",");
139 std::size_t length = theColor.copy(aBuf, aPos, 0);
141 aRes[0] = atoi(aBuf);
144 int aNPos = theColor.find(",", aPos + 1);
145 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
147 aRes[1] = atoi(aBuf);
150 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
152 aRes[2] = atoi(aBuf);
157 int stringToInteger(const std::string& theInt)
159 return atoi(theInt.c_str());
162 double stringToDouble(const std::string& theDouble)
165 return strtod(theDouble.c_str(), &p);