1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Config_PropManager.cpp
4 // Created: 13 Aug 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "Config_PropManager.h"
9 std::vector<int> stringToRGB(const std::string& theColor);
10 int stringToInteger(const std::string& theInt);
11 double stringToDouble(const std::string& theDouble);
12 bool stringToBoolean(const std::string& theInt);
14 Config_Properties Config_PropManager::myProps;
16 Config_Prop* Config_PropManager::registerProp(const std::string& theSection, const std::string& theName,
17 const std::string& theTitle, Config_Prop::PropType theType,
18 const std::string& theDefaultValue)
20 Config_Prop* aProp = findProp(theSection, theName);
23 if (aProp->value() == "") {
24 aProp->setValue(theDefaultValue);
26 if (aProp->defaultValue() == "") {
27 aProp->setDefaultValue(theDefaultValue);
29 if (aProp->type() == Config_Prop::Disabled) {
30 aProp->setType(theType);
31 aProp->setTitle(theTitle);
35 aProp = new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue);
36 myProps.push_back(aProp);
41 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
43 Config_Properties::const_iterator aIt;
44 for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) {
45 Config_Prop* aProp = (*aIt);
46 if ((aProp->section() == theSection) && (aProp->name() == theName))
52 Config_Properties Config_PropManager::getProperties()
54 Config_Properties aRes;
55 Config_Properties::const_iterator aIt;
56 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
57 Config_Prop* aProp = (*aIt);
58 if (aProp->type() != Config_Prop::Disabled)
59 aRes.push_back(aProp);
64 std::list<std::string> Config_PropManager::getSections()
66 // Return only non disabled sections
67 std::list<std::string> aSections;
68 Config_Properties::const_iterator aIt;
69 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
70 const Config_Prop* aProp = (*aIt);
71 if (aProp->type() != Config_Prop::Disabled)
72 aSections.push_back(aProp->section());
78 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
80 Config_Properties aRes;
81 Config_Properties::iterator aIt;
82 for (aIt = myProps.begin(); aIt != myProps.end(); aIt++) {
83 Config_Prop* aProp = (*aIt);
84 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
85 aRes.push_back(aProp);
90 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName,
91 const std::string& theDefault)
93 Config_Properties aProps = getProperties(theSection);
94 Config_Properties::const_iterator aIt;
95 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
96 Config_Prop* aProp = (*aIt);
97 if (aProp->name() == theName)
98 return aProp->value();
103 std::vector<int> Config_PropManager::color(const std::string& theSection,
104 const std::string& theName,
105 const std::string& theDefault)
107 std::string aStr = string(theSection, theName, theDefault);
108 return stringToRGB(aStr);
111 int Config_PropManager::integer(const std::string& theSection, const std::string& theName,
112 const std::string& theDefault)
114 std::string aStr = string(theSection, theName, theDefault);
115 return stringToInteger(aStr);
118 double Config_PropManager::real(const std::string& theSection, const std::string& theName,
119 const std::string& theDefault)
121 std::string aStr = string(theSection, theName, theDefault);
122 return stringToDouble(aStr);
125 bool Config_PropManager::boolean(const std::string& theSection,
126 const std::string& theName,
127 const std::string& theDefault)
129 std::string aStr = string(theSection, theName, theDefault);
130 return stringToBoolean(aStr);
133 std::vector<int> stringToRGB(const std::string& theColor)
135 std::vector<int> aRes(3);
137 if (theColor[0] == '#') {
142 aBuf[0] = theColor[1];
143 aBuf[1] = theColor[2];
144 aRes[0] = strtol(aBuf, &aP, 16);
146 aBuf[0] = theColor[3];
147 aBuf[1] = theColor[4];
148 aRes[1] = strtol(aBuf, &aP, 16);
150 aBuf[0] = theColor[5];
151 aBuf[1] = theColor[6];
152 aRes[2] = strtol(aBuf, &aP, 16);
154 int aPos = theColor.find(",");
157 std::size_t length = theColor.copy(aBuf, aPos, 0);
159 aRes[0] = atoi(aBuf);
162 int aNPos = theColor.find(",", aPos + 1);
163 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
165 aRes[1] = atoi(aBuf);
168 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
170 aRes[2] = atoi(aBuf);
175 int stringToInteger(const std::string& theInt)
177 return atoi(theInt.c_str());
180 double stringToDouble(const std::string& theDouble)
183 return strtod(theDouble.c_str(), &p);
186 bool stringToBoolean(const std::string& theBoolean)
188 return theBoolean == "true";