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