Salome HOME
Issue #355 Delete: elements of sketch and constraints
[modules/shaper.git] / src / Config / Config_Common.cpp
index a6a9ee512e055782f0b7ff46968d63088633ab44..9c4df7f57c539174f1496979685e7d8b48c113e9 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D\r
+\r
 /*\r
  * Config_Common.cpp\r
  *\r
 #include <libxml/parser.h>\r
 #include <libxml/tree.h>\r
 \r
-#include <sstream> //for stringstream\r\r
+#include <sstream> // for stringstream\r
+\r
+#include <string>\r
+#include <algorithm> // for std::transform\r
+\r
 bool isElementNode(xmlNodePtr theNode)\r
 {\r
+  if (!theNode)\r
+    return false;\r
   return theNode->type == XML_ELEMENT_NODE;\r
 }\r
 \r
@@ -42,6 +50,19 @@ bool isNode(xmlNodePtr theNode, const char* theNodeName, ...)
   return false;\r
 }\r
 \r
+bool isWidgetNode(xmlNodePtr theNode)\r
+{\r
+  if(!isElementNode(theNode))\r
+    return false;\r
+  // it's parent is "feature" or "source"\r
+  xmlNodePtr aParentNode = theNode->parent;\r
+  if(!isNode(aParentNode, NODE_FEATURE, NODE_SOURCE, NULL))\r
+    return false;\r
+\r
+  //it should not be a "source" or a "validator" node\r
+  return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NODE_SELFILTER, NULL);\r
+}\r
+\r
 bool hasChild(xmlNodePtr theNode)\r
 {\r
   xmlNodePtr aNode = theNode->children;\r
@@ -53,25 +74,25 @@ bool hasChild(xmlNodePtr theNode)
   return false;\r
 }\r
 \r
-bool getValidatorInfo(xmlNodePtr theNode, std::string& outValidatorId,\r
+bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,\r
                       std::list<std::string>& outValidatorParameters)\r
 {\r
-  //Validator id:\r
+  //Property id:\r
   char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);\r
   if (!anIdProp || anIdProp[0] == 0) {\r
     return false;\r
   }\r
-  outValidatorId = std::string(anIdProp);\r
+  outPropertyId = std::string(anIdProp);\r
 \r
-  //Validator parameters:\r
-  char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST VALIDATOR_PARAMETERS);\r
+  //Property parameters:\r
+  char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST _PARAMETERS);\r
   if (aParamProp && aParamProp[0] != 0) {\r
     std::string aPropString = std::string(aParamProp);\r
     std::stringstream aPropStringStream(aPropString);\r
     char COMMA_DELIM = ',';\r
-    std::string aValidatorParameter;\r
-    while (std::getline(aPropStringStream, aValidatorParameter, ',')) {\r
-      outValidatorParameters.push_back(aValidatorParameter);\r
+    std::string aParameter;\r
+    while (std::getline(aPropStringStream, aParameter, ',')) {\r
+      outValidatorParameters.push_back(aParameter);\r
     }\r
   }\r
   return true;\r
@@ -79,6 +100,8 @@ bool getValidatorInfo(xmlNodePtr theNode, std::string& outValidatorId,
 \r
 std::string library(const std::string& theLibName)\r
 {\r
+  if(theLibName.empty())\r
+    return std::string();\r
   std::string aLibName = theLibName;\r
 #ifndef WIN32\r
   static std::string aLibExt( ".so" );\r
@@ -94,3 +117,44 @@ std::string library(const std::string& theLibName)
 \r
   return aLibName;\r
 }\r
+\r
+std::string getProperty(xmlNodePtr theNode, const char* thePropName)\r
+{\r
+  std::string result = "";\r
+  char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST thePropName);\r
+  if (!aPropChars || aPropChars[0] == 0)\r
+    return result;\r
+  result = std::string(aPropChars);\r
+  return result;\r
+}\r
+\r
+std::string getNormalizedProperty(xmlNodePtr theNode, const char* thePropName)\r
+{\r
+  return normalize(getProperty(theNode, thePropName));\r
+}\r
+\r
+bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)\r
+{\r
+  std::string prop = normalize(getProperty(theNode, theAttributeName));\r
+  bool result = theDefault;\r
+  if (prop == "true" || prop == "1") {\r
+    result = true;\r
+  } else if (prop == "false" || prop == "0") {\r
+    result = false;\r
+  }\r
+  return result;\r
+}\r
+\r
+CONFIG_EXPORT std::string normalize(const char* theString)\r
+{\r
+  if (!theString)\r
+    return std::string();\r
+  return normalize(std::string(theString));\r
+}\r
+\r
+CONFIG_EXPORT std::string normalize(const std::string& theString)\r
+{\r
+  std::string result = theString;\r
+  std::transform(result.begin(), result.end(), result.begin(), ::tolower);\r
+  return result;\r
+}\r