1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "Config_PropManager.h"
22 std::vector<int> stringToRGB(const std::string& theColor);
23 int stringToInteger(const std::string& theInt);
24 bool stringToBoolean(const std::string& theInt);
26 Config_Properties& Config_PropManager::props() {
27 static Config_Properties* confProps = new Config_Properties();
32 Config_Prop* Config_PropManager::registerProp(const std::string& theSection,
33 const std::string& theName,
34 const std::string& theTitle,
35 Config_Prop::PropType theType,
36 const std::string& theDefaultValue,
37 const std::string& theMin,
38 const std::string& theMax)
40 Config_Prop* aProp = findProp(theSection, theName);
43 if (aProp->value() == "") {
44 aProp->setValue(theDefaultValue);
46 if (aProp->defaultValue() == "") {
47 aProp->setDefaultValue(theDefaultValue);
49 if (aProp->type() == Config_Prop::Disabled) {
50 aProp->setType(theType);
51 aProp->setTitle(theTitle);
53 aProp->setMin(theMin);
54 aProp->setMax(theMax);
58 new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue, theMin, theMax);
59 props().push_back(aProp);
64 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
66 Config_Properties::const_iterator aIt;
67 Config_Properties aProps = props();
68 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
69 Config_Prop* aProp = (*aIt);
70 if ((aProp->section() == theSection) && (aProp->name() == theName))
76 Config_Properties Config_PropManager::getProperties()
78 Config_Properties aRes;
79 Config_Properties::const_iterator aIt;
80 Config_Properties aProps = props();
81 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
82 Config_Prop* aProp = (*aIt);
83 if (aProp->type() != Config_Prop::Disabled)
84 aRes.push_back(aProp);
89 std::list<std::string> Config_PropManager::getSections()
91 // Return only non disabled sections
92 std::list<std::string> aSections;
93 Config_Properties::const_iterator aIt;
94 Config_Properties aProps = props();
95 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
96 const Config_Prop* aProp = (*aIt);
97 if (aProp->type() != Config_Prop::Disabled)
98 aSections.push_back(aProp->section());
104 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
106 Config_Properties aRes;
107 Config_Properties::iterator aIt;
108 Config_Properties aProps = props();
109 for (aIt = aProps.begin(); aIt != aProps.end(); aIt++) {
110 Config_Prop* aProp = (*aIt);
111 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
112 aRes.push_back(aProp);
117 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName)
119 Config_Properties aProps = getProperties(theSection);
120 Config_Properties::const_iterator aIt;
121 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
122 Config_Prop* aProp = (*aIt);
123 if (aProp->name() == theName)
124 return aProp->value();
126 std::string aMsg = "Property " + theSection + ":" + theName + " is not registered";
130 std::vector<int> Config_PropManager::color(const std::string& theSection,
131 const std::string& theName)
133 std::string aStr = string(theSection, theName);
134 return stringToRGB(aStr);
137 int Config_PropManager::integer(const std::string& theSection, const std::string& theName)
139 std::string aStr = string(theSection, theName);
140 return stringToInteger(aStr);
143 double Config_PropManager::real(const std::string& theSection, const std::string& theName)
145 std::string aStr = string(theSection, theName);
146 return stringToDouble(aStr);
149 bool Config_PropManager::boolean(const std::string& theSection,
150 const std::string& theName)
152 std::string aStr = string(theSection, theName);
153 return stringToBoolean(aStr);
156 std::vector<int> stringToRGB(const std::string& theColor)
158 std::vector<int> aRes(3);
160 if (theColor[0] == '#') {
165 aBuf[0] = theColor[1];
166 aBuf[1] = theColor[2];
167 aRes[0] = strtol(aBuf, &aP, 16);
169 aBuf[0] = theColor[3];
170 aBuf[1] = theColor[4];
171 aRes[1] = strtol(aBuf, &aP, 16);
173 aBuf[0] = theColor[5];
174 aBuf[1] = theColor[6];
175 aRes[2] = strtol(aBuf, &aP, 16);
177 int aPos = (int)theColor.find(",");
180 std::size_t length = theColor.copy(aBuf, aPos, 0);
182 aRes[0] = atoi(aBuf);
185 int aNPos = (int)theColor.find(",", aPos + 1);
186 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
188 aRes[1] = atoi(aBuf);
191 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
193 aRes[2] = atoi(aBuf);
198 int stringToInteger(const std::string& theInt)
200 return atoi(theInt.c_str());
203 double Config_PropManager::stringToDouble(const std::string& theDouble)
205 std::string aStr = theDouble;
207 // change locale and convert "," to "." if exists
208 std::string aCurLocale = setlocale(LC_NUMERIC, 0);
209 setlocale(LC_NUMERIC, "C");
210 int dotpos = (int)aStr.find(',');
211 if (dotpos != std::string::npos)
212 aStr.replace(dotpos, 1, ".");
215 double aValue = strtod(aStr.c_str(), &p);
218 setlocale(LC_NUMERIC, aCurLocale.c_str());
222 bool stringToBoolean(const std::string& theBoolean)
224 return theBoolean == "true";