Salome HOME
updated copyright message
[modules/shaper.git] / src / Config / Config_Common.cpp
index 6d89535e7edf6a6553264933b25ff86c0381e338..a4b68bbf829ea9cdeeb64a629dc04bb377d82033 100644 (file)
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D\r
-\r
-/*\r
- * Config_Common.cpp\r
- *\r
- *  Created on: Apr 17, 2014\r
- *      Author: sbh\r
- */\r
-\r
-#include "Config_Common.h"\r
-#include <Config_Keywords.h>\r
-\r
-#include <libxml/parser.h>\r
-#include <libxml/tree.h>\r
-\r
-#include <sstream> // for stringstream\r
-\r
-#include <string>\r
-#include <algorithm> // for std::transform\r
-#include <vector>\r
-\r
-bool isElementNode(xmlNodePtr theNode)\r
-{\r
-  if (!theNode)\r
-    return false;\r
-  return theNode->type == XML_ELEMENT_NODE;\r
-}\r
-\r
-bool isNode(xmlNodePtr theNode, const char* theNodeName, ...)\r
-{\r
-  const xmlChar* aName = theNode->name;\r
-  if (!aName || !isElementNode(theNode)) {\r
-    return false;\r
-  }\r
-  if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {\r
-    return true;\r
-  }\r
-  va_list args;  // define argument list variable\r
-  va_start(args, theNodeName);  // init list; point to last defined argument\r
-  while (true) {\r
-    char *anArg = va_arg (args, char*);  // get next argument\r
-    if (anArg == NULL)\r
-      break;\r
-    if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {\r
-      va_end(args);  // cleanup the system stack\r
-      return true;\r
-    }\r
-  }\r
-  va_end(args);  // cleanup the system stack\r
-  return false;\r
-}\r
-\r
-bool isAttributeNode(xmlNodePtr theNode)\r
-{\r
-  if(!isElementNode(theNode))\r
-    return false;\r
-  // it's parent is "feature" or "source" or page ("case" or "box")\r
-  if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE, \r
-                         WDG_GROUP, WDG_OPTIONALBOX,\r
-                         WDG_TOOLBOX_BOX, WDG_SWITCH_CASE, NULL))\r
-    return false;\r
-\r
-  //it should not be a "source" or a "validator" node\r
-  bool isLogical = isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NODE_SELFILTER, NULL);\r
-  bool isPagedContainer = isNode(theNode, WDG_TOOLBOX, WDG_TOOLBOX_BOX,\r
-                                          WDG_GROUP, WDG_OPTIONALBOX,\r
-                                          WDG_SWITCH, WDG_SWITCH_CASE,  NULL);\r
-  return !isLogical && !isPagedContainer;\r
-}\r
-\r
-bool isWidgetNode(xmlNodePtr theNode)\r
-{\r
-  if(!isElementNode(theNode))\r
-    return false;\r
-  // it's parent is "feature" or "source" or a page ("box", "case")\r
-  if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE, WDG_GROUP, WDG_OPTIONALBOX,\r
-                         WDG_TOOLBOX_BOX, WDG_SWITCH_CASE, 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
-// widget api?\r
-bool isCaseNode(xmlNodePtr theNode)\r
-{\r
-  if(!isElementNode(theNode))\r
-    return false;\r
-\r
-  return isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, NULL);\r
-}\r
-\r
-bool hasChild(xmlNodePtr theNode)\r
-{\r
-  xmlNodePtr aNode = theNode->children;\r
-  for (; aNode; aNode = aNode->next) {\r
-    if (isElementNode(theNode)) {\r
-      return true;\r
-    }\r
-  }\r
-  return false;\r
-}\r
-\r
-bool hasParent(xmlNodePtr theNode)\r
-{\r
-  xmlNodePtr aNode = theNode->parent;\r
-  if (!aNode) {\r
-    return false;\r
-  }\r
-  for (; aNode; aNode = aNode->next) {\r
-    if (isElementNode(theNode)) {\r
-      return true;\r
-    }\r
-  }\r
-  return false;\r
-}\r
-\r
-bool hasParent(xmlNodePtr theNode, const char* theNodeName, ...)\r
-{\r
-  if (!hasParent(theNode)) {\r
-    return false; // have no parents at all\r
-  }\r
-  xmlNodePtr aNode = theNode->parent;\r
-  const xmlChar* aName = aNode->name;\r
-  if (!aName || !isElementNode(aNode)) {\r
-    return false;\r
-  }\r
-  if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {\r
-    return true;\r
-  }\r
-  va_list args;  // define argument list variable\r
-  va_start(args, theNodeName);  // init list; point to last defined argument\r
-  while (true) {\r
-    char *anArg = va_arg (args, char*);  // get next argument\r
-    if (anArg == NULL)\r
-      break;\r
-    if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {\r
-      va_end(args);  // cleanup the system stack\r
-      return true;\r
-    }\r
-  }\r
-  va_end(args);  // cleanup the system stack\r
-  return false;\r
-}\r
-\r
-xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const std::vector<const char*>& theNodeNames)\r
-{\r
-  if (!hasParent(theNode)) {\r
-    return 0; // have no parents at all\r
-  }\r
-  xmlNodePtr aNode = theNode->parent;\r
-  const xmlChar* aName = aNode->name;\r
-  if (!aName || !isElementNode(aNode)) {\r
-    return 0;\r
-  }\r
-  for (size_t anIndex = 0; anIndex < theNodeNames.size(); ++anIndex) {\r
-    if (!xmlStrcmp(aName, (const xmlChar *) theNodeNames[anIndex]))\r
-      return aNode;\r
-  }\r
-  return hasParentRecursive(aNode, theNodeNames);\r
-}\r
-\r
-xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const char* theNodeName, ...)\r
-{\r
-  std::vector<const char*> aNodeNames;\r
-  va_list args;  // define argument list variable\r
-  va_start(args, theNodeName);  // init list; point to last defined argument\r
-  aNodeNames.push_back(theNodeName);\r
-  while (true) {\r
-    char *anArg = va_arg (args, char*);  // get next argument\r
-    if (anArg == NULL)\r
-      break;\r
-    aNodeNames.push_back(anArg);\r
-  }\r
-  va_end(args);  // cleanup the system stack\r
-  return hasParentRecursive(theNode, aNodeNames);\r
-}\r
-\r
-bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,\r
-                      std::list<std::string>& outValidatorParameters)\r
-{\r
-  //Property id:\r
-  char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);\r
-  if (!anIdProp || anIdProp[0] == 0) {\r
-    return false;\r
-  }\r
-  outPropertyId = std::string(anIdProp);\r
-\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 aParameter;\r
-    while (std::getline(aPropStringStream, aParameter, ',')) {\r
-      outValidatorParameters.push_back(aParameter);\r
-    }\r
-  }\r
-  return true;\r
-}\r
-\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
-  if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {\r
-    aLibName = "lib" + aLibName;\r
-  }\r
-#else\r
-  static std::string aLibExt(".dll");\r
-#endif\r
-  std::string anExt = aLibName.substr(aLibName.size() - 4);\r
-  if (anExt != aLibExt)\r
-    aLibName += aLibExt;\r
-\r
-  return aLibName;\r
-}\r
-\r
-bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ' '); }\r
-\r
-std::string getProperty(xmlNodePtr theNode, const char* thePropName)\r
-{\r
-  std::string result = "";\r
-  xmlChar* aPropChars = xmlGetProp(theNode, BAD_CAST thePropName);\r
-  if (!aPropChars || aPropChars[0] == 0)\r
-    return result;\r
-  result = std::string((char*)aPropChars);\r
-  xmlFree(aPropChars);\r
-\r
-  std::string::iterator new_end = std::unique(result.begin(), result.end(), BothAreSpaces);\r
-  result.erase(new_end, result.end()); \r
-\r
-  return result;\r
-}\r
-\r
-std::string getContent(xmlNodePtr theNode)\r
-{\r
-  std::string result = "";\r
-  xmlChar* aContent = xmlNodeGetContent(theNode);\r
-  if (!aContent || aContent[0] == 0)\r
-    return result;\r
-  result = std::string((char*)aContent);\r
-  xmlFree(aContent);\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
+// Copyright (C) 2014-2023  CEA, EDF
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+#include "Config_Common.h"
+#include <Config_Keywords.h>
+
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+
+#include <sstream> // for stringstream
+
+#include <string>
+#include <algorithm> // for std::transform
+#include <vector>
+
+bool isElementNode(xmlNodePtr theNode)
+{
+  if (!theNode)
+    return false;
+  return theNode->type == XML_ELEMENT_NODE;
+}
+
+bool isNode(xmlNodePtr theNode, const char* theNodeName, ...)
+{
+  const xmlChar* aName = theNode->name;
+  if (!aName || !isElementNode(theNode)) {
+    return false;
+  }
+  if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
+    return true;
+  }
+  va_list args;  // define argument list variable
+  va_start(args, theNodeName);  // init list; point to last defined argument
+  while (true) {
+    char *anArg = va_arg (args, char*);  // get next argument
+    if (anArg == NULL)
+      break;
+    if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
+      va_end(args);  // cleanup the system stack
+      return true;
+    }
+  }
+  va_end(args);  // cleanup the system stack
+  return false;
+}
+
+bool isAttributeNode(xmlNodePtr theNode)
+{
+  if(!isElementNode(theNode))
+    return false;
+  // it's parent is "feature" or "source" or page ("case" or "box")
+  if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE,
+                         WDG_GROUP, WDG_OPTIONALBOX,
+                         WDG_TOOLBOX_BOX, WDG_RADIOBOX_ITEM,
+                         WDG_SWITCH_CASE, NULL))
+    return false;
+
+  //it should not be a "source" or a "validator" node
+  bool isLogical = isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NULL);
+  // here must be only widgets not connected to attributes
+  bool isPagedContainer = isNode(theNode, WDG_TOOLBOX_BOX,
+                                          WDG_GROUP,
+                                          WDG_SWITCH_CASE,  NULL);
+  return !isLogical && !isPagedContainer;
+}
+
+bool isWidgetNode(xmlNodePtr theNode)
+{
+  if(!isElementNode(theNode))
+    return false;
+  // it's parent is "feature" or "source" or a page ("box", "case")
+  if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE, WDG_GROUP, WDG_OPTIONALBOX,
+    WDG_TOOLBOX_BOX, WDG_RADIOBOX_ITEM, WDG_SWITCH_CASE, WDG_FILTER, NULL))
+    return false;
+
+  //it should not be a "source" or a "validator" node
+  return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NULL);
+}
+
+// LCOV_EXCL_START
+// widget api?
+bool isCaseNode(xmlNodePtr theNode)
+{
+  if(!isElementNode(theNode))
+    return false;
+
+  return isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX,
+    WDG_RADIOBOX_ITEM, NULL);
+}
+// LCOV_EXCL_STOP
+
+bool hasChild(xmlNodePtr theNode)
+{
+  xmlNodePtr aNode = theNode->children;
+  for (; aNode; aNode = aNode->next) {
+    if (isElementNode(theNode)) {
+      return true;
+    }
+  }
+  return false;
+}
+
+bool hasParent(xmlNodePtr theNode)
+{
+  xmlNodePtr aNode = theNode->parent;
+  if (!aNode) {
+    return false;
+  }
+  for (; aNode; aNode = aNode->next) {
+    if (isElementNode(theNode)) {
+      return true;
+    }
+  }
+  return false;
+}
+
+bool hasParent(xmlNodePtr theNode, const char* theNodeName, ...)
+{
+  if (!hasParent(theNode)) {
+    return false; // have no parents at all
+  }
+  xmlNodePtr aNode = theNode->parent;
+  const xmlChar* aName = aNode->name;
+  if (!aName || !isElementNode(aNode)) {
+    return false;
+  }
+  if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
+    return true;
+  }
+  va_list args;  // define argument list variable
+  va_start(args, theNodeName);  // init list; point to last defined argument
+  while (true) {
+    char *anArg = va_arg (args, char*);  // get next argument
+    if (anArg == NULL)
+      break;
+    if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
+      va_end(args);  // cleanup the system stack
+      return true;
+    }
+  }
+  va_end(args);  // cleanup the system stack
+  return false;
+}
+
+xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const std::vector<const char*>& theNodeNames)
+{
+  if (!hasParent(theNode)) {
+    return 0; // have no parents at all
+  }
+  xmlNodePtr aNode = theNode->parent;
+  const xmlChar* aName = aNode->name;
+  if (!aName || !isElementNode(aNode)) {
+    return 0;
+  }
+  for (size_t anIndex = 0; anIndex < theNodeNames.size(); ++anIndex) {
+    if (!xmlStrcmp(aName, (const xmlChar *) theNodeNames[anIndex]))
+      return aNode;
+  }
+  return hasParentRecursive(aNode, theNodeNames);
+}
+
+xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const char* theNodeName, ...)
+{
+  std::vector<const char*> aNodeNames;
+  va_list args;  // define argument list variable
+  va_start(args, theNodeName);  // init list; point to last defined argument
+  aNodeNames.push_back(theNodeName);
+  while (true) {
+    char *anArg = va_arg (args, char*);  // get next argument
+    if (anArg == NULL)
+      break;
+    aNodeNames.push_back(anArg);
+  }
+  va_end(args);  // cleanup the system stack
+  return hasParentRecursive(theNode, aNodeNames);
+}
+
+bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,
+                      std::list<std::string>& outValidatorParameters)
+{
+  //Property id:
+  char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);
+  if (!anIdProp || anIdProp[0] == 0) {
+    return false;
+  }
+  outPropertyId = std::string(anIdProp);
+
+  //Property parameters:
+  char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST _PARAMETERS);
+  if (aParamProp && aParamProp[0] != 0) {
+    std::string aPropString = std::string(aParamProp);
+    std::stringstream aPropStringStream(aPropString);
+    char COMMA_DELIM = ',';
+    std::string aParameter;
+    while (std::getline(aPropStringStream, aParameter, COMMA_DELIM)) {
+      outValidatorParameters.push_back(aParameter);
+    }
+  }
+  return true;
+}
+
+std::string library(const std::string& theLibName)
+{
+  if(theLibName.empty())
+    return std::string();
+  std::string aLibName = theLibName;
+#ifndef WIN32
+  static std::string aLibExt( ".so" );
+  if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {
+    aLibName = "lib" + aLibName;
+  }
+#else
+  static std::string aLibExt(".dll");
+#endif
+  std::string anExt = aLibName.substr(aLibName.size() - 4);
+  if (anExt != aLibExt)
+    aLibName += aLibExt;
+
+  return aLibName;
+}
+
+bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ' '); }
+
+std::string getProperty(xmlNodePtr theNode, const char* thePropName)
+{
+  std::string result = "";
+  xmlChar* aPropChars = xmlGetProp(theNode, BAD_CAST thePropName);
+  if (!aPropChars || aPropChars[0] == 0)
+    return result;
+  result = std::string((char*)aPropChars);
+  xmlFree(aPropChars);
+
+  std::string::iterator new_end = std::unique(result.begin(), result.end(), BothAreSpaces);
+  result.erase(new_end, result.end());
+
+  return result;
+}
+
+// LCOV_EXCL_START
+std::string getContent(xmlNodePtr theNode)
+{
+  std::string result = "";
+  xmlChar* aContent = xmlNodeGetContent(theNode);
+  if (!aContent || aContent[0] == 0)
+    return result;
+  result = std::string((char*)aContent);
+  xmlFree(aContent);
+  return result;
+}
+// LCOV_EXCL_STOP
+
+std::string getNormalizedProperty(xmlNodePtr theNode, const char* thePropName)
+{
+  return normalize(getProperty(theNode, thePropName));
+}
+
+bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)
+{
+  std::string prop = normalize(getProperty(theNode, theAttributeName));
+  bool result = theDefault;
+  if (prop == "true" || prop == "1") {
+    result = true;
+  } else if (prop == "false" || prop == "0") {
+    result = false;
+  }
+  return result;
+}
+
+CONFIG_EXPORT std::string normalize(const std::string& theString)
+{
+  std::string result = theString;
+  std::transform(result.begin(), result.end(), result.begin(),
+                 [](char c) { return static_cast<char>(::tolower(c)); });
+  return result;
+}