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