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