mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-28 21:07:59 +03:00
Merge remote-tracking branch 'origin/roman' into roman
This commit is contained in:
166
src/config/TOMLNode.cpp
Normal file
166
src/config/TOMLNode.cpp
Normal file
@@ -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);
|
||||
}
|
||||
@@ -11,9 +11,6 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user