From 5d34db5fcfbade3f933bacf15d58222a7a75a8fd Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 9 Apr 2021 17:49:44 +0200 Subject: [PATCH] added to Space: - methods for testing a string value if a Space is a string or a table: bool has_value(const char * val) const; bool has_value(const std::string & val) const; bool has_value(const wchar_t * val) const; bool has_value(const std::wstring & val) const; - methods for testing a string value in an object (testing a string or a table): bool has_value(const wchar_t * field, const char * val) const; bool has_value(const wchar_t * field, const std::string & val) const; bool has_value(const wchar_t * field, const wchar_t * val) const; bool has_value(const wchar_t * field, const std::wstring & val) const; - methods for removing a child space: void remove_child_space(const wchar_t * name); void remove_child_space(const std::wstring & name); void remove_child_space(size_t index); --- space/space.cpp | 155 ++++++++++++++++++++++++++++++++++++++++++++++++ space/space.h | 30 +++++++--- 2 files changed, 178 insertions(+), 7 deletions(-) diff --git a/space/space.cpp b/space/space.cpp index ba64913..6130e69 100644 --- a/space/space.cpp +++ b/space/space.cpp @@ -1284,6 +1284,8 @@ Space::TableType * Space::get_table() return type == type_table ? &value.value_table : nullptr; } + + bool Space::is_equal(const char * val) const { if( type == type_string ) @@ -1326,6 +1328,65 @@ bool Space::is_equal(const std::wstring & val) const +bool Space::has_value(const char * val) const +{ + if( type == type_string ) + { + return value.value_string == val; + } + + if( type == type_table ) + { + for(size_t i=0 ; i < value.value_table.size() ; ++i) + { + PT::Space * table_item = value.value_table[i]; + + if( table_item->type == type_string ) + { + return table_item->value.value_string == val; + } + } + } + + return false; +} + +bool Space::has_value(const std::string & val) const +{ + return has_value(val.c_str()); +} + + +bool Space::has_value(const wchar_t * val) const +{ + if( type == type_wstring ) + { + return value.value_wstring == val; + } + + if( type == type_table ) + { + for(size_t i=0 ; i < value.value_table.size() ; ++i) + { + PT::Space * table_item = value.value_table[i]; + + if( table_item->type == type_wstring ) + { + return table_item->value.value_wstring == val; + } + } + } + + return false; +} + + +bool Space::has_value(const std::wstring & val) const +{ + return has_value(val.c_str()); +} + + @@ -1618,6 +1679,60 @@ bool Space::is_equal(const wchar_t * field, const std::wstring & val) const +bool Space::has_value(const wchar_t * field, const char * val) const +{ + const Space * space = get_object_field(field); + + if( space ) + { + return space->has_value(val); + } + + return false; +} + +bool Space::has_value(const wchar_t * field, const std::string & val) const +{ + const Space * space = get_object_field(field); + + if( space ) + { + return space->has_value(val); + } + + return false; +} + + +bool Space::has_value(const wchar_t * field, const wchar_t * val) const +{ + const Space * space = get_object_field(field); + + if( space ) + { + return space->has_value(val); + } + + return false; +} + + +bool Space::has_value(const wchar_t * field, const std::wstring & val) const +{ + const Space * space = get_object_field(field); + + if( space ) + { + return space->has_value(val); + } + + return false; +} + + + + + Space * Space::find_child_space(const wchar_t * name) { return find_child_space_generic(name); @@ -1661,6 +1776,46 @@ Space & Space::find_add_child_space(const std::wstring & name) } +void Space::remove_child_space(const wchar_t * name) +{ + if( child_spaces ) + { + for(size_t i=0 ; isize() ; ) + { + PT::Space * child = (*child_spaces)[i]; + + if( child->name && (*child->name) == name ) + { + delete child; + child = nullptr; + child_spaces->erase(child_spaces->begin() + i); + } + else + { + ++i; + } + } + } +} + +void Space::remove_child_space(const std::wstring & name) +{ + return remove_child_space(name.c_str()); +} + + +void Space::remove_child_space(size_t index) +{ + if( child_spaces && index < child_spaces->size() ) + { + PT::Space * child = (*child_spaces)[index]; + delete child; + child = nullptr; + + child_spaces->erase(child_spaces->begin() + index); + } +} + diff --git a/space/space.h b/space/space.h index 02375e3..e8e2c88 100644 --- a/space/space.h +++ b/space/space.h @@ -294,11 +294,6 @@ public: Space & add_empty_space(const std::wstring & field); // IMPROVEME rename me to something better - // for child spaces (used only in Space format) - // rename to something better - Space & add_child_space(const wchar_t * space_name); - - // IMPROVEME add a similar 'set' method and cctor template Space & add_stream(const wchar_t * field, StreamType & str) @@ -414,11 +409,17 @@ public: ObjectType * get_object(); TableType * get_table(); + // may a better name? bool is_equal(const char * val) const; bool is_equal(const std::string & val) const; bool is_equal(const wchar_t * val) const; bool is_equal(const std::wstring & val) const; - // add these is_equal with std::wstring + + // may a better name? + bool has_value(const char * val) const; + bool has_value(const std::string & val) const; + bool has_value(const wchar_t * val) const; + bool has_value(const std::wstring & val) const; // what about getters from tables? @@ -540,21 +541,36 @@ public: } + // add this method with field with std::wstring bool is_equal(const wchar_t * field, const char * val) const; bool is_equal(const wchar_t * field, const std::string & val) const; bool is_equal(const wchar_t * field, const wchar_t * val) const; bool is_equal(const wchar_t * field, const std::wstring & val) const; - // add these is_equal with std::wstring + + // may a better name? + // add this method with field with std::wstring + bool has_value(const wchar_t * field, const char * val) const; + bool has_value(const wchar_t * field, const std::string & val) const; + bool has_value(const wchar_t * field, const wchar_t * val) const; + bool has_value(const wchar_t * field, const std::wstring & val) const; + // for child spaces (used only in Space format) Space * find_child_space(const wchar_t * name); Space * find_child_space(const std::wstring & name); const Space * find_child_space(const wchar_t * name) const; const Space * find_child_space(const std::wstring & name) const; + Space & add_child_space(const wchar_t * space_name); + Space & find_add_child_space(const wchar_t * name); Space & find_add_child_space(const std::wstring & name); + void remove_child_space(const wchar_t * name); + void remove_child_space(const std::wstring & name); + void remove_child_space(size_t index); + + protected: