1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "Config_PropManager.h"
23 std::vector<int> stringToRGB(const std::string& theColor);
24 int stringToInteger(const std::string& theInt);
25 bool stringToBoolean(const std::string& theInt);
27 Config_Properties& Config_PropManager::props() {
28 static Config_Properties* confProps = new Config_Properties();
33 Config_Prop* Config_PropManager::registerProp(const std::string& theSection,
34 const std::string& theName,
35 const std::string& theTitle,
36 Config_Prop::PropType theType,
37 const std::string& theDefaultValue,
38 const std::string& theMin,
39 const std::string& theMax)
41 Config_Prop* aProp = findProp(theSection, theName);
44 if (aProp->value() == "") {
45 aProp->setValue(theDefaultValue);
47 if (aProp->defaultValue() == "") {
48 aProp->setDefaultValue(theDefaultValue);
50 if (aProp->type() == Config_Prop::Disabled) {
51 aProp->setType(theType);
52 aProp->setTitle(theTitle);
54 aProp->setMin(theMin);
55 aProp->setMax(theMax);
59 new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue, theMin, theMax);
60 props().push_back(aProp);
65 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
67 Config_Properties::const_iterator aIt;
68 Config_Properties aProps = props();
69 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
70 Config_Prop* aProp = (*aIt);
71 if ((aProp->section() == theSection) && (aProp->name() == theName))
77 Config_Properties Config_PropManager::getProperties()
79 Config_Properties aRes;
80 Config_Properties::const_iterator aIt;
81 Config_Properties aProps = props();
82 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
83 Config_Prop* aProp = (*aIt);
84 if (aProp->type() != Config_Prop::Disabled)
85 aRes.push_back(aProp);
90 std::list<std::string> Config_PropManager::getSections()
92 // Return only non disabled sections
93 std::list<std::string> aSections;
94 Config_Properties::const_iterator aIt;
95 Config_Properties aProps = props();
96 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
97 const Config_Prop* aProp = (*aIt);
98 if (aProp->type() != Config_Prop::Disabled)
99 aSections.push_back(aProp->section());
105 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
107 Config_Properties aRes;
108 Config_Properties::iterator aIt;
109 Config_Properties aProps = props();
110 for (aIt = aProps.begin(); aIt != aProps.end(); aIt++) {
111 Config_Prop* aProp = (*aIt);
112 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
113 aRes.push_back(aProp);
118 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName)
120 Config_Properties aProps = getProperties(theSection);
121 Config_Properties::const_iterator aIt;
122 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
123 Config_Prop* aProp = (*aIt);
124 if (aProp->name() == theName)
125 return aProp->value();
127 std::string aMsg = "Property " + theSection + ":" + theName + " is not registered";
131 std::vector<int> Config_PropManager::color(const std::string& theSection,
132 const std::string& theName)
134 std::string aStr = string(theSection, theName);
135 return stringToRGB(aStr);
138 int Config_PropManager::integer(const std::string& theSection, const std::string& theName)
140 std::string aStr = string(theSection, theName);
141 return stringToInteger(aStr);
144 double Config_PropManager::real(const std::string& theSection, const std::string& theName)
146 std::string aStr = string(theSection, theName);
147 return stringToDouble(aStr);
150 bool Config_PropManager::boolean(const std::string& theSection,
151 const std::string& theName)
153 std::string aStr = string(theSection, theName);
154 return stringToBoolean(aStr);
157 std::vector<int> stringToRGB(const std::string& theColor)
159 std::vector<int> aRes(3);
161 if (theColor[0] == '#') {
166 aBuf[0] = theColor[1];
167 aBuf[1] = theColor[2];
168 aRes[0] = strtol(aBuf, &aP, 16);
170 aBuf[0] = theColor[3];
171 aBuf[1] = theColor[4];
172 aRes[1] = strtol(aBuf, &aP, 16);
174 aBuf[0] = theColor[5];
175 aBuf[1] = theColor[6];
176 aRes[2] = strtol(aBuf, &aP, 16);
178 int aPos = (int)theColor.find(",");
181 std::size_t length = theColor.copy(aBuf, aPos, 0);
183 aRes[0] = atoi(aBuf);
186 int aNPos = (int)theColor.find(",", aPos + 1);
187 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
189 aRes[1] = atoi(aBuf);
192 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
194 aRes[2] = atoi(aBuf);
199 int stringToInteger(const std::string& theInt)
201 return atoi(theInt.c_str());
204 double Config_PropManager::stringToDouble(const std::string& theDouble)
206 std::string aStr = theDouble;
208 // change locale and convert "," to "." if exists
209 std::string aCurLocale = setlocale(LC_NUMERIC, 0);
210 setlocale(LC_NUMERIC, "C");
211 int dotpos = (int)aStr.find(',');
212 if (dotpos != std::string::npos)
213 aStr.replace(dotpos, 1, ".");
216 double aValue = strtod(aStr.c_str(), &p);
219 setlocale(LC_NUMERIC, aCurLocale.c_str());
223 bool stringToBoolean(const std::string& theBoolean)
225 return theBoolean == "true";