From 68955e294ea772b5a37653b3c871f6ca6268324d Mon Sep 17 00:00:00 2001 From: Valerii Sokolov Date: Wed, 29 Apr 2015 13:46:47 +0200 Subject: Now include path is system-independent. --- src/XMLNode.cpp | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src/XMLNode.cpp') diff --git a/src/XMLNode.cpp b/src/XMLNode.cpp index 4b2bdf4..5e9d927 100644 --- a/src/XMLNode.cpp +++ b/src/XMLNode.cpp @@ -28,13 +28,8 @@ $Id$ #include "astra/XMLNode.h" -#ifdef _MSC_VER #include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_print.hpp" -#else -#include "rapidxml.hpp" -#include "rapidxml_print.hpp" -#endif #include -- cgit v1.2.3 From 47fe3421585302f2101691a685ab99b0e1ad5cfc Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Fri, 1 May 2015 17:48:32 +0200 Subject: Change XMLNode* to XMLNode An XMLNode object is already simply a pointer, so no need to dynamically allocate XMLNodes. --- src/XMLNode.cpp | 155 ++++++++++++++++++++++++-------------------------------- 1 file changed, 66 insertions(+), 89 deletions(-) (limited to 'src/XMLNode.cpp') diff --git a/src/XMLNode.cpp b/src/XMLNode.cpp index 5e9d927..75985cc 100644 --- a/src/XMLNode.cpp +++ b/src/XMLNode.cpp @@ -38,22 +38,11 @@ using namespace astra; using namespace std; -//----------------------------------------------------------------------------- -// Utility function to delete a list of nodes -static void deleteNodes(list& nodes) -{ - for (list::iterator i = nodes.begin(); i != nodes.end(); ++i) - delete (*i); - - nodes.clear(); -} - - //----------------------------------------------------------------------------- // default constructor XMLNode::XMLNode() { - + fDOMElement = 0; } //----------------------------------------------------------------------------- @@ -79,14 +68,14 @@ void XMLNode::setDOMNode(xml_node<>* n) //----------------------------------------------------------------------------- // print XML Node -void XMLNode::print() +void XMLNode::print() const { std::cout << fDOMElement; } //----------------------------------------------------------------------------- // print XML Node -std::string XMLNode::toString() +std::string XMLNode::toString() const { std::string s; ::print(std::back_inserter(s), *fDOMElement, 0); @@ -95,64 +84,61 @@ std::string XMLNode::toString() //----------------------------------------------------------------------------- // Get single node -XMLNode* XMLNode::getSingleNode(string name) +XMLNode XMLNode::getSingleNode(string name) const { xml_node<> *node = fDOMElement->first_node(name.c_str()); - if (node) - return new XMLNode(node); - else - return 0; + return XMLNode(node); } //----------------------------------------------------------------------------- // Get list of nodes -list XMLNode::getNodes(string name) +list XMLNode::getNodes(string name) const { - list result; + list result; xml_node<> *iter; for (iter = fDOMElement->first_node(name.c_str()); iter; iter = iter->next_sibling(name.c_str())) { - result.push_back(new XMLNode(iter)); + result.push_back(XMLNode(iter)); } return result; } //----------------------------------------------------------------------------- // Get list of nodes -list XMLNode::getNodes() +list XMLNode::getNodes() const { - list result; + list result; xml_node<> *iter; for (iter = fDOMElement->first_node(); iter; iter = iter->next_sibling()) { - result.push_back(new XMLNode(iter)); + result.push_back(XMLNode(iter)); } return result; } //----------------------------------------------------------------------------- // Get name of this node -std::string XMLNode::getName() +std::string XMLNode::getName() const { return fDOMElement->name(); } //----------------------------------------------------------------------------- // Get node content - STRING -string XMLNode::getContent() +string XMLNode::getContent() const { return fDOMElement->value(); } //----------------------------------------------------------------------------- // Get node content - NUMERICAL -float32 XMLNode::getContentNumerical() +float32 XMLNode::getContentNumerical() const { return boost::lexical_cast(getContent()); } //----------------------------------------------------------------------------- // Get node content - BOOLEAN -bool XMLNode::getContentBool() +bool XMLNode::getContentBool() const { string res = getContent(); return ((res == "1") || (res == "yes") || (res == "true") || (res == "on")); @@ -160,21 +146,20 @@ bool XMLNode::getContentBool() //----------------------------------------------------------------------------- // Get node content - STRING LIST -vector XMLNode::getContentArray() +vector XMLNode::getContentArray() const { // get listsize int iSize = boost::lexical_cast(getAttribute("listsize")); // create result array vector res(iSize); // loop all list item nodes - list nodes = getNodes("ListItem"); - for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { - int iIndex = (*it)->getAttributeNumerical("index"); - string sValue = (*it)->getAttribute("value"); + list nodes = getNodes("ListItem"); + for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { + int iIndex = it->getAttributeNumerical("index"); + string sValue = it->getAttribute("value"); ASTRA_ASSERT(iIndex < iSize); res[iIndex] = sValue; } - deleteNodes(nodes); // return return res; @@ -182,7 +167,7 @@ vector XMLNode::getContentArray() //----------------------------------------------------------------------------- // Get node content - NUMERICAL LIST -vector XMLNode::getContentNumericalArray() +vector XMLNode::getContentNumericalArray() const { // is scalar if (!hasAttribute("listsize")) { @@ -195,19 +180,18 @@ vector XMLNode::getContentNumericalArray() // create result array vector res(iSize); // loop all list item nodes - list nodes = getNodes("ListItem"); - for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { - int iIndex = (*it)->getAttributeNumerical("index"); - float32 fValue = (*it)->getAttributeNumerical("value"); + list nodes = getNodes("ListItem"); + for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { + int iIndex = it->getAttributeNumerical("index"); + float32 fValue = it->getAttributeNumerical("value"); ASTRA_ASSERT(iIndex < iSize); res[iIndex] = fValue; } - deleteNodes(nodes); // return return res; } -vector XMLNode::getContentNumericalArrayDouble() +vector XMLNode::getContentNumericalArrayDouble() const { // is scalar if (!hasAttribute("listsize")) { @@ -220,21 +204,20 @@ vector XMLNode::getContentNumericalArrayDouble() // create result array vector res(iSize); // loop all list item nodes - list nodes = getNodes("ListItem"); - for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { - int iIndex = (*it)->getAttributeNumerical("index"); - double fValue = (*it)->getAttributeNumericalDouble("value"); + list nodes = getNodes("ListItem"); + for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { + int iIndex = it->getAttributeNumerical("index"); + double fValue = it->getAttributeNumericalDouble("value"); ASTRA_ASSERT(iIndex < iSize); res[iIndex] = fValue; } - deleteNodes(nodes); // return return res; } //----------------------------------------------------------------------------- // Get node content - NUMERICAL LIST 2 -void XMLNode::getContentNumericalArray(float32*& _pfData, int& _iSize) +void XMLNode::getContentNumericalArray(float32*& _pfData, int& _iSize) const { // is scalar if (!hasAttribute("listsize")) { @@ -248,19 +231,18 @@ void XMLNode::getContentNumericalArray(float32*& _pfData, int& _iSize) // create result array _pfData = new float32[_iSize]; // loop all list item nodes - list nodes = getNodes("ListItem"); - for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { - int iIndex = (*it)->getAttributeNumerical("index"); - float32 fValue = (*it)->getAttributeNumerical("value"); + list nodes = getNodes("ListItem"); + for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { + int iIndex = it->getAttributeNumerical("index"); + float32 fValue = it->getAttributeNumerical("value"); ASTRA_ASSERT(iIndex < _iSize); _pfData[iIndex] = fValue; } - deleteNodes(nodes); } //----------------------------------------------------------------------------- // Is attribute? -bool XMLNode::hasAttribute(string _sName) +bool XMLNode::hasAttribute(string _sName) const { xml_attribute<> *attr = fDOMElement->first_attribute(_sName.c_str()); return (attr != 0); @@ -268,7 +250,7 @@ bool XMLNode::hasAttribute(string _sName) //----------------------------------------------------------------------------- // Get attribute - STRING -string XMLNode::getAttribute(string _sName, string _sDefaultValue) +string XMLNode::getAttribute(string _sName, string _sDefaultValue) const { xml_attribute<> *attr = fDOMElement->first_attribute(_sName.c_str()); @@ -279,12 +261,12 @@ string XMLNode::getAttribute(string _sName, string _sDefaultValue) //----------------------------------------------------------------------------- // Get attribute - NUMERICAL -float32 XMLNode::getAttributeNumerical(string _sName, float32 _fDefaultValue) +float32 XMLNode::getAttributeNumerical(string _sName, float32 _fDefaultValue) const { if (!hasAttribute(_sName)) return _fDefaultValue; return boost::lexical_cast(getAttribute(_sName)); } -double XMLNode::getAttributeNumericalDouble(string _sName, double _fDefaultValue) +double XMLNode::getAttributeNumericalDouble(string _sName, double _fDefaultValue) const { if (!hasAttribute(_sName)) return _fDefaultValue; return boost::lexical_cast(getAttribute(_sName)); @@ -292,7 +274,7 @@ double XMLNode::getAttributeNumericalDouble(string _sName, double _fDefaultValue //----------------------------------------------------------------------------- // Get attribute - BOOLEAN -bool XMLNode::getAttributeBool(string _sName, bool _bDefaultValue) +bool XMLNode::getAttributeBool(string _sName, bool _bDefaultValue) const { if (!hasAttribute(_sName)) return _bDefaultValue; string res = getAttribute(_sName); @@ -301,7 +283,7 @@ bool XMLNode::getAttributeBool(string _sName, bool _bDefaultValue) //----------------------------------------------------------------------------- // Has option? -bool XMLNode::hasOption(string _sKey) +bool XMLNode::hasOption(string _sKey) const { xml_node<> *iter; for (iter = fDOMElement->first_node("Option"); iter; iter = iter->next_sibling("Option")) { @@ -314,7 +296,7 @@ bool XMLNode::hasOption(string _sKey) //----------------------------------------------------------------------------- // Get option - STRING -string XMLNode::getOption(string _sKey, string _sDefaultValue) +string XMLNode::getOption(string _sKey, string _sDefaultValue) const { xml_node<> *iter; for (iter = fDOMElement->first_node("Option"); iter; iter = iter->next_sibling("Option")) { @@ -331,7 +313,7 @@ string XMLNode::getOption(string _sKey, string _sDefaultValue) //----------------------------------------------------------------------------- // Get option - NUMERICAL -float32 XMLNode::getOptionNumerical(string _sKey, float32 _fDefaultValue) +float32 XMLNode::getOptionNumerical(string _sKey, float32 _fDefaultValue) const { if (!hasOption(_sKey)) return _fDefaultValue; return boost::lexical_cast(getOption(_sKey)); @@ -339,7 +321,7 @@ float32 XMLNode::getOptionNumerical(string _sKey, float32 _fDefaultValue) //----------------------------------------------------------------------------- // Get option - BOOL -bool XMLNode::getOptionBool(string _sKey, bool _bDefaultValue) +bool XMLNode::getOptionBool(string _sKey, bool _bDefaultValue) const { bool bHasOption = hasOption(_sKey); if (!bHasOption) return _bDefaultValue; @@ -349,20 +331,18 @@ bool XMLNode::getOptionBool(string _sKey, bool _bDefaultValue) //----------------------------------------------------------------------------- // Get option - NUMERICAL ARRAY -vector XMLNode::getOptionNumericalArray(string _sKey) +vector XMLNode::getOptionNumericalArray(string _sKey) const { if (!hasOption(_sKey)) return vector(); - list nodes = getNodes("Option"); - for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { - if ((*it)->getAttribute("key") == _sKey) { - vector vals = (*it)->getContentNumericalArray(); - deleteNodes(nodes); + list nodes = getNodes("Option"); + for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { + if (it->getAttribute("key") == _sKey) { + vector vals = it->getContentNumericalArray(); return vals; } } - deleteNodes(nodes); return vector(); } @@ -385,41 +365,40 @@ vector XMLNode::getOptionNumericalArray(string _sKey) //----------------------------------------------------------------------------- // Add child node - EMPTY -XMLNode* XMLNode::addChildNode(string _sNodeName) +XMLNode XMLNode::addChildNode(string _sNodeName) { xml_document<> *doc = fDOMElement->document(); char *node_name = doc->allocate_string(_sNodeName.c_str()); xml_node<> *node = doc->allocate_node(node_element, node_name); fDOMElement->append_node(node); - // TODO: clean up: this 'new' requires callers to do memory management - return new XMLNode(node); + return XMLNode(node); } //----------------------------------------------------------------------------- // Add child node - STRING -XMLNode* XMLNode::addChildNode(string _sNodeName, string _sText) +XMLNode XMLNode::addChildNode(string _sNodeName, string _sText) { - XMLNode* res = addChildNode(_sNodeName); - res->setContent(_sText); + XMLNode res = addChildNode(_sNodeName); + res.setContent(_sText); return res; } //----------------------------------------------------------------------------- // Add child node - FLOAT -XMLNode* XMLNode::addChildNode(string _sNodeName, float32 _fValue) +XMLNode XMLNode::addChildNode(string _sNodeName, float32 _fValue) { - XMLNode* res = addChildNode(_sNodeName); - res->setContent(_fValue); + XMLNode res = addChildNode(_sNodeName); + res.setContent(_fValue); return res; } //----------------------------------------------------------------------------- // Add child node - LIST -XMLNode* XMLNode::addChildNode(string _sNodeName, float32* _pfList, int _iSize) +XMLNode XMLNode::addChildNode(string _sNodeName, float32* _pfList, int _iSize) { - XMLNode* res = addChildNode(_sNodeName); - res->setContent(_pfList, _iSize); + XMLNode res = addChildNode(_sNodeName); + res.setContent(_pfList, _iSize); return res; } @@ -472,20 +451,18 @@ void XMLNode::addAttribute(string _sName, float32 _fValue) // Add option - STRING void XMLNode::addOption(string _sName, string _sText) { - XMLNode* node = addChildNode("Option"); - node->addAttribute("key", _sName); - node->addAttribute("value", _sText); - delete node; + XMLNode node = addChildNode("Option"); + node.addAttribute("key", _sName); + node.addAttribute("value", _sText); } //----------------------------------------------------------------------------- // Add option - FLOAT void XMLNode::addOption(string _sName, float32 _sText) { - XMLNode* node = addChildNode("Option"); - node->addAttribute("key", _sName); - node->addAttribute("value", _sText); - delete node; + XMLNode node = addChildNode("Option"); + node.addAttribute("key", _sName); + node.addAttribute("value", _sText); } //----------------------------------------------------------------------------- -- cgit v1.2.3 From fff7470f1d74b0085355130350fa834ea8d37069 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Wed, 6 May 2015 13:50:11 +0200 Subject: Make XML array handling consistent setContent and getContent were using different XML formats previously. --- src/XMLNode.cpp | 150 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 87 insertions(+), 63 deletions(-) (limited to 'src/XMLNode.cpp') diff --git a/src/XMLNode.cpp b/src/XMLNode.cpp index 75985cc..0ec701f 100644 --- a/src/XMLNode.cpp +++ b/src/XMLNode.cpp @@ -32,6 +32,11 @@ $Id$ #include "rapidxml/rapidxml_print.hpp" #include +#include +#include +#include + + using namespace rapidxml; using namespace astra; @@ -167,77 +172,43 @@ vector XMLNode::getContentArray() const //----------------------------------------------------------------------------- // Get node content - NUMERICAL LIST +// NB: A 2D matrix is returned as a linear list vector XMLNode::getContentNumericalArray() const { - // is scalar - if (!hasAttribute("listsize")) { - vector res(1); - res[0] = getContentNumerical(); - return res; - } + string input = getContent(); - int iSize = boost::lexical_cast(getAttribute("listsize")); - // create result array - vector res(iSize); - // loop all list item nodes - list nodes = getNodes("ListItem"); - for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { - int iIndex = it->getAttributeNumerical("index"); - float32 fValue = it->getAttributeNumerical("value"); - ASTRA_ASSERT(iIndex < iSize); - res[iIndex] = fValue; + // split + std::vector items; + boost::split(items, input, boost::is_any_of(",;")); + + // init list + vector out; + out.resize(items.size()); + + // loop elements + for (unsigned int i = 0; i < items.size(); i++) { + out[i] = boost::lexical_cast(items[i]); } - // return - return res; + return out; } vector XMLNode::getContentNumericalArrayDouble() const { - // is scalar - if (!hasAttribute("listsize")) { - vector res(1); - res[0] = getContentNumerical(); - return res; - } + string input = getContent(); - int iSize = boost::lexical_cast(getAttribute("listsize")); - // create result array - vector res(iSize); - // loop all list item nodes - list nodes = getNodes("ListItem"); - for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { - int iIndex = it->getAttributeNumerical("index"); - double fValue = it->getAttributeNumericalDouble("value"); - ASTRA_ASSERT(iIndex < iSize); - res[iIndex] = fValue; - } - // return - return res; -} + // split + std::vector items; + boost::split(items, input, boost::is_any_of(",;")); -//----------------------------------------------------------------------------- -// Get node content - NUMERICAL LIST 2 -void XMLNode::getContentNumericalArray(float32*& _pfData, int& _iSize) const -{ - // is scalar - if (!hasAttribute("listsize")) { - _iSize = 1; - _pfData = new float32[_iSize]; - _pfData[0] = getContentNumerical(); - return; - } - // get listsize - _iSize = boost::lexical_cast(getAttribute("listsize")); - // create result array - _pfData = new float32[_iSize]; - // loop all list item nodes - list nodes = getNodes("ListItem"); - for (list::iterator it = nodes.begin(); it != nodes.end(); it++) { - int iIndex = it->getAttributeNumerical("index"); - float32 fValue = it->getAttributeNumerical("value"); - ASTRA_ASSERT(iIndex < _iSize); - _pfData[iIndex] = fValue; + // init list + vector out; + out.resize(items.size()); + + // loop elements + for (unsigned int i = 0; i < items.size(); i++) { + out[i] = boost::lexical_cast(items[i]); } + return out; } //----------------------------------------------------------------------------- @@ -420,15 +391,68 @@ void XMLNode::setContent(float32 _fValue) //----------------------------------------------------------------------------- // Set content - LIST -void XMLNode::setContent(float32* pfList, int _iSize) -{ + +template +static std::string setContentList_internal(T* pfList, int _iSize) { std::string str = (_iSize > 0) ? boost::lexical_cast(pfList[0]) : ""; for (int i = 1; i < _iSize; i++) { str += "," + boost::lexical_cast(pfList[i]); } - setContent(str); + return str; +} + +void XMLNode::setContent(float32* pfList, int _iSize) +{ + setContent(setContentList_internal(pfList, _iSize)); +} + +void XMLNode::setContent(double* pfList, int _iSize) +{ + setContent(setContentList_internal(pfList, _iSize)); } +//----------------------------------------------------------------------------- +// Set content - MATRIX + +template +static std::string setContentMatrix_internal(T* _pfMatrix, int _iWidth, int _iHeight, bool transposed) +{ + std::string str = ""; + + int s1,s2; + + if (!transposed) { + s1 = 1; + s2 = _iWidth; + } else { + s1 = _iHeight; + s2 = 1; + } + + for (int y = 0; y < _iHeight; ++y) { + if (_iWidth > 0) + str += boost::lexical_cast(_pfMatrix[0*s1 + y*s2]); + for (int x = 1; x < _iWidth; x++) + str += "," + boost::lexical_cast(_pfMatrix[x*s1 + y*s2]); + + if (y != _iHeight-1) + str += ";"; + } + + return str; +} + +void XMLNode::setContent(float32* _pfMatrix, int _iWidth, int _iHeight, bool transposed) +{ + setContent(setContentMatrix_internal(_pfMatrix, _iWidth, _iHeight, transposed)); +} + +void XMLNode::setContent(double* _pfMatrix, int _iWidth, int _iHeight, bool transposed) +{ + setContent(setContentMatrix_internal(_pfMatrix, _iWidth, _iHeight, transposed)); +} + + //----------------------------------------------------------------------------- // Add attribute - STRING void XMLNode::addAttribute(string _sName, string _sText) -- cgit v1.2.3