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