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 email : webmaster.salome@opencascade.com<mailto: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::myProps;
29 Config_Prop* Config_PropManager::registerProp(const std::string& theSection,
30 const std::string& theName,
31 const std::string& theTitle,
32 Config_Prop::PropType theType,
33 const std::string& theDefaultValue)
35 Config_Prop* aProp = findProp(theSection, theName);
38 if (aProp->value() == "") {
39 aProp->setValue(theDefaultValue);
41 if (aProp->defaultValue() == "") {
42 aProp->setDefaultValue(theDefaultValue);
44 if (aProp->type() == Config_Prop::Disabled) {
45 aProp->setType(theType);
46 aProp->setTitle(theTitle);
50 aProp = new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue);
51 myProps.push_back(aProp);
56 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
58 Config_Properties::const_iterator aIt;
59 for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) {
60 Config_Prop* aProp = (*aIt);
61 if ((aProp->section() == theSection) && (aProp->name() == theName))
67 Config_Properties Config_PropManager::getProperties()
69 Config_Properties aRes;
70 Config_Properties::const_iterator aIt;
71 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
72 Config_Prop* aProp = (*aIt);
73 if (aProp->type() != Config_Prop::Disabled)
74 aRes.push_back(aProp);
79 std::list<std::string> Config_PropManager::getSections()
81 // Return only non disabled sections
82 std::list<std::string> aSections;
83 Config_Properties::const_iterator aIt;
84 for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
85 const Config_Prop* aProp = (*aIt);
86 if (aProp->type() != Config_Prop::Disabled)
87 aSections.push_back(aProp->section());
93 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
95 Config_Properties aRes;
96 Config_Properties::iterator aIt;
97 for (aIt = myProps.begin(); aIt != myProps.end(); aIt++) {
98 Config_Prop* aProp = (*aIt);
99 if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
100 aRes.push_back(aProp);
105 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName)
107 Config_Properties aProps = getProperties(theSection);
108 Config_Properties::const_iterator aIt;
109 for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
110 Config_Prop* aProp = (*aIt);
111 if (aProp->name() == theName)
112 return aProp->value();
114 std::string aMsg = "Property " + theSection + ":" + theName + " is not registered";
118 std::vector<int> Config_PropManager::color(const std::string& theSection,
119 const std::string& theName)
121 std::string aStr = string(theSection, theName);
122 return stringToRGB(aStr);
125 int Config_PropManager::integer(const std::string& theSection, const std::string& theName)
127 std::string aStr = string(theSection, theName);
128 return stringToInteger(aStr);
131 double Config_PropManager::real(const std::string& theSection, const std::string& theName)
133 std::string aStr = string(theSection, theName);
134 return stringToDouble(aStr);
137 bool Config_PropManager::boolean(const std::string& theSection,
138 const std::string& theName)
140 std::string aStr = string(theSection, theName);
141 return stringToBoolean(aStr);
144 std::vector<int> stringToRGB(const std::string& theColor)
146 std::vector<int> aRes(3);
148 if (theColor[0] == '#') {
153 aBuf[0] = theColor[1];
154 aBuf[1] = theColor[2];
155 aRes[0] = strtol(aBuf, &aP, 16);
157 aBuf[0] = theColor[3];
158 aBuf[1] = theColor[4];
159 aRes[1] = strtol(aBuf, &aP, 16);
161 aBuf[0] = theColor[5];
162 aBuf[1] = theColor[6];
163 aRes[2] = strtol(aBuf, &aP, 16);
165 int aPos = (int)theColor.find(",");
168 std::size_t length = theColor.copy(aBuf, aPos, 0);
170 aRes[0] = atoi(aBuf);
173 int aNPos = (int)theColor.find(",", aPos + 1);
174 length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
176 aRes[1] = atoi(aBuf);
179 length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
181 aRes[2] = atoi(aBuf);
186 int stringToInteger(const std::string& theInt)
188 return atoi(theInt.c_str());
191 double Config_PropManager::stringToDouble(const std::string& theDouble)
193 std::string aStr = theDouble;
195 // change locale and convert "," to "." if exists
196 std::string aCurLocale = setlocale(LC_NUMERIC, 0);
197 setlocale(LC_NUMERIC, "C");
198 int dotpos = (int)aStr.find(',');
199 if (dotpos != std::string::npos)
200 aStr.replace(dotpos, 1, ".");
203 double aValue = strtod(aStr.c_str(), &p);
206 setlocale(LC_NUMERIC, aCurLocale.c_str());
210 bool stringToBoolean(const std::string& theBoolean)
212 return theBoolean == "true";