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,
17 const std::string& theName,
18 const std::string& theTitle,
19 Config_Prop::PropType theType,
20 const std::string& theDefaultValue)
22 Config_Prop* aProp = findProp(theSection, theName);
25 if (aProp->value() == "") {
26 aProp->setValue(theDefaultValue);
28 if (aProp->defaultValue() == "") {
29 aProp->setDefaultValue(theDefaultValue);
31 if (aProp->type() == Config_Prop::Disabled) {
32 aProp->setType(theType);
33 aProp->setTitle(theTitle);
37 aProp = new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue);
38 myProps.push_back(aProp);
43 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
45 Config_Properties::const_iterator aIt;
46 for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) {
47 Config_Prop* aProp = (*aIt);
48 if ((aProp->section() == theSection) && (aProp->name() == theName))
54 Config_Properties Config_PropManager::getProperties()
56 Config_Properties aRes;
57 Config_Properties::const_iterator aIt;
58 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
59 Config_Prop* aProp = (*aIt);
60 if (aProp->type() != Config_Prop::Disabled)
61 aRes.push_back(aProp);
66 std::list<std::string> Config_PropManager::getSections()
68 // Return only non disabled sections
69 std::list<std::string> aSections;
70 Config_Properties::const_iterator aIt;
71 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
72 const Config_Prop* aProp = (*aIt);
73 if (aProp->type() != Config_Prop::Disabled)
74 aSections.push_back(aProp->section());
80 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
82 Config_Properties aRes;
83 Config_Properties::iterator aIt;
84 for (aIt = myProps.begin(); aIt != myProps.end(); aIt++) {
85 Config_Prop* aProp = (*aIt);
86 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
87 aRes.push_back(aProp);
92 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName,
93 const std::string& theDefault)
95 Config_Properties aProps = getProperties(theSection);
96 Config_Properties::const_iterator aIt;
97 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
98 Config_Prop* aProp = (*aIt);
99 if (aProp->name() == theName)
100 return aProp->value();
105 std::vector<int> Config_PropManager::color(const std::string& theSection,
106 const std::string& theName,
107 const std::string& theDefault)
109 std::string aStr = string(theSection, theName, theDefault);
110 return stringToRGB(aStr);
113 int Config_PropManager::integer(const std::string& theSection, const std::string& theName,
114 const std::string& theDefault)
116 std::string aStr = string(theSection, theName, theDefault);
117 return stringToInteger(aStr);
120 double Config_PropManager::real(const std::string& theSection, const std::string& theName,
121 const std::string& theDefault)
123 std::string aStr = string(theSection, theName, theDefault);
124 return stringToDouble(aStr);
127 bool Config_PropManager::boolean(const std::string& theSection,
128 const std::string& theName,
129 const std::string& theDefault)
131 std::string aStr = string(theSection, theName, theDefault);
132 return stringToBoolean(aStr);
135 std::vector<int> stringToRGB(const std::string& theColor)
137 std::vector<int> aRes(3);
139 if (theColor[0] == '#') {
144 aBuf[0] = theColor[1];
145 aBuf[1] = theColor[2];
146 aRes[0] = strtol(aBuf, &aP, 16);
148 aBuf[0] = theColor[3];
149 aBuf[1] = theColor[4];
150 aRes[1] = strtol(aBuf, &aP, 16);
152 aBuf[0] = theColor[5];
153 aBuf[1] = theColor[6];
154 aRes[2] = strtol(aBuf, &aP, 16);
156 int aPos = (int)theColor.find(",");
159 std::size_t length = theColor.copy(aBuf, aPos, 0);
161 aRes[0] = atoi(aBuf);
164 int aNPos = (int)theColor.find(",", aPos + 1);
165 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
167 aRes[1] = atoi(aBuf);
170 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
172 aRes[2] = atoi(aBuf);
177 int stringToInteger(const std::string& theInt)
179 return atoi(theInt.c_str());
182 double stringToDouble(const std::string& theDouble)
185 return strtod(theDouble.c_str(), &p);
188 bool stringToBoolean(const std::string& theBoolean)
190 return theBoolean == "true";