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