filedat: add getist() and getmap()

This commit is contained in:
zawz 2020-05-22 16:09:07 +02:00
parent 09c4bdb692
commit 8e7ec95e05
4 changed files with 45 additions and 27 deletions

View file

@ -6,7 +6,7 @@ ODIR_SHARED=obj_so
NAME = libztd
CC=g++
CXXFLAGS= -I$(IDIR) -Wall -pedantic -std=c++17 -O2
CXXFLAGS= -I$(IDIR) -Wall -std=c++17 -O2
$(shell mkdir -p $(ODIR))
$(shell mkdir -p $(ODIR_SHARED))
@ -41,9 +41,9 @@ static: $(OBJ)
shared: $(OBJ_SHARED)
$(CC) -shared -o libztd.so $^
install: all
install:
mkdir -p $(INSTALL)/usr/lib
mv libztd.a libztd.so $(INSTALL)/usr/lib
cp libztd.a libztd.so $(INSTALL)/usr/lib
mkdir -p $(INSTALL)/usr/include/ztd
cp -r include/* $(INSTALL)/usr/include/ztd

View file

@ -209,6 +209,8 @@ namespace ztd
//! @brief Erase index from list
void erase(const unsigned int index);
std::vector<ztd::chunkdat*> getlist();
std::map<std::string, ztd::chunkdat*> getmap();
//! @brief Create a copy of the chunk
inline chunkdat copy() const { return chunkdat(*this); }

View file

@ -131,7 +131,7 @@ namespace ztd
inline int pclose2(FILE* fd, pid_t pid) { return eclose(fd, pid); }
/// exec extensions
// exec extensions
//! @brief Execute a binary and retrieve its outputs
/*!
@ -139,8 +139,9 @@ namespace ztd
@param args Arguments given to the binary
*/
std::pair<std::string, int> exec(std::string const& bin, std::vector<std::string> const& args);
template<class... Args>
//! @brief Variadic call of exec()
template<class... Args>
std::pair<std::string, int> exec(std::string const& bin, Args... args) { std::vector<std::string> rargs = { static_cast<std::string>(args)...}; return exec(bin, rargs); }
//! @brief Execute string as a binary file
@ -149,6 +150,7 @@ namespace ztd
@param args Arguments given to the file
*/
std::pair<std::string, int> script(std::string const& data, std::vector<std::string> const& args);
//! @brief Variadic call of script()
template<class... Args>
std::pair<std::string, int> script(std::string const& data, Args... args) { std::vector<std::string> rargs = { static_cast<std::string>(args)...}; return script(data, rargs); }

View file

@ -73,14 +73,19 @@ std::string ztd::filedat::removeComments(std::string str)
uint32_t i=0;
while(i < str.size())
{
if( str[i] == '"') // double quotes
if( str[i] == '\\')
{
//skip checking char
i++;
}
else if( str[i] == '"') // double quotes
{
uint32_t j=i;
i++;
while(i < str.size() && str[i]!='"') // until end of quote
{
if(i+1 < str.size() && str[i] == '\\' && str[i+1] == '"') //escaped quote
i++; //ignore backslash
i++; //ignore backslash
i++; // add char and increment
}
@ -275,12 +280,12 @@ static std::tuple<std::string, std::string, int, int, bool> _getstrval(const std
while(i < str.size() && str[i]!='"') // until end of quote
{
if(i+1 < str.size() && str[i] == '\\' && str[i+1] == '"') //escaped quote
i++; //ignore backslash
i++; //ignore backslash
val.push_back(str[i++]); // add char and increment
}
if(i >= str.size()) // quote didn't end
throw ztd::format_error("Double quote doesn't close", "", str, j);
throw ztd::format_error("Double quote doesn't close", "", str, j);
i++;
}
else if( str[i] == '\'') // single quotes
@ -290,12 +295,12 @@ static std::tuple<std::string, std::string, int, int, bool> _getstrval(const std
while(i < str.size() && str[i]!='\'') // until end of quote
{
if(i+1 < str.size() && str[i] == '\\' && str[i+1] == '\'') //escaped quote
i++; //ignore backslash
i++; //ignore backslash
val += str[i++]; // add char
}
if(i >= str.size()) // quote didn't end
throw ztd::format_error("Single quote doesn't close", "", str, j);
throw ztd::format_error("Single quote doesn't close", "", str, j);
i++;
}
if(str[i] == '{') // {} map
@ -830,6 +835,31 @@ void ztd::chunkdat::erase(const unsigned int index)
}
}
std::vector<ztd::chunkdat*> ztd::chunkdat::getlist()
{
if(this->type()!=ztd::chunk_abstract::list)
{
if(m_parent != nullptr)
throw ztd::format_error("chunkdat isn't a list", m_parent->filePath(), m_parent->im_data(), m_offset );
else
throw ztd::format_error("chunkdat isn't a list", "", this->strval(), -1);
}
ztd::chunk_list* cl = dynamic_cast<chunk_list*>(m_achunk);
return cl->list;
}
std::map<std::string, ztd::chunkdat*> ztd::chunkdat::getmap()
{
if(this->type()!=ztd::chunk_abstract::map)
{
if(m_parent != nullptr)
throw ztd::format_error("chunkdat isn't a map", m_parent->filePath(), m_parent->im_data(), m_offset );
else
throw ztd::format_error("chunkdat isn't a map", "", this->strval(), -1);
}
ztd::chunk_map* dc = dynamic_cast<chunk_map*>(m_achunk);
return dc->values;
}
std::string ztd::chunkdat::strval(unsigned int alignment, std::string const& aligner) const
{
if(this->type()==ztd::chunk_abstract::string)
@ -941,26 +971,18 @@ ztd::chunkdat& ztd::chunkdat::subChunkRef(std::string const& in) const
if(this->type()!=ztd::chunk_abstract::map)
{
if(m_parent != nullptr)
{
throw ztd::format_error("chunkdat isn't a map", m_parent->filePath(), m_parent->im_data(), m_offset );
}
else
{
throw ztd::format_error("chunkdat isn't a map", "", this->strval(), -1);
}
}
ztd::chunk_map* dc = dynamic_cast<chunk_map*>(m_achunk);
auto fi = dc->values.find(in);
if(fi == dc->values.end())
{
if(m_parent != nullptr)
{
throw ztd::format_error("Map doesn't have '" + in + "' flag", m_parent->filePath(), m_parent->im_data(), m_offset );
}
else
{
throw ztd::format_error("Map doesn't have '" + in + "' flag", "", this->strval(), -1);
}
}
return *fi->second;
}
@ -970,25 +992,17 @@ ztd::chunkdat& ztd::chunkdat::subChunkRef(const unsigned int a) const
if(this->type()!=ztd::chunk_abstract::list)
{
if(m_parent != nullptr)
{
throw ztd::format_error("chunkdat isn't a list", m_parent->filePath(), m_parent->im_data(), m_offset );
}
else
{
throw ztd::format_error("chunkdat isn't a list", "", this->strval(), -1);
}
}
ztd::chunk_list* cl = dynamic_cast<chunk_list*>(m_achunk);
if(a >= cl->list.size())
{
if(m_parent != nullptr)
{
throw ztd::format_error("List size is below " + std::to_string(a), m_parent->filePath(), m_parent->im_data(), m_offset );
}
else
{
throw ztd::format_error("List size is below " + std::to_string(a), "", this->strval(), -1);
}
}
return *cl->list[a];
}