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::myProps;
30 Config_Prop* Config_PropManager::registerProp(const std::string& theSection,
31 const std::string& theName,
32 const std::string& theTitle,
33 Config_Prop::PropType theType,
34 const std::string& theDefaultValue)
36 Config_Prop* aProp = findProp(theSection, theName);
39 if (aProp->value() == "") {
40 aProp->setValue(theDefaultValue);
42 if (aProp->defaultValue() == "") {
43 aProp->setDefaultValue(theDefaultValue);
45 if (aProp->type() == Config_Prop::Disabled) {
46 aProp->setType(theType);
47 aProp->setTitle(theTitle);
51 aProp = new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue);
52 myProps.push_back(aProp);
57 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
59 Config_Properties::const_iterator aIt;
60 for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) {
61 Config_Prop* aProp = (*aIt);
62 if ((aProp->section() == theSection) && (aProp->name() == theName))
68 Config_Properties Config_PropManager::getProperties()
70 Config_Properties aRes;
71 Config_Properties::const_iterator aIt;
72 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
73 Config_Prop* aProp = (*aIt);
74 if (aProp->type() != Config_Prop::Disabled)
75 aRes.push_back(aProp);
80 std::list<std::string> Config_PropManager::getSections()
82 // Return only non disabled sections
83 std::list<std::string> aSections;
84 Config_Properties::const_iterator aIt;
85 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
86 const Config_Prop* aProp = (*aIt);
87 if (aProp->type() != Config_Prop::Disabled)
88 aSections.push_back(aProp->section());
94 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
96 Config_Properties aRes;
97 Config_Properties::iterator aIt;
98 for (aIt = myProps.begin(); aIt != myProps.end(); aIt++) {
99 Config_Prop* aProp = (*aIt);
100 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
101 aRes.push_back(aProp);
106 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName)
108 Config_Properties aProps = getProperties(theSection);
109 Config_Properties::const_iterator aIt;
110 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
111 Config_Prop* aProp = (*aIt);
112 if (aProp->name() == theName)
113 return aProp->value();
115 std::string aMsg = "Property " + theSection + ":" + theName + " is not registered";
119 std::vector<int> Config_PropManager::color(const std::string& theSection,
120 const std::string& theName)
122 std::string aStr = string(theSection, theName);
123 return stringToRGB(aStr);
126 int Config_PropManager::integer(const std::string& theSection, const std::string& theName)
128 std::string aStr = string(theSection, theName);
129 return stringToInteger(aStr);
132 double Config_PropManager::real(const std::string& theSection, const std::string& theName)
134 std::string aStr = string(theSection, theName);
135 return stringToDouble(aStr);
138 bool Config_PropManager::boolean(const std::string& theSection,
139 const std::string& theName)
141 std::string aStr = string(theSection, theName);
142 return stringToBoolean(aStr);
145 std::vector<int> stringToRGB(const std::string& theColor)
147 std::vector<int> aRes(3);
149 if (theColor[0] == '#') {
154 aBuf[0] = theColor[1];
155 aBuf[1] = theColor[2];
156 aRes[0] = strtol(aBuf, &aP, 16);
158 aBuf[0] = theColor[3];
159 aBuf[1] = theColor[4];
160 aRes[1] = strtol(aBuf, &aP, 16);
162 aBuf[0] = theColor[5];
163 aBuf[1] = theColor[6];
164 aRes[2] = strtol(aBuf, &aP, 16);
166 int aPos = (int)theColor.find(",");
169 std::size_t length = theColor.copy(aBuf, aPos, 0);
171 aRes[0] = atoi(aBuf);
174 int aNPos = (int)theColor.find(",", aPos + 1);
175 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
177 aRes[1] = atoi(aBuf);
180 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
182 aRes[2] = atoi(aBuf);
187 int stringToInteger(const std::string& theInt)
189 return atoi(theInt.c_str());
192 double Config_PropManager::stringToDouble(const std::string& theDouble)
194 std::string aStr = theDouble;
196 // change locale and convert "," to "." if exists
197 std::string aCurLocale = setlocale(LC_NUMERIC, 0);
198 setlocale(LC_NUMERIC, "C");
199 int dotpos = (int)aStr.find(',');
200 if (dotpos != std::string::npos)
201 aStr.replace(dotpos, 1, ".");
204 double aValue = strtod(aStr.c_str(), &p);
207 setlocale(LC_NUMERIC, aCurLocale.c_str());
211 bool stringToBoolean(const std::string& theBoolean)
213 return theBoolean == "true";