Commit d5656600 authored by Pavel Vainerman's avatar Pavel Vainerman

(UniXML): добавил функцию getPropList().

parent 23520fb8
...@@ -31,12 +31,14 @@ ...@@ -31,12 +31,14 @@
#include <string> #include <string>
#include <cstddef> #include <cstddef>
#include <memory> #include <memory>
#include <vector>
#include <libxml/parser.h> #include <libxml/parser.h>
#include <libxml/tree.h> #include <libxml/tree.h>
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace uniset namespace uniset
{ {
typedef std::vector< std::pair<const std::string, const std::string> > UniXMLPropList;
class UniXML_iterator: class UniXML_iterator:
public std::iterator<std::bidirectional_iterator_tag, xmlNode, ptrdiff_t, xmlNode*, xmlNode&> public std::iterator<std::bidirectional_iterator_tag, xmlNode, ptrdiff_t, xmlNode*, xmlNode&>
...@@ -104,6 +106,8 @@ namespace uniset ...@@ -104,6 +106,8 @@ namespace uniset
void goBegin() noexcept; void goBegin() noexcept;
void goEnd() noexcept; void goEnd() noexcept;
UniXMLPropList getPropList() const;
private: private:
xmlNode* curNode; xmlNode* curNode;
...@@ -114,6 +118,7 @@ namespace uniset ...@@ -114,6 +118,7 @@ namespace uniset
public: public:
typedef UniXML_iterator iterator; typedef UniXML_iterator iterator;
typedef UniXMLPropList PropList;
UniXML( const std::string& filename ); UniXML( const std::string& filename );
UniXML(); UniXML();
...@@ -149,6 +154,8 @@ namespace uniset ...@@ -149,6 +154,8 @@ namespace uniset
// Установить свойство name указанного узла node // Установить свойство name указанного узла node
static void setProp(xmlNode* node, const std::string& name, const std::string& text); static void setProp(xmlNode* node, const std::string& name, const std::string& text);
static UniXMLPropList getPropList( xmlNode* node );
// Добавить новый дочерний узел // Добавить новый дочерний узел
static xmlNode* createChild(xmlNode* node, const std::string& title, const std::string& text); static xmlNode* createChild(xmlNode* node, const std::string& title, const std::string& text);
......
...@@ -210,6 +210,26 @@ void UniXML::setProp(xmlNode* node, const string& name, const string& text ) ...@@ -210,6 +210,26 @@ void UniXML::setProp(xmlNode* node, const string& name, const string& text )
::xmlSetProp(node, (const xmlChar*)name.c_str(), (const xmlChar*)text.c_str()); ::xmlSetProp(node, (const xmlChar*)name.c_str(), (const xmlChar*)text.c_str());
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
UniXMLPropList UniXML::getPropList( xmlNode* node )
{
UniXMLPropList lst;
if( !node )
return std::move(lst);
xmlAttr* attribute = node->properties;
while( attribute )
{
xmlChar* value = ::xmlNodeListGetString(node->doc, attribute->children, 1);
const std::string nm( (const char*)attribute->name );
const std::string val( (const char*)value );
lst.push_back( {nm,val} );
xmlFree(value);
attribute = attribute->next;
}
return std::move(lst);
}
// -----------------------------------------------------------------------------
xmlNode* UniXML::createChild(xmlNode* node, const string& title, const string& text) xmlNode* UniXML::createChild(xmlNode* node, const string& title, const string& text)
{ {
return ::xmlNewChild(node, NULL, (const xmlChar*)title.c_str(), (const xmlChar*)text.c_str()); return ::xmlNewChild(node, NULL, (const xmlChar*)title.c_str(), (const xmlChar*)text.c_str());
...@@ -526,6 +546,11 @@ void UniXML_iterator::goEnd() noexcept ...@@ -526,6 +546,11 @@ void UniXML_iterator::goEnd() noexcept
} }
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
UniXMLPropList UniXML_iterator::getPropList() const
{
return UniXML::getPropList(curNode);
}
// -------------------------------------------------------------------------
UniXML_iterator::operator xmlNode* () const noexcept UniXML_iterator::operator xmlNode* () const noexcept
{ {
//ulog.< "current\n"; //ulog.< "current\n";
......
#include <catch.hpp> #include <catch.hpp>
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#include <iostream> #include <iostream>
#include <sstream>
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#include "Exceptions.h" #include "Exceptions.h"
#include "UniXML.h" #include "UniXML.h"
...@@ -162,3 +163,32 @@ TEST_CASE("UniXML::iterator::find", "[unixml][iterator-find][basic]" ) ...@@ -162,3 +163,32 @@ TEST_CASE("UniXML::iterator::find", "[unixml][iterator-find][basic]" )
CHECK( sIt.find("subnode") ); CHECK( sIt.find("subnode") );
REQUIRE( sIt.getProp("name") == "Test5" ); REQUIRE( sIt.getProp("name") == "Test5" );
} }
// -----------------------------------------------------------------------------
TEST_CASE("UniXML::iterator::getPropList", "[unixml][iterator-proplist][basic]" )
{
UniXML uxml("tests_unixml.xml");
REQUIRE( uxml.isOpen() );
UniXML::iterator it = uxml.begin();
REQUIRE( it.find("TestPropList") != 0 );
UniXML::PropList lst = it.getPropList();
REQUIRE( lst.size() == 5 );
std::ostringstream n;
std::ostringstream v;
for( size_t i=0; i<5; i++ )
{
n.str("");
n << "prop" << (i+1);
v.str("");
v << "val" << (i+1);
REQUIRE( lst[i].first == n.str() );
REQUIRE( lst[i].second == v.str() );
}
}
// -----------------------------------------------------------------------------
...@@ -44,6 +44,8 @@ ...@@ -44,6 +44,8 @@
<subnode name="Test4"/> <subnode name="Test4"/>
<TestPropList prop1="val1" prop2="val2" prop3="val3" prop4="val4" prop5="val5"/>
<TestProc name="TestProc1" <TestProc name="TestProc1"
on_s="Input1_S" on_s="Input1_S"
lamp_c="Lamp58_C" lamp_c="Lamp58_C"
......
...@@ -16,6 +16,7 @@ extensions/tests/SMemoryTest ...@@ -16,6 +16,7 @@ extensions/tests/SMemoryTest
extensions/UNetUDP extensions/UNetUDP
extensions/UniNetwork extensions/UniNetwork
include include
include/libxml2/libxml
include/modbus include/modbus
python/lib/pyUniSet python/lib/pyUniSet
src/Log src/Log
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment