filedat: better set() and add copy()
This commit is contained in:
parent
3303ba8db0
commit
50752560c5
2 changed files with 54 additions and 5 deletions
|
|
@ -105,7 +105,8 @@ namespace ztd
|
|||
chunkdat(const char* in);
|
||||
//! @brief Constructor with initial value
|
||||
chunkdat(std::string const& in);
|
||||
chunkdat(const char* in, const int in_size, int offset=0, filedat* data=nullptr);
|
||||
//! @brief Constructor with initial value
|
||||
chunkdat(const char* in, const int in_size, int offset=0, filedat* parent=nullptr);
|
||||
//! @brief Constructor with copy
|
||||
chunkdat(chunkdat const& in);
|
||||
//dtor
|
||||
|
|
@ -137,12 +138,17 @@ namespace ztd
|
|||
/*!
|
||||
@param in String data
|
||||
@param offset Used for debugging
|
||||
@param data Used for debuggingy
|
||||
@param data Used for debugging
|
||||
*/
|
||||
inline void set(std::string const& in, int offset=0, filedat* data=nullptr) { this->set(in.c_str(), in.size(), offset, data); }
|
||||
inline void set(std::string const& in, int offset=0, filedat* parent=nullptr) { this->set(in.c_str(), in.size(), offset, parent); }
|
||||
//! @brief Copy chunk data
|
||||
void set(chunkdat const& in);
|
||||
|
||||
//! @brief Create a copy of the chunk
|
||||
inline chunkdat copy() { return chunkdat(*this); }
|
||||
//! @brief Create a pointed copy of the chunk. This needs to be deleted manually
|
||||
inline chunkdat* pcopy() { return new chunkdat(*this); }
|
||||
|
||||
//! @brief Copy chunk's data
|
||||
inline void set(chunkdat const& in) { this->set(in.strval(), in.offset(), in.parent()); } // TODO
|
||||
|
||||
//! @brief Get string value of data
|
||||
/*!
|
||||
|
|
|
|||
|
|
@ -596,6 +596,49 @@ void ztd::chunkdat::set(const char* in, const int in_size, int offset, filedat*
|
|||
}
|
||||
}
|
||||
|
||||
void ztd::chunkdat::set(ztd::chunkdat const& in)
|
||||
{
|
||||
// reset everything
|
||||
this->clear();
|
||||
// trace info
|
||||
m_offset=in.m_offset;
|
||||
m_parent=in.m_parent;
|
||||
|
||||
// case copy
|
||||
if(in.type()==ztd::chunk_abstract::map) //map
|
||||
{
|
||||
ztd::chunk_map* cc = dynamic_cast<chunk_map*>(in.getp());
|
||||
ztd::chunk_map* tch = new ztd::chunk_map();
|
||||
for(auto it : cc->values)
|
||||
{
|
||||
tch->values.insert( std::make_pair(it.first, it.second->pcopy()) );
|
||||
}
|
||||
}
|
||||
else if(in.type()==ztd::chunk_abstract::list) //list
|
||||
{
|
||||
ztd::chunk_list* cc = dynamic_cast<chunk_list*>(in.getp());
|
||||
ztd::chunk_list* tch = new ztd::chunk_list();
|
||||
for(auto it : cc->list)
|
||||
{
|
||||
tch->list.push_back(it->pcopy());
|
||||
}
|
||||
}
|
||||
else if(in.type()==ztd::chunk_abstract::string) //string
|
||||
{
|
||||
ztd::chunk_string* cc = dynamic_cast<chunk_string*>(in.getp());
|
||||
ztd::chunk_string* tch = new ztd::chunk_string();
|
||||
tch->val = std::string(cc->val);
|
||||
m_achunk = tch;
|
||||
}
|
||||
else //none
|
||||
{
|
||||
//already cleared: do nothing
|
||||
return;
|
||||
}
|
||||
|
||||
this->set(in.strval(), in.offset(), in.parent());
|
||||
}
|
||||
|
||||
void ztd::chunkdat::addToMap(std::string const& name, chunkdat const& val)
|
||||
{
|
||||
if(this->type()==ztd::chunk_abstract::map)
|
||||
|
|
|
|||
Loading…
Reference in a new issue