From 22cda8daf047594e7653b16be9d7222a0e36dabb Mon Sep 17 00:00:00 2001 From: 3lswear Date: Fri, 21 Jan 2022 18:12:10 +0300 Subject: [PATCH] split class intermediate --- src/config/TOMLNode.cpp | 166 ++++++++++++++++++++++++++++++++++++++++ src/config/TOMLNode.hpp | 150 +++--------------------------------- 2 files changed, 178 insertions(+), 138 deletions(-) create mode 100644 src/config/TOMLNode.cpp diff --git a/src/config/TOMLNode.cpp b/src/config/TOMLNode.cpp new file mode 100644 index 0000000..0afc7fb --- /dev/null +++ b/src/config/TOMLNode.cpp @@ -0,0 +1,166 @@ +#include "webserv.hpp" +#include "TOMLNode.hpp" + +/* toml_node::toml_node(void) */ +/* { */ +/* return; */ +/* } */ + +/* toml_node::toml_node(const toml_node &src) */ +/* { */ +/* *this = src; */ +/* return; */ +/* } */ + +/* toml_node::~toml_node(void) */ +/* { */ +/* return; */ +/* } */ + +/* toml_node &toml_node::operator=(const toml_node &rhs) */ +/* { */ +/* //code */ +/* return (*this); */ +/* } */ + +toml_node::e_type toml_node::get_type(void) +{ + return (type); +} + +TOMLMap *toml_node::getMap(void) +{ + return (value.map); +} + +TOMLMapArray *toml_node::getMapArray(void) +{ + return (value.map_array); +} + +void toml_node::setString(std::string *str) +{ + value.str = str; + type = STRING; +} + +void toml_node::setNumber(int num) +{ + value.integer = num; + type = NUM; +} + +void toml_node::setArr(TOMLArray *toml_array) +{ + value.array = toml_array; + type = ARRAY; +} +void toml_node::setBool(bool b) +{ + value.boolean = b; + type = BOOL; +} +void toml_node::setNil(void) +{ + type = NIL; +} + +void toml_node::setObject(TOMLMap *obj) +{ + value.map = obj; + type = MAP; +} + +void toml_node::setMapArray(TOMLMapArray *map_array) +{ + value.map_array = map_array; + type = MAPARRAY; +} + +std::string *toml_node::toString(void) const +{ + switch (type) + { + case STRING: + { + return (value.str); + } + case NUM: + { + std::stringstream ss; + ss << value.integer; + std::string *result = new std::string(); + ss >> *result; + return (result); + } + case ARRAY: + { + TOMLArray::iterator it; + std::string *result = new std::string("[ "); + for (it = value.array->begin(); it != value.array->end(); ++it) + { + *result += *((*it)->toString()); + *result += ", "; + } + *result += " ]"; + return (result); + } + case MAP: + { + return (TOMLMap_to_string(value.map)); + } + case MAPARRAY: + { + std::stringstream ss; + std::string *result = new std::string(); + TOMLMapArray::iterator it; + TOMLMapArray *map_array = value.map_array; + + ss << "[\n"; + for (it = map_array->begin(); it != map_array->end(); ++it) + { + ss << (*TOMLMap_to_string(*it)); + ss << ", " << std::endl; + } + ss << "]\n"; + + + /* ss >> *result; */ + *result = ss.str(); + return (result); + } + case BOOL: + { + std::string *result; + if (value.boolean) + result = new std::string("true"); + else + result = new std::string("false"); + return (result); + } + default: + return ( new std::string("Not implemented :)")); + } +} + +std::string *TOMLMap_to_string(TOMLMap *map) +{ + std::stringstream ss; + std::string *result = new std::string(); + TOMLMap::iterator it; + + ss << "{\n"; + for (it = map->begin(); it != map->end(); ++it) + { + ss << it->first + << ": " + << *(it->second->toString()) + << std::endl; + } + + ss << "}" << std::endl; + + /* ss >> *result; */ + *result = ss.str(); + return (result); +} diff --git a/src/config/TOMLNode.hpp b/src/config/TOMLNode.hpp index 2cf2c64..8f0fd45 100644 --- a/src/config/TOMLNode.hpp +++ b/src/config/TOMLNode.hpp @@ -11,9 +11,6 @@ #include #include - - - class toml_node { @@ -37,145 +34,22 @@ class toml_node STRING, NUM, BOOL, ARRAY, MAP, MAPARRAY, NIL } type; - enum e_type get_type(void) - { - return (type); - } + enum e_type get_type(void); - TOMLMap *getMap(void) - { - return (value.map); - } + TOMLMap *getMap(void); + TOMLMapArray *getMapArray(void); - TOMLMapArray *getMapArray(void) - { - return (value.map_array); - } + void setString(std::string *str); + void setNumber(int num); - void setString(std::string *str) - { - value.str = str; - type = STRING; - } + void setArr(TOMLArray *toml_array); + void setBool(bool b); + void setNil(void); + void setObject(TOMLMap *obj); + void setMapArray(TOMLMapArray *map_array); - void setNumber(int num) - { - value.integer = num; - type = NUM; - } - - void setArr(TOMLArray *toml_array) - { - value.array = toml_array; - type = ARRAY; - } - void setBool(bool b) - { - value.boolean = b; - type = BOOL; - } - void setNil(void) - { - type = NIL; - } - void setObject(TOMLMap *obj) - { - value.map = obj; - type = MAP; - } - void setMapArray(TOMLMapArray *map_array) - { - value.map_array = map_array; - type = MAPARRAY; - } - - static std::string *TOMLMap_to_string(TOMLMap *map) - { - std::stringstream ss; - std::string *result = new std::string(); - TOMLMap::iterator it; - - ss << "{\n"; - for (it = map->begin(); it != map->end(); ++it) - { - ss << it->first - << ": " - << *(it->second->toString()) - << std::endl; - } - - ss << "}" << std::endl; - - /* ss >> *result; */ - *result = ss.str(); - return (result); - } - - std::string *toString(void) const - { - switch (type) - { - case STRING: - { - return (value.str); - } - case NUM: - { - std::stringstream ss; - ss << value.integer; - std::string *result = new std::string(); - ss >> *result; - return (result); - } - case ARRAY: - { - TOMLArray::iterator it; - std::string *result = new std::string("[ "); - for (it = value.array->begin(); it != value.array->end(); ++it) - { - *result += *((*it)->toString()); - *result += ", "; - } - *result += " ]"; - return (result); - } - case MAP: - { - return (TOMLMap_to_string(value.map)); - } - case MAPARRAY: - { - std::stringstream ss; - std::string *result = new std::string(); - TOMLMapArray::iterator it; - TOMLMapArray *map_array = value.map_array; - - ss << "[\n"; - for (it = map_array->begin(); it != map_array->end(); ++it) - { - ss << (*TOMLMap_to_string(*it)); - ss << ", " << std::endl; - } - ss << "]\n"; - - - /* ss >> *result; */ - *result = ss.str(); - return (result); - } - case BOOL: - { - std::string *result; - if (value.boolean) - result = new std::string("true"); - else - result = new std::string("false"); - return (result); - } - default: - return ( new std::string("Not implemented :)")); - } - } + std::string *TOMLMap_to_string(TOMLMap *map) const; + std::string *toString(void) const; }; #endif