From d4d3d90094d44e7cef5976d6217947e3cd04f5e8 Mon Sep 17 00:00:00 2001 From: zawz Date: Fri, 16 Aug 2019 19:09:08 +0200 Subject: [PATCH] filedat: added erase(), concatenate() and operators --- Makefile | 3 +- include/filedat.hpp | 61 +++++++++++++++++++++++++----------- src/filedat.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 118 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 7cd1cc9..aa37d1c 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,8 @@ shared: $(OBJ_SHARED) install: all mv libztd.a libztd.so /usr/lib - cp -r include /usr/include/ztd + mkdir -p /usr/include/ztd + cp -r include/* /usr/include/ztd uninstall: rm /usr/lib/libztd.* diff --git a/include/filedat.hpp b/include/filedat.hpp index a7d5799..6b7d777 100644 --- a/include/filedat.hpp +++ b/include/filedat.hpp @@ -178,7 +178,18 @@ namespace ztd @param vec Vector of data to add */ inline void add(std::vector const& vec) { addToList(vec); } - // void concatenate(chunkdat const& chk); //concatenates chunks + //! @brief Concatenate chunks of data + /*! Effective only if the two chunks are of the same type\n + Map: combines two maps into a single map\n + List: combines into a single list\n + String: Concatenate strings + */ + void concatenate(chunkdat const& chk); + + void erase(const std::string& key); + + void erase(const unsigned int index); + //! @brief Create a copy of the chunk inline chunkdat copy() const { return chunkdat(*this); } @@ -214,27 +225,26 @@ namespace ztd */ chunkdat* subChunkPtr(const unsigned int a) const; - //! @brief Reference to subchunk of map - /*! - @see subChunkRef(std::string const& a) const - */ - inline chunkdat& operator[](std::string const& a) const { return subChunkRef(a); } - //! @brief Reference to subchunk of Map - /*! - @see subChunkRef(const unsigned int a) const - */ - inline chunkdat& operator[](const unsigned int a) const { return subChunkRef(a); } - //! @brief Copy contents of chunk - inline chunkdat& operator=(chunkdat const& a) { set(a); return *this; } - //! @brief add() and return *this + //! @brief Reference to subchunk of map. @see subChunkRef(std::string const& a) const + inline chunkdat& operator[](std::string const& a) const { return subChunkRef(a); } + //! @brief Reference to subchunk of list. @see subChunkRef(const unsigned int a) const + inline chunkdat& operator[](const unsigned int a) const { return subChunkRef(a); } + //! @brief Set chunk data and return *this. @see set(chunkdat const& in) + inline chunkdat& operator=(chunkdat const& a) { set(a); return *this; } + //! @brief add and return *this. @see add(std::string const& name, chunkdat const& val) inline chunkdat& operator+=(std::pair const& a) { add(a.first, a.second); return *this; } - //! @brief add() and return *this + //! @brief add and return *this. @see add(std::vector> const& vec) inline chunkdat& operator+=(std::vector> const& a) { add(a); return *this; } - //! @brief add() and return *this + //! @brief add and return *this. @see add(chunkdat const& val) inline chunkdat& operator+=(chunkdat const& a) { add(a); return *this; } - //! @brief add() and return *this + //! @brief add and return *this. @see add(std::vector const& vec) inline chunkdat& operator+=(std::vector const& a) { add(a); return *this; } - // inline bool operator*=(chunkdat const& a) { concatenate(a); } + //! @brief concatenate and return *this. @see concatenate(chunkdat const& chk) + inline chunkdat& operator*=(chunkdat const& a) { concatenate(a); return *this; } + //! @brief remove and return *this. @see remove(const std::string& key) + inline chunkdat& operator-=(const std::string& key) { erase(key); return *this; } + //! @brief remove and return *this. @see remove(const unsigned int index) + inline chunkdat& operator-=(const unsigned int index) { erase(index); return *this; } //add operator+ and operator* @@ -246,6 +256,21 @@ namespace ztd chunk_abstract* m_achunk; }; + //! @brief add + inline chunkdat operator+(const chunkdat& a, const std::pair& b) { chunkdat ret(a); ret += b; return ret; } + //! @brief add + inline chunkdat operator+(const chunkdat& a, const std::vector>& b) { chunkdat ret(a); ret += b; return ret; } + //! @brief add + inline chunkdat operator+(const chunkdat& a, const chunkdat& b) { chunkdat ret(a); ret += b; return ret; } + //! @brief add + inline chunkdat operator+(const chunkdat& a, const std::vector& b) { chunkdat ret(a); ret += b; return ret; } + //! @brief concatenated chunk + inline chunkdat operator*(const chunkdat& a, const chunkdat& b) { chunkdat ret(a); ret *= b; return ret; } + //! @brief substract + inline chunkdat operator-(const chunkdat& a, const std::string& b) { chunkdat ret(a); ret -= b; return ret; } + //! @brief substract + inline chunkdat operator-(const chunkdat& a, const unsigned int b) { chunkdat ret(a); ret -= b; return ret; } + //! @brief File data object /*! Object for importing, reading, altering and writing of file data\n diff --git a/src/filedat.cpp b/src/filedat.cpp index 13db3c6..6fdbf44 100644 --- a/src/filedat.cpp +++ b/src/filedat.cpp @@ -623,7 +623,7 @@ void ztd::chunkdat::addToMap(std::string const& name, chunkdat const& val) void ztd::chunkdat::addToMap(std::vector> const& vec) { for(auto it : vec) - this->addToMap(it.first, it.second); + this->addToMap(it.first, it.second); } void ztd::chunkdat::addToList(chunkdat const& val) @@ -648,7 +648,78 @@ void ztd::chunkdat::addToList(chunkdat const& val) void ztd::chunkdat::addToList(std::vector const& vec) { for(auto it : vec) - this->addToList(it); + this->addToList(it); +} + +void ztd::chunkdat::concatenate(chunkdat const& chk) +{ + if(this->type() == ztd::chunk_abstract::none) //nothing: copy + { + this->set(chk); + } + else if(this->type()==ztd::chunk_abstract::map && chk.type()==ztd::chunk_abstract::map) //map + { + ztd::chunk_map* cc = dynamic_cast(chk.getp()); + for(auto it : cc->values) + { + this->add(it.first, *it.second); + } + } + else if(this->type()==ztd::chunk_abstract::list && chk.type()==ztd::chunk_abstract::list) //list + { + ztd::chunk_list* cc = dynamic_cast(chk.getp()); + for(auto it : cc->list) + { + this->add(*it); + } + } + else if(this->type()==ztd::chunk_abstract::string && chk.type()==ztd::chunk_abstract::string) //string + { + ztd::chunk_string* ci = dynamic_cast(chk.getp()); + ztd::chunk_string* cc = dynamic_cast(m_achunk); + cc->val += ci->val; + } + else + { + throw ztd::format_error("Cannot concatenate chunks of different types", "", "", -1); + } +} + +void ztd::chunkdat::erase(const std::string& key) +{ + if(this->type()==ztd::chunk_abstract::map) + { + ztd::chunk_map* cp = dynamic_cast(m_achunk); + auto it = cp->values.find(key); + if( it == cp->values.end() ) + { + throw ztd::format_error("Key '" + key + "' not present", "", this->strval(), -1); + } + delete it->second; + cp->values.erase(it); + } + else + { + throw ztd::format_error("Cannot erase element from non-map chunk", "", this->strval(), -1); + } +} + +void ztd::chunkdat::erase(const unsigned int index) +{ + if(this->type()==ztd::chunk_abstract::list) + { + if(index >= (unsigned int) this->listSize()) + { + throw ztd::format_error("Cannot erase out of bonds: "+std::to_string(index)+" in size "+std::to_string(this->listSize()), "", this->strval(), -1); + } + ztd::chunk_list* lp = dynamic_cast(m_achunk); + delete lp->list[index]; + lp->list.erase(lp->list.begin() + index); + } + else + { + throw ztd::format_error("Cannot erase element from non-list chunk", "", this->strval(), -1); + } } std::string ztd::chunkdat::strval(unsigned int alignment, std::string const& aligner) const