1 // Copyright (C) 2014-2023 CEA, EDF
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 bool Config_PropManager::autoColorStatus = false;
24 std::vector<int> stringToRGB(const std::string& theColor);
25 int stringToInteger(const std::string& theInt);
26 bool stringToBoolean(const std::string& theInt);
28 Config_Properties& Config_PropManager::props() {
29 static Config_Properties* confProps = new Config_Properties();
33 bool Config_PropManager::getAutoColorStatus()
35 return Config_PropManager::autoColorStatus;
38 void Config_PropManager::setAutoColorStatus(const bool theValue)
40 Config_PropManager::autoColorStatus = theValue;
43 Config_Prop* Config_PropManager::registerProp(const std::string& theSection,
44 const std::string& theName,
45 const std::string& theTitle,
46 Config_Prop::PropType theType,
47 const std::string& theDefaultValue,
48 const std::string& theMin,
49 const std::string& theMax)
51 Config_Prop* aProp = findProp(theSection, theName);
54 if (aProp->value() == "") {
55 aProp->setValue(theDefaultValue);
57 if (aProp->defaultValue() == "") {
58 aProp->setDefaultValue(theDefaultValue);
60 if (aProp->type() == Config_Prop::Disabled) {
61 aProp->setType(theType);
62 aProp->setTitle(theTitle);
65 aProp->setMin(theMin);
67 aProp->setMax(theMax);
71 new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue, theMin, theMax);
72 props().push_back(aProp);
77 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
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->section() == theSection) && (aProp->name() == theName))
89 Config_Properties Config_PropManager::getProperties()
91 Config_Properties aRes;
92 Config_Properties::const_iterator aIt;
93 Config_Properties aProps = props();
94 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
95 Config_Prop* aProp = (*aIt);
96 if (aProp->type() != Config_Prop::Disabled)
97 aRes.push_back(aProp);
102 std::list<std::string> Config_PropManager::getSections()
104 // Return only non disabled sections
105 std::list<std::string> aSections;
106 Config_Properties::const_iterator aIt;
107 Config_Properties aProps = props();
108 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
109 const Config_Prop* aProp = (*aIt);
110 if (aProp->type() != Config_Prop::Disabled)
111 aSections.push_back(aProp->section());
117 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
119 Config_Properties aRes;
120 Config_Properties::iterator aIt;
121 Config_Properties aProps = props();
122 for (aIt = aProps.begin(); aIt != aProps.end(); aIt++) {
123 Config_Prop* aProp = (*aIt);
124 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
125 aRes.push_back(aProp);
130 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName)
132 Config_Properties aProps = getProperties(theSection);
133 Config_Properties::const_iterator aIt;
134 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
135 Config_Prop* aProp = (*aIt);
136 if (aProp->name() == theName)
137 return aProp->value();
139 std::string aMsg = "Property " + theSection + ":" + theName + " is not registered";
143 std::vector<int> Config_PropManager::color(const std::string& theSection,
144 const std::string& theName)
146 std::string aStr = string(theSection, theName);
147 return stringToRGB(aStr);
150 int Config_PropManager::integer(const std::string& theSection, const std::string& theName)
152 std::string aStr = string(theSection, theName);
153 return stringToInteger(aStr);
156 double Config_PropManager::real(const std::string& theSection, const std::string& theName)
158 std::string aStr = string(theSection, theName);
159 return stringToDouble(aStr);
162 bool Config_PropManager::boolean(const std::string& theSection,
163 const std::string& theName)
165 std::string aStr = string(theSection, theName);
166 return stringToBoolean(aStr);
169 std::vector<int> stringToRGB(const std::string& theColor)
171 std::vector<int> aRes(3);
173 if (theColor[0] == '#') {
178 aBuf[0] = theColor[1];
179 aBuf[1] = theColor[2];
180 aRes[0] = strtol(aBuf, &aP, 16);
182 aBuf[0] = theColor[3];
183 aBuf[1] = theColor[4];
184 aRes[1] = strtol(aBuf, &aP, 16);
186 aBuf[0] = theColor[5];
187 aBuf[1] = theColor[6];
188 aRes[2] = strtol(aBuf, &aP, 16);
190 int aPos = (int)theColor.find(",");
193 std::size_t length = theColor.copy(aBuf, aPos, 0);
195 aRes[0] = atoi(aBuf);
198 int aNPos = (int)theColor.find(",", aPos + 1);
199 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
201 aRes[1] = atoi(aBuf);
204 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
206 aRes[2] = atoi(aBuf);
211 int stringToInteger(const std::string& theInt)
213 return atoi(theInt.c_str());
216 double Config_PropManager::stringToDouble(const std::string& theDouble)
218 std::string aStr = theDouble;
220 // change locale and convert "," to "." if exists
221 std::string aCurLocale = setlocale(LC_NUMERIC, 0);
222 setlocale(LC_NUMERIC, "C");
223 size_t dotpos = aStr.find(',');
224 if (dotpos != std::string::npos)
225 aStr.replace(dotpos, 1, ".");
228 double aValue = strtod(aStr.c_str(), &p);
231 setlocale(LC_NUMERIC, aCurLocale.c_str());
235 bool stringToBoolean(const std::string& theBoolean)
237 return theBoolean == "true";