Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / Config / Config_PropManager.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "Config_PropManager.h"
22
23 std::vector<int> stringToRGB(const std::string& theColor);
24 int stringToInteger(const std::string& theInt);
25 bool stringToBoolean(const std::string& theInt);
26
27 Config_Properties& Config_PropManager::props() {
28   static Config_Properties* confProps = new Config_Properties();
29   return *confProps;
30 }
31
32
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)
40 {
41   Config_Prop* aProp = findProp(theSection, theName);
42
43   if (aProp) {
44     if (aProp->value() == "") {
45       aProp->setValue(theDefaultValue);
46     }
47     if (aProp->defaultValue() == "") {
48       aProp->setDefaultValue(theDefaultValue);
49     }
50     if (aProp->type() == Config_Prop::Disabled) {
51       aProp->setType(theType);
52       aProp->setTitle(theTitle);
53     }
54     aProp->setMin(theMin);
55     aProp->setMax(theMax);
56   }
57   else {
58     aProp =
59       new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue, theMin, theMax);
60     props().push_back(aProp);
61   }
62   return aProp;
63 }
64
65 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
66 {
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))
72       return aProp;
73   }
74   return NULL;
75 }
76
77 Config_Properties Config_PropManager::getProperties()
78 {
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);
86   }
87   return aRes;
88 }
89
90 std::list<std::string> Config_PropManager::getSections()
91 {
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());
100   }
101   aSections.unique();
102   return aSections;
103 }
104
105 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
106 {
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);
114   }
115   return aRes;
116 }
117
118 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName)
119 {
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();
126   }
127   std::string aMsg = "Property " + theSection + ":" + theName + " is not registered";
128   throw aMsg.c_str();
129 }
130
131 std::vector<int> Config_PropManager::color(const std::string& theSection,
132                                            const std::string& theName)
133 {
134   std::string aStr = string(theSection, theName);
135   return stringToRGB(aStr);
136 }
137
138 int Config_PropManager::integer(const std::string& theSection, const std::string& theName)
139 {
140   std::string aStr = string(theSection, theName);
141   return stringToInteger(aStr);
142 }
143
144 double Config_PropManager::real(const std::string& theSection, const std::string& theName)
145 {
146   std::string aStr = string(theSection, theName);
147   return stringToDouble(aStr);
148 }
149
150 bool Config_PropManager::boolean(const std::string& theSection,
151                                  const std::string& theName)
152 {
153   std::string aStr = string(theSection, theName);
154   return stringToBoolean(aStr);
155 }
156
157 std::vector<int> stringToRGB(const std::string& theColor)
158 {
159   std::vector<int> aRes(3);
160
161   if (theColor[0] == '#') {
162     char aBuf[3];
163     char* aP;
164     aBuf[2] = '\0';
165
166     aBuf[0] = theColor[1];
167     aBuf[1] = theColor[2];
168     aRes[0] = strtol(aBuf, &aP, 16);
169
170     aBuf[0] = theColor[3];
171     aBuf[1] = theColor[4];
172     aRes[1] = strtol(aBuf, &aP, 16);
173
174     aBuf[0] = theColor[5];
175     aBuf[1] = theColor[6];
176     aRes[2] = strtol(aBuf, &aP, 16);
177   } else {
178     int aPos = (int)theColor.find(",");
179     char aBuf[10];
180     // Get Red
181     std::size_t length = theColor.copy(aBuf, aPos, 0);
182     aBuf[length] = '\0';
183     aRes[0] = atoi(aBuf);
184
185     // Get Green
186     int aNPos = (int)theColor.find(",", aPos + 1);
187     length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
188     aBuf[length] = '\0';
189     aRes[1] = atoi(aBuf);
190
191     // Get Green
192     length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
193     aBuf[length] = '\0';
194     aRes[2] = atoi(aBuf);
195   }
196   return aRes;
197 }
198
199 int stringToInteger(const std::string& theInt)
200 {
201   return atoi(theInt.c_str());
202 }
203
204 double Config_PropManager::stringToDouble(const std::string& theDouble)
205 {
206   std::string aStr = theDouble;
207
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, ".");
214
215   char* p;
216   double aValue = strtod(aStr.c_str(), &p);
217
218   // restore locale
219   setlocale(LC_NUMERIC, aCurLocale.c_str());
220   return aValue;
221 }
222
223 bool stringToBoolean(const std::string& theBoolean)
224 {
225   return theBoolean == "true";
226 }