Salome HOME
b4e2e5f5abfe91bb3df78609f06f864917174c8b
[modules/shaper.git] / src / Config / Config_Common.h
1 /*
2  * Config_Common.h
3  *
4  *  Created on: Apr 17, 2014
5  *      Author: sbh
6  */
7
8 #include <libxml/parser.h>
9 #include <libxml/tree.h>
10
11 //>> Forward declaration of xmlNodePtr.
12 typedef struct _xmlNode xmlNode;
13 typedef xmlNode *xmlNodePtr;
14 struct _xmlNode;
15 //<<
16
17 //>> Forward declaration of xmlDocPtr.
18 typedef struct _xmlDoc xmlDoc;
19 typedef xmlDoc *xmlDocPtr;
20 struct _xmlDoc;
21 //<<
22
23 /*
24  * Returns true if theNode is XML ELEMENT node (not a "text" node ie).
25  */
26 static bool isElementNode(xmlNodePtr theNode)
27 {
28   return theNode->type == XML_ELEMENT_NODE;
29 }
30
31 /*
32  * Returns true if theNode is XML node with a given name.
33
34  * Please note that this function should be called with NULL last argument.
35  * In example: isNode(aNode, "type1", ["type2", ...], NULL);
36  * ", NULL" is required to use unlimited number of arguments.
37  * TODO(sbh): find a way to simplify calling this method.
38  */
39 static bool isNode(xmlNodePtr theNode, const char* theNodeName, ...)
40 {
41   bool result = false;
42   const xmlChar* aName = theNode->name;
43   if (!aName || !isElementNode(theNode)) {
44     return false;
45   }
46   if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
47     return true;
48   }
49   va_list args; // define argument list variable
50   va_start(args, theNodeName); // init list; point to last defined argument
51   while(true) {
52     char *anArg = va_arg (args, char*); // get next argument
53     if (anArg == NULL)
54       break;
55     if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
56       va_end(args); // cleanup the system stack
57       return true;
58     }
59   }
60   va_end(args); // cleanup the system stack
61   return false;
62 }
63
64
65 /*
66  * Every xml node has child. Even if there is no explicit
67  * child nodes libxml gives the "Text node" as child.
68  *
69  * This method checks if real child nodes exist in the
70  * given node.
71  */
72 static bool hasChild(xmlNodePtr theNode)
73 {
74   xmlNodePtr aNode = theNode->children;
75   for(; aNode; aNode = aNode->next) {
76     if (isElementNode(theNode)) {
77       return true;
78     }
79   }
80   return false;
81 }