Commit 12f6058d authored by Pavel Vainerman's avatar Pavel Vainerman

Добавил пару вспомогательных функций "для удобства инициализации".

(getProp2(),getArg2Param())
parent 07661f58
...@@ -133,6 +133,8 @@ namespace UniSetTypes ...@@ -133,6 +133,8 @@ namespace UniSetTypes
/*! получить значение указанного параметра, или значение по умолчанию */ /*! получить значение указанного параметра, или значение по умолчанию */
std::string getArgParam(const std::string& name, const std::string& defval=""); std::string getArgParam(const std::string& name, const std::string& defval="");
/*! получить значение, если пустое, то defval, если defval="" return defval2 */
std::string getArg2Param(const std::string& name, const std::string& defval, const std::string& defval2="");
/*! получить числовое значение параметра, если не число, то 0. Если параметра нет, используется значение defval */ /*! получить числовое значение параметра, если не число, то 0. Если параметра нет, используется значение defval */
int getArgInt(const std::string& name, const std::string& defval=""); int getArgInt(const std::string& name, const std::string& defval="");
/*! получить числовое значение параметра, но если оно не положительное, вернуть defval */ /*! получить числовое значение параметра, но если оно не положительное, вернуть defval */
......
...@@ -46,6 +46,7 @@ class UniXML_iterator: ...@@ -46,6 +46,7 @@ class UniXML_iterator:
{} {}
UniXML_iterator() {} UniXML_iterator() {}
std::string getProp2( const std::string& name, const std::string& defval="" );
std::string getProp( const std::string& name ); std::string getProp( const std::string& name );
std::string getPropUtf8( const std::string& name ); std::string getPropUtf8( const std::string& name );
int getIntProp( const std::string& name ); int getIntProp( const std::string& name );
...@@ -195,6 +196,8 @@ public: ...@@ -195,6 +196,8 @@ public:
// Получить свойство name указанного узла node // Получить свойство name указанного узла node
static std::string getProp(const xmlNode* node, const std::string& name); static std::string getProp(const xmlNode* node, const std::string& name);
static std::string getProp2(const xmlNode* node, const std::string& name, const std::string& defval="" );
static std::string getPropUtf8(const xmlNode* node, const std::string& name); static std::string getPropUtf8(const xmlNode* node, const std::string& name);
static int getIntProp(const xmlNode* node, const std::string& name); static int getIntProp(const xmlNode* node, const std::string& name);
/// if value if not positive ( <= 0 ), returns def /// if value if not positive ( <= 0 ), returns def
......
...@@ -400,6 +400,18 @@ void Configuration::initConfiguration( int argc, const char* const* argv ) ...@@ -400,6 +400,18 @@ void Configuration::initConfiguration( int argc, const char* const* argv )
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
std::string Configuration::getArg2Param( const std::string& name, const std::string& defval, const std::string& defval2 )
{
string s(UniSetTypes::getArgParam(name, _argc, _argv,""));
if( !s.empty() )
return std::move(s);
if( !defval.empty() )
return defval;
return defval2;
}
string Configuration::getArgParam( const string& name, const string& defval ) string Configuration::getArgParam( const string& name, const string& defval )
{ {
return UniSetTypes::getArgParam(name, _argc, _argv, defval); return UniSetTypes::getArgParam(name, _argc, _argv, defval);
......
...@@ -116,6 +116,15 @@ string UniXML::getPropUtf8(const xmlNode* node, const string& name) ...@@ -116,6 +116,15 @@ string UniXML::getPropUtf8(const xmlNode* node, const string& name)
return getProp(node, name); return getProp(node, name);
} }
string UniXML::getProp2(const xmlNode* node, const string& name, const string& defval)
{
string s(getProp(node,name));
if( !s.empty() )
return std::move(s);
return defval;
}
string UniXML::getProp(const xmlNode* node, const string& name) string UniXML::getProp(const xmlNode* node, const string& name)
{ {
xmlChar* text = ::xmlGetProp((xmlNode*)node, (const xmlChar*)name.c_str()); xmlChar* text = ::xmlGetProp((xmlNode*)node, (const xmlChar*)name.c_str());
...@@ -386,6 +395,11 @@ bool UniXML_iterator::goChildren() ...@@ -386,6 +395,11 @@ bool UniXML_iterator::goChildren()
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
string UniXML_iterator::getProp2( const string& name, const string& defval )
{
return UniXML::getProp2(curNode,name,defval);
}
string UniXML_iterator::getProp( const string& name ) string UniXML_iterator::getProp( const string& name )
{ {
return UniXML::getProp(curNode, name); return UniXML::getProp(curNode, name);
......
...@@ -39,6 +39,9 @@ TEST_CASE("UniXML", "[unixml][basic]" ) ...@@ -39,6 +39,9 @@ TEST_CASE("UniXML", "[unixml][basic]" )
CHECK( uxml.getPIntProp(tnode,"negative",20) == -10 ); CHECK( uxml.getPIntProp(tnode,"negative",20) == -10 );
CHECK( uxml.getPIntProp(tnode,"unknown",20) == 20 ); CHECK( uxml.getPIntProp(tnode,"unknown",20) == 20 );
CHECK( uxml.getProp2(tnode,"unknown","def") == "def" );
CHECK( uxml.getProp2(tnode,"text","def") == "text" );
// nextNode // nextNode
// create // create
// remove // remove
...@@ -114,6 +117,8 @@ TEST_CASE("UniXML::iterator", "[unixml][iterator][basic]" ) ...@@ -114,6 +117,8 @@ TEST_CASE("UniXML::iterator", "[unixml][iterator][basic]" )
CHECK( it.find("TestData") != 0 ); CHECK( it.find("TestData") != 0 );
CHECK( it.getProp("text") == "text" ); CHECK( it.getProp("text") == "text" );
CHECK( it.getProp2("text","def") == "text" );
CHECK( it.getProp2("unknown","def") == "def" );
CHECK( it.getIntProp("x") == 10 ); CHECK( it.getIntProp("x") == 10 );
CHECK( it.getPIntProp("y",-20) == 100 ); CHECK( it.getPIntProp("y",-20) == 100 );
CHECK( it.getPIntProp("zero",20) == 0 ); CHECK( it.getPIntProp("zero",20) == 0 );
......
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