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);
13 Config_Properties Config_PropManager::myProps;
15 Config_Prop* Config_PropManager::registerProp(const std::string& theSection, const std::string& theName,
16 const std::string& theTitle, Config_Prop::PropType theType,
17 const std::string& theDefaultValue)
19 Config_Prop* aProp = findProp(theSection, theName);
22 if (aProp->value() == "") {
23 aProp->setValue(theDefaultValue);
25 if (aProp->defaultValue() == "") {
26 aProp->setDefaultValue(theDefaultValue);
28 if (aProp->type() == Config_Prop::Disabled) {
29 aProp->setType(theType);
30 aProp->setTitle(theTitle);
34 aProp = new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue);
35 myProps.push_back(aProp);
40 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
42 Config_Properties::const_iterator aIt;
43 for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) {
44 Config_Prop* aProp = (*aIt);
45 if ((aProp->section() == theSection) && (aProp->name() == theName))
51 Config_Properties Config_PropManager::getProperties()
53 Config_Properties aRes;
54 Config_Properties::const_iterator aIt;
55 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
56 Config_Prop* aProp = (*aIt);
57 if (aProp->type() != Config_Prop::Disabled)
58 aRes.push_back(aProp);
63 std::list<std::string> Config_PropManager::getSections()
65 // Return only non disabled sections
66 std::list<std::string> aSections;
67 Config_Properties::const_iterator aIt;
68 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
69 const Config_Prop* aProp = (*aIt);
70 if (aProp->type() != Config_Prop::Disabled)
71 aSections.push_back(aProp->section());
77 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
79 Config_Properties aRes;
80 Config_Properties::iterator aIt;
81 for (aIt = myProps.begin(); aIt != myProps.end(); aIt++) {
82 Config_Prop* aProp = (*aIt);
83 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
84 aRes.push_back(aProp);
89 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName,
90 const std::string& theDefault)
92 Config_Properties aProps = getProperties(theSection);
93 Config_Properties::const_iterator aIt;
94 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
95 Config_Prop* aProp = (*aIt);
96 if (aProp->name() == theName)
97 return aProp->value();
102 std::vector<int> Config_PropManager::color(const std::string& theSection,
103 const std::string& theName,
104 const std::string& theDefault)
106 std::string aStr = string(theSection, theName, theDefault);
107 return stringToRGB(aStr);
110 int Config_PropManager::integer(const std::string& theSection, const std::string& theName,
111 const std::string& theDefault)
113 std::string aStr = string(theSection, theName, theDefault);
114 return stringToInteger(aStr);
117 double Config_PropManager::real(const std::string& theSection, const std::string& theName,
118 const std::string& theDefault)
120 std::string aStr = string(theSection, theName, theDefault);
121 return stringToDouble(aStr);
124 std::vector<int> stringToRGB(const std::string& theColor)
126 std::vector<int> aRes(3);
128 if (theColor[0] == '#') {
133 aBuf[0] = theColor[1];
134 aBuf[1] = theColor[2];
135 aRes[0] = strtol(aBuf, &aP, 16);
137 aBuf[0] = theColor[3];
138 aBuf[1] = theColor[4];
139 aRes[1] = strtol(aBuf, &aP, 16);
141 aBuf[0] = theColor[5];
142 aBuf[1] = theColor[6];
143 aRes[2] = strtol(aBuf, &aP, 16);
145 int aPos = theColor.find(",");
148 std::size_t length = theColor.copy(aBuf, aPos, 0);
150 aRes[0] = atoi(aBuf);
153 int aNPos = theColor.find(",", aPos + 1);
154 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
156 aRes[1] = atoi(aBuf);
159 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
161 aRes[2] = atoi(aBuf);
166 int stringToInteger(const std::string& theInt)
168 return atoi(theInt.c_str());
171 double stringToDouble(const std::string& theDouble)
174 return strtod(theDouble.c_str(), &p);