1 // Copyright (C) 2014-2021 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);
54 aProp->setMin(theMin);
56 aProp->setMax(theMax);
60 new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue, theMin, theMax);
61 props().push_back(aProp);
66 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
68 Config_Properties::const_iterator aIt;
69 Config_Properties aProps = props();
70 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
71 Config_Prop* aProp = (*aIt);
72 if ((aProp->section() == theSection) && (aProp->name() == theName))
78 Config_Properties Config_PropManager::getProperties()
80 Config_Properties aRes;
81 Config_Properties::const_iterator aIt;
82 Config_Properties aProps = props();
83 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
84 Config_Prop* aProp = (*aIt);
85 if (aProp->type() != Config_Prop::Disabled)
86 aRes.push_back(aProp);
91 std::list<std::string> Config_PropManager::getSections()
93 // Return only non disabled sections
94 std::list<std::string> aSections;
95 Config_Properties::const_iterator aIt;
96 Config_Properties aProps = props();
97 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
98 const Config_Prop* aProp = (*aIt);
99 if (aProp->type() != Config_Prop::Disabled)
100 aSections.push_back(aProp->section());
106 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
108 Config_Properties aRes;
109 Config_Properties::iterator aIt;
110 Config_Properties aProps = props();
111 for (aIt = aProps.begin(); aIt != aProps.end(); aIt++) {
112 Config_Prop* aProp = (*aIt);
113 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
114 aRes.push_back(aProp);
119 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName)
121 Config_Properties aProps = getProperties(theSection);
122 Config_Properties::const_iterator aIt;
123 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
124 Config_Prop* aProp = (*aIt);
125 if (aProp->name() == theName)
126 return aProp->value();
128 std::string aMsg = "Property " + theSection + ":" + theName + " is not registered";
132 std::vector<int> Config_PropManager::color(const std::string& theSection,
133 const std::string& theName)
135 std::string aStr = string(theSection, theName);
136 return stringToRGB(aStr);
139 int Config_PropManager::integer(const std::string& theSection, const std::string& theName)
141 std::string aStr = string(theSection, theName);
142 return stringToInteger(aStr);
145 double Config_PropManager::real(const std::string& theSection, const std::string& theName)
147 std::string aStr = string(theSection, theName);
148 return stringToDouble(aStr);
151 bool Config_PropManager::boolean(const std::string& theSection,
152 const std::string& theName)
154 std::string aStr = string(theSection, theName);
155 return stringToBoolean(aStr);
158 std::vector<int> stringToRGB(const std::string& theColor)
160 std::vector<int> aRes(3);
162 if (theColor[0] == '#') {
167 aBuf[0] = theColor[1];
168 aBuf[1] = theColor[2];
169 aRes[0] = strtol(aBuf, &aP, 16);
171 aBuf[0] = theColor[3];
172 aBuf[1] = theColor[4];
173 aRes[1] = strtol(aBuf, &aP, 16);
175 aBuf[0] = theColor[5];
176 aBuf[1] = theColor[6];
177 aRes[2] = strtol(aBuf, &aP, 16);
179 int aPos = (int)theColor.find(",");
182 std::size_t length = theColor.copy(aBuf, aPos, 0);
184 aRes[0] = atoi(aBuf);
187 int aNPos = (int)theColor.find(",", aPos + 1);
188 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
190 aRes[1] = atoi(aBuf);
193 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
195 aRes[2] = atoi(aBuf);
200 int stringToInteger(const std::string& theInt)
202 return atoi(theInt.c_str());
205 double Config_PropManager::stringToDouble(const std::string& theDouble)
207 std::string aStr = theDouble;
209 // change locale and convert "," to "." if exists
210 std::string aCurLocale = setlocale(LC_NUMERIC, 0);
211 setlocale(LC_NUMERIC, "C");
212 size_t dotpos = aStr.find(',');
213 if (dotpos != std::string::npos)
214 aStr.replace(dotpos, 1, ".");
217 double aValue = strtod(aStr.c_str(), &p);
220 setlocale(LC_NUMERIC, aCurLocale.c_str());
224 bool stringToBoolean(const std::string& theBoolean)
226 return theBoolean == "true";