filedat: added erase(), concatenate() and operators
This commit is contained in:
parent
bc2a0439fa
commit
d4d3d90094
3 changed files with 118 additions and 21 deletions
3
Makefile
3
Makefile
|
|
@ -46,7 +46,8 @@ shared: $(OBJ_SHARED)
|
||||||
|
|
||||||
install: all
|
install: all
|
||||||
mv libztd.a libztd.so /usr/lib
|
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:
|
uninstall:
|
||||||
rm /usr/lib/libztd.*
|
rm /usr/lib/libztd.*
|
||||||
|
|
|
||||||
|
|
@ -178,7 +178,18 @@ namespace ztd
|
||||||
@param vec Vector of data to add
|
@param vec Vector of data to add
|
||||||
*/
|
*/
|
||||||
inline void add(std::vector<chunkdat> const& vec) { addToList(vec); }
|
inline void add(std::vector<chunkdat> 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
|
//! @brief Create a copy of the chunk
|
||||||
inline chunkdat copy() const { return chunkdat(*this); }
|
inline chunkdat copy() const { return chunkdat(*this); }
|
||||||
|
|
@ -214,27 +225,26 @@ namespace ztd
|
||||||
*/
|
*/
|
||||||
chunkdat* subChunkPtr(const unsigned int a) const;
|
chunkdat* subChunkPtr(const unsigned int a) const;
|
||||||
|
|
||||||
//! @brief Reference to subchunk of map
|
//! @brief Reference to subchunk of map. @see subChunkRef(std::string const& a) const
|
||||||
/*!
|
|
||||||
@see subChunkRef(std::string const& a) const
|
|
||||||
*/
|
|
||||||
inline chunkdat& operator[](std::string const& a) const { return subChunkRef(a); }
|
inline chunkdat& operator[](std::string const& a) const { return subChunkRef(a); }
|
||||||
//! @brief Reference to subchunk of Map
|
//! @brief Reference to subchunk of list. @see subChunkRef(const unsigned int a) const
|
||||||
/*!
|
|
||||||
@see subChunkRef(const unsigned int a) const
|
|
||||||
*/
|
|
||||||
inline chunkdat& operator[](const unsigned int a) const { return subChunkRef(a); }
|
inline chunkdat& operator[](const unsigned int a) const { return subChunkRef(a); }
|
||||||
//! @brief Copy contents of chunk
|
//! @brief Set chunk data and return *this. @see set(chunkdat const& in)
|
||||||
inline chunkdat& operator=(chunkdat const& a) { set(a); return *this; }
|
inline chunkdat& operator=(chunkdat const& a) { set(a); return *this; }
|
||||||
//! @brief add() and return *this
|
//! @brief add and return *this. @see add(std::string const& name, chunkdat const& val)
|
||||||
inline chunkdat& operator+=(std::pair<std::string, chunkdat> const& a) { add(a.first, a.second); return *this; }
|
inline chunkdat& operator+=(std::pair<std::string, chunkdat> const& a) { add(a.first, a.second); return *this; }
|
||||||
//! @brief add() and return *this
|
//! @brief add and return *this. @see add(std::vector<std::pair<std::string, chunkdat>> const& vec)
|
||||||
inline chunkdat& operator+=(std::vector<std::pair<std::string, chunkdat>> const& a) { add(a); return *this; }
|
inline chunkdat& operator+=(std::vector<std::pair<std::string, chunkdat>> 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; }
|
inline chunkdat& operator+=(chunkdat const& a) { add(a); return *this; }
|
||||||
//! @brief add() and return *this
|
//! @brief add and return *this. @see add(std::vector<chunkdat> const& vec)
|
||||||
inline chunkdat& operator+=(std::vector<chunkdat> const& a) { add(a); return *this; }
|
inline chunkdat& operator+=(std::vector<chunkdat> 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*
|
//add operator+ and operator*
|
||||||
|
|
||||||
|
|
@ -246,6 +256,21 @@ namespace ztd
|
||||||
chunk_abstract* m_achunk;
|
chunk_abstract* m_achunk;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//! @brief add
|
||||||
|
inline chunkdat operator+(const chunkdat& a, const std::pair<std::string, chunkdat>& b) { chunkdat ret(a); ret += b; return ret; }
|
||||||
|
//! @brief add
|
||||||
|
inline chunkdat operator+(const chunkdat& a, const std::vector<std::pair<std::string, chunkdat>>& 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<chunkdat>& 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
|
//! @brief File data object
|
||||||
/*!
|
/*!
|
||||||
Object for importing, reading, altering and writing of file data\n
|
Object for importing, reading, altering and writing of file data\n
|
||||||
|
|
|
||||||
|
|
@ -651,6 +651,77 @@ void ztd::chunkdat::addToList(std::vector<chunkdat> const& 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<chunk_map*>(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<chunk_list*>(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<chunk_string*>(chk.getp());
|
||||||
|
ztd::chunk_string* cc = dynamic_cast<chunk_string*>(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<chunk_map*>(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<chunk_list*>(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
|
std::string ztd::chunkdat::strval(unsigned int alignment, std::string const& aligner) const
|
||||||
{
|
{
|
||||||
if(this->type()==ztd::chunk_abstract::string)
|
if(this->type()==ztd::chunk_abstract::string)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue