Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom.git into Dev_0.6.1
[modules/shaper.git] / src / Config / Config_Common.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D\r
2 \r
3 /*\r
4  * Config_Common.cpp\r
5  *\r
6  *  Created on: Apr 17, 2014\r
7  *      Author: sbh\r
8  */\r
9 \r
10 #include "Config_Common.h"\r
11 #include <Config_Keywords.h>\r
12 \r
13 #include <libxml/parser.h>\r
14 #include <libxml/tree.h>\r
15 \r
16 #include <sstream> // for stringstream\r
17 \r
18 #include <string>\r
19 #include <algorithm> // for std::transform\r
20 \r
21 bool isElementNode(xmlNodePtr theNode)\r
22 {\r
23   if (!theNode)\r
24     return false;\r
25   return theNode->type == XML_ELEMENT_NODE;\r
26 }\r
27 \r
28 bool isNode(xmlNodePtr theNode, const char* theNodeName, ...)\r
29 {\r
30   bool result = false;\r
31   const xmlChar* aName = theNode->name;\r
32   if (!aName || !isElementNode(theNode)) {\r
33     return false;\r
34   }\r
35   if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {\r
36     return true;\r
37   }\r
38   va_list args;  // define argument list variable\r
39   va_start(args, theNodeName);  // init list; point to last defined argument\r
40   while (true) {\r
41     char *anArg = va_arg (args, char*);  // get next argument\r
42     if (anArg == NULL)\r
43       break;\r
44     if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {\r
45       va_end(args);  // cleanup the system stack\r
46       return true;\r
47     }\r
48   }\r
49   va_end(args);  // cleanup the system stack\r
50   return false;\r
51 }\r
52 \r
53 bool isWidgetNode(xmlNodePtr theNode)\r
54 {\r
55   if(!isElementNode(theNode))\r
56     return false;\r
57   // it's parent is "feature" or "source"\r
58   xmlNodePtr aParentNode = theNode->parent;\r
59   if(!isNode(aParentNode, NODE_FEATURE, NODE_SOURCE, NULL))\r
60     return false;\r
61 \r
62   //it should not be a "source" or a "validator" node\r
63   return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NODE_SELFILTER, NULL);\r
64 }\r
65 \r
66 bool hasChild(xmlNodePtr theNode)\r
67 {\r
68   xmlNodePtr aNode = theNode->children;\r
69   for (; aNode; aNode = aNode->next) {\r
70     if (isElementNode(theNode)) {\r
71       return true;\r
72     }\r
73   }\r
74   return false;\r
75 }\r
76 \r
77 bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,\r
78                       std::list<std::string>& outValidatorParameters)\r
79 {\r
80   //Property id:\r
81   char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);\r
82   if (!anIdProp || anIdProp[0] == 0) {\r
83     return false;\r
84   }\r
85   outPropertyId = std::string(anIdProp);\r
86 \r
87   //Property parameters:\r
88   char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST _PARAMETERS);\r
89   if (aParamProp && aParamProp[0] != 0) {\r
90     std::string aPropString = std::string(aParamProp);\r
91     std::stringstream aPropStringStream(aPropString);\r
92     char COMMA_DELIM = ',';\r
93     std::string aParameter;\r
94     while (std::getline(aPropStringStream, aParameter, ',')) {\r
95       outValidatorParameters.push_back(aParameter);\r
96     }\r
97   }\r
98   return true;\r
99 }\r
100 \r
101 std::string library(const std::string& theLibName)\r
102 {\r
103   if(theLibName.empty())\r
104     return std::string();\r
105   std::string aLibName = theLibName;\r
106 #ifndef WIN32\r
107   static std::string aLibExt( ".so" );\r
108   if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {\r
109     aLibName = "lib" + aLibName;\r
110   }\r
111 #else\r
112   static std::string aLibExt(".dll");\r
113 #endif\r
114   std::string anExt = aLibName.substr(aLibName.size() - 4);\r
115   if (anExt != aLibExt)\r
116     aLibName += aLibExt;\r
117 \r
118   return aLibName;\r
119 }\r
120 \r
121 std::string getProperty(xmlNodePtr theNode, const char* thePropName)\r
122 {\r
123   std::string result = "";\r
124   char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST thePropName);\r
125   if (!aPropChars || aPropChars[0] == 0)\r
126     return result;\r
127   result = std::string(aPropChars);\r
128   return result;\r
129 }\r
130 \r
131 bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)\r
132 {\r
133   std::string prop = getProperty(theNode, theAttributeName);\r
134   std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);\r
135   bool result = theDefault;\r
136   if (prop == "true" || prop == "1") {\r
137     result = true;\r
138   } else if (prop == "false" || prop == "0") {\r
139     result = false;\r
140   }\r
141   return result;\r
142 }\r