]> SALOME platform Git repositories - modules/shaper.git/blob - src/Config/Config_PropManager.cpp
Salome HOME
Update copyrights
[modules/shaper.git] / src / Config / Config_PropManager.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "Config_PropManager.h"
21
22 std::vector<int> stringToRGB(const std::string& theColor);
23 int stringToInteger(const std::string& theInt);
24 bool stringToBoolean(const std::string& theInt);
25
26 Config_Properties& Config_PropManager::props() {
27   static Config_Properties* confProps = new Config_Properties();
28   return *confProps;
29 }
30
31
32 Config_Prop* Config_PropManager::registerProp(const std::string& theSection,
33                                               const std::string& theName,
34                                               const std::string& theTitle,
35                                               Config_Prop::PropType theType,
36                                               const std::string& theDefaultValue,
37                                               const std::string& theMin,
38                                               const std::string& theMax)
39 {
40   Config_Prop* aProp = findProp(theSection, theName);
41
42   if (aProp) {
43     if (aProp->value() == "") {
44       aProp->setValue(theDefaultValue);
45     }
46     if (aProp->defaultValue() == "") {
47       aProp->setDefaultValue(theDefaultValue);
48     }
49     if (aProp->type() == Config_Prop::Disabled) {
50       aProp->setType(theType);
51       aProp->setTitle(theTitle);
52     }
53     aProp->setMin(theMin);
54     aProp->setMax(theMax);
55   }
56   else {
57     aProp =
58       new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue, theMin, theMax);
59     props().push_back(aProp);
60   }
61   return aProp;
62 }
63
64 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
65 {
66   Config_Properties::const_iterator aIt;
67   Config_Properties aProps = props();
68   for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
69     Config_Prop* aProp = (*aIt);
70     if ((aProp->section() == theSection) && (aProp->name() == theName))
71       return aProp;
72   }
73   return NULL;
74 }
75
76 Config_Properties Config_PropManager::getProperties()
77 {
78   Config_Properties aRes;
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->type() != Config_Prop::Disabled)
84       aRes.push_back(aProp);
85   }
86   return aRes;
87 }
88
89 std::list<std::string> Config_PropManager::getSections()
90 {
91   // Return only non disabled sections
92   std::list<std::string> aSections;
93   Config_Properties::const_iterator aIt;
94   Config_Properties aProps = props();
95   for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
96     const Config_Prop* aProp = (*aIt);
97     if (aProp->type() != Config_Prop::Disabled)
98       aSections.push_back(aProp->section());
99   }
100   aSections.unique();
101   return aSections;
102 }
103
104 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
105 {
106   Config_Properties aRes;
107   Config_Properties::iterator aIt;
108   Config_Properties aProps = props();
109   for (aIt = aProps.begin(); aIt != aProps.end(); aIt++) {
110     Config_Prop* aProp = (*aIt);
111     if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
112       aRes.push_back(aProp);
113   }
114   return aRes;
115 }
116
117 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName)
118 {
119   Config_Properties aProps = getProperties(theSection);
120   Config_Properties::const_iterator aIt;
121   for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
122     Config_Prop* aProp = (*aIt);
123     if (aProp->name() == theName)
124       return aProp->value();
125   }
126   std::string aMsg = "Property " + theSection + ":" + theName + " is not registered";
127   throw aMsg.c_str();
128 }
129
130 std::vector<int> Config_PropManager::color(const std::string& theSection,
131                                            const std::string& theName)
132 {
133   std::string aStr = string(theSection, theName);
134   return stringToRGB(aStr);
135 }
136
137 int Config_PropManager::integer(const std::string& theSection, const std::string& theName)
138 {
139   std::string aStr = string(theSection, theName);
140   return stringToInteger(aStr);
141 }
142
143 double Config_PropManager::real(const std::string& theSection, const std::string& theName)
144 {
145   std::string aStr = string(theSection, theName);
146   return stringToDouble(aStr);
147 }
148
149 bool Config_PropManager::boolean(const std::string& theSection,
150                                  const std::string& theName)
151 {
152   std::string aStr = string(theSection, theName);
153   return stringToBoolean(aStr);
154 }
155
156 std::vector<int> stringToRGB(const std::string& theColor)
157 {
158   std::vector<int> aRes(3);
159
160   if (theColor[0] == '#') {
161     char aBuf[3];
162     char* aP;
163     aBuf[2] = '\0';
164
165     aBuf[0] = theColor[1];
166     aBuf[1] = theColor[2];
167     aRes[0] = strtol(aBuf, &aP, 16);
168
169     aBuf[0] = theColor[3];
170     aBuf[1] = theColor[4];
171     aRes[1] = strtol(aBuf, &aP, 16);
172
173     aBuf[0] = theColor[5];
174     aBuf[1] = theColor[6];
175     aRes[2] = strtol(aBuf, &aP, 16);
176   } else {
177     int aPos = (int)theColor.find(",");
178     char aBuf[10];
179     // Get Red
180     std::size_t length = theColor.copy(aBuf, aPos, 0);
181     aBuf[length] = '\0';
182     aRes[0] = atoi(aBuf);
183
184     // Get Green
185     int aNPos = (int)theColor.find(",", aPos + 1);
186     length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
187     aBuf[length] = '\0';
188     aRes[1] = atoi(aBuf);
189
190     // Get Green
191     length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
192     aBuf[length] = '\0';
193     aRes[2] = atoi(aBuf);
194   }
195   return aRes;
196 }
197
198 int stringToInteger(const std::string& theInt)
199 {
200   return atoi(theInt.c_str());
201 }
202
203 double Config_PropManager::stringToDouble(const std::string& theDouble)
204 {
205   std::string aStr = theDouble;
206
207   // change locale and convert "," to "." if exists
208   std::string aCurLocale = setlocale(LC_NUMERIC, 0);
209   setlocale(LC_NUMERIC, "C");
210   int dotpos = (int)aStr.find(',');
211   if (dotpos != std::string::npos)
212     aStr.replace(dotpos, 1, ".");
213
214   char* p;
215   double aValue = strtod(aStr.c_str(), &p);
216
217   // restore locale
218   setlocale(LC_NUMERIC, aCurLocale.c_str());
219   return aValue;
220 }
221
222 bool stringToBoolean(const std::string& theBoolean)
223 {
224   return theBoolean == "true";
225 }