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