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;
17 Config_Prop* Config_PropManager::registerProp(const std::string& theSection,
18 const std::string& theName,
19 const std::string& theTitle,
20 Config_Prop::PropType theType,
21 const std::string& theDefaultValue)
23 Config_Prop* aProp = findProp(theSection, theName);
26 if (aProp->value() == "") {
27 aProp->setValue(theDefaultValue);
29 if (aProp->defaultValue() == "") {
30 aProp->setDefaultValue(theDefaultValue);
32 if (aProp->type() == Config_Prop::Disabled) {
33 aProp->setType(theType);
34 aProp->setTitle(theTitle);
38 aProp = new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue);
39 myProps.push_back(aProp);
44 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
46 Config_Properties::const_iterator aIt;
47 for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) {
48 Config_Prop* aProp = (*aIt);
49 if ((aProp->section() == theSection) && (aProp->name() == theName))
55 Config_Properties Config_PropManager::getProperties()
57 Config_Properties aRes;
58 Config_Properties::const_iterator aIt;
59 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
60 Config_Prop* aProp = (*aIt);
61 if (aProp->type() != Config_Prop::Disabled)
62 aRes.push_back(aProp);
67 std::list<std::string> Config_PropManager::getSections()
69 // Return only non disabled sections
70 std::list<std::string> aSections;
71 Config_Properties::const_iterator aIt;
72 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
73 const Config_Prop* aProp = (*aIt);
74 if (aProp->type() != Config_Prop::Disabled)
75 aSections.push_back(aProp->section());
81 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
83 Config_Properties aRes;
84 Config_Properties::iterator aIt;
85 for (aIt = myProps.begin(); aIt != myProps.end(); aIt++) {
86 Config_Prop* aProp = (*aIt);
87 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
88 aRes.push_back(aProp);
93 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName)
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();
102 std::string aMsg = "Property " + theSection + ":" + theName + " is not registered";
106 std::vector<int> Config_PropManager::color(const std::string& theSection,
107 const std::string& theName)
109 std::string aStr = string(theSection, theName);
110 return stringToRGB(aStr);
113 int Config_PropManager::integer(const std::string& theSection, const std::string& theName)
115 std::string aStr = string(theSection, theName);
116 return stringToInteger(aStr);
119 double Config_PropManager::real(const std::string& theSection, const std::string& theName)
121 std::string aStr = string(theSection, theName);
122 return stringToDouble(aStr);
125 bool Config_PropManager::boolean(const std::string& theSection,
126 const std::string& theName)
128 std::string aStr = string(theSection, theName);
129 return stringToBoolean(aStr);
132 std::vector<int> stringToRGB(const std::string& theColor)
134 std::vector<int> aRes(3);
136 if (theColor[0] == '#') {
141 aBuf[0] = theColor[1];
142 aBuf[1] = theColor[2];
143 aRes[0] = strtol(aBuf, &aP, 16);
145 aBuf[0] = theColor[3];
146 aBuf[1] = theColor[4];
147 aRes[1] = strtol(aBuf, &aP, 16);
149 aBuf[0] = theColor[5];
150 aBuf[1] = theColor[6];
151 aRes[2] = strtol(aBuf, &aP, 16);
153 int aPos = (int)theColor.find(",");
156 std::size_t length = theColor.copy(aBuf, aPos, 0);
158 aRes[0] = atoi(aBuf);
161 int aNPos = (int)theColor.find(",", aPos + 1);
162 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
164 aRes[1] = atoi(aBuf);
167 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
169 aRes[2] = atoi(aBuf);
174 int stringToInteger(const std::string& theInt)
176 return atoi(theInt.c_str());
179 double stringToDouble(const std::string& theDouble)
182 return strtod(theDouble.c_str(), &p);
185 bool stringToBoolean(const std::string& theBoolean)
187 return theBoolean == "true";