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