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