feat: rewrite toSting to std::string

This commit is contained in:
3lswear
2022-02-20 19:59:23 +03:00
parent e0ac307f06
commit 8b2bfe6360
5 changed files with 28 additions and 29 deletions

View File

@@ -97,32 +97,32 @@ void toml_node::setMapArray(TOMLMapArray *map_array)
} }
std::string *toml_node::toString(void) const std::string toml_node::toString(void) const
{ {
switch (type) switch (type)
{ {
case STRING: case STRING:
{ {
return (value.str); return (*value.str);
} }
case NUM: case NUM:
{ {
std::stringstream ss; std::stringstream ss;
ss << value.integer; ss << value.integer;
std::string *result = new std::string(); std::string result;
ss >> *result; ss >> result;
return (result); return (result);
} }
case ARRAY: case ARRAY:
{ {
TOMLArray::iterator it; TOMLArray::iterator it;
std::string *result = new std::string("[ "); std::string result("[ ");
for (it = value.array->begin(); it != value.array->end(); ++it) for (it = value.array->begin(); it != value.array->end(); ++it)
{ {
*result += *((*it)->toString()); result += ((*it)->toString());
*result += ", "; result += ", ";
} }
*result += " ]"; result += " ]";
return (result); return (result);
} }
case MAP: case MAP:
@@ -132,41 +132,41 @@ std::string *toml_node::toString(void) const
case MAPARRAY: case MAPARRAY:
{ {
std::stringstream ss; std::stringstream ss;
std::string *result = new std::string(); std::string result;
TOMLMapArray::iterator it; TOMLMapArray::iterator it;
TOMLMapArray *map_array = value.map_array; TOMLMapArray *map_array = value.map_array;
ss << "[\n"; ss << "[\n";
for (it = map_array->begin(); it != map_array->end(); ++it) for (it = map_array->begin(); it != map_array->end(); ++it)
{ {
ss << (*TOMLMap_to_string(*it)); ss << (TOMLMap_to_string(*it));
ss << ", " << std::endl; ss << ", " << std::endl;
} }
ss << "]\n"; ss << "]\n";
/* ss >> *result; */ /* ss >> *result; */
*result = ss.str(); result = ss.str();
return (result); return (result);
} }
case BOOL: case BOOL:
{ {
std::string *result; std::string result;
if (value.boolean) if (value.boolean)
result = new std::string("true"); result = std::string("true");
else else
result = new std::string("false"); result = std::string("false");
return (result); return (result);
} }
default: default:
return ( new std::string("Not implemented :)")); return (std::string("Not implemented :)"));
} }
} }
std::string *toml_node::TOMLMap_to_string(TOMLMap *map) std::string toml_node::TOMLMap_to_string(TOMLMap *map)
{ {
std::stringstream ss; std::stringstream ss;
std::string *result = new std::string(); std::string result;
TOMLMap::iterator it; TOMLMap::iterator it;
ss << "{\n"; ss << "{\n";
@@ -174,13 +174,13 @@ std::string *toml_node::TOMLMap_to_string(TOMLMap *map)
{ {
ss << it->first ss << it->first
<< ": " << ": "
<< *(it->second->toString()) << (it->second->toString())
<< std::endl; << std::endl;
} }
ss << "}" << std::endl; ss << "}" << std::endl;
/* ss >> *result; */ /* ss >> *result; */
*result = ss.str(); result = ss.str();
return (result); return (result);
} }

View File

@@ -54,8 +54,8 @@ class toml_node
void setObject(TOMLMap *obj); void setObject(TOMLMap *obj);
void setMapArray(TOMLMapArray *map_array); void setMapArray(TOMLMapArray *map_array);
std::string *toString(void) const; std::string toString(void) const;
static std::string *TOMLMap_to_string(TOMLMap *map); static std::string TOMLMap_to_string(TOMLMap *map);
}; };

View File

@@ -20,19 +20,18 @@
/* } */ /* } */
namespace config namespace config
{ {
void display(TOMLMap *root_map) void display(TOMLMap *root_map)
{ {
std::cout << ">>> printing config: <<<" << std::endl; DBOUT << ">>> printing config: <<<" << std::endl;
TOMLMap::iterator it; TOMLMap::iterator it;
for (it = root_map->begin(); it != root_map->end(); ++it) for (it = root_map->begin(); it != root_map->end(); ++it)
{ {
std::cout << it->first DBOUT << it->first
<< ": " << ": "
<< *(it->second->toString()); << (it->second->toString());
std::cout << ", " << std::endl; DBOUT << ", " << std::endl;
/* << std::endl << "-------" << std::endl; */ /* << std::endl << "-------" << std::endl; */
} }
} }
@@ -42,6 +41,7 @@ namespace config
TOMLMap::iterator it; TOMLMap::iterator it;
DBOUT << ">>> cleaning up: <<<" << std::endl; DBOUT << ">>> cleaning up: <<<" << std::endl;
DBOUT << root << ENDL;
if (!root) if (!root)
return; return;
for (it = root->begin(); it != root->end(); ++it) for (it = root->begin(); it != root->end(); ++it)
@@ -52,7 +52,7 @@ namespace config
clean_generic(it->second); clean_generic(it->second);
/* delete it->second; */ /* delete it->second; */
std::cout << ", " << std::endl; DBOUT << ", " << std::endl;
} }
DBOUT << YELLO << "end of clean" << ENDL; DBOUT << YELLO << "end of clean" << ENDL;
root->clear(); root->clear();

View File

@@ -8,6 +8,5 @@
#include "TOMLParser.hpp" #include "TOMLParser.hpp"
TOMLMap *parse(char *filename); TOMLMap *parse(char *filename);
void display(TOMLMap *config);
#endif #endif

View File

@@ -9,7 +9,7 @@ typedef std::vector<toml_node *> TOMLArray;
namespace config namespace config
{ {
void display(TOMLMap *config);
void clean_parsed(TOMLMap *root); void clean_parsed(TOMLMap *root);
void clean_generic(toml_node *node); void clean_generic(toml_node *node);
} }