mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 05:17:59 +03:00
intermid commit
This commit is contained in:
66
src/config/TOMLNode.hpp
Normal file
66
src/config/TOMLNode.hpp
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
#include "webserv.hpp"
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
|
||||||
|
class toml_node;
|
||||||
|
|
||||||
|
/* typedef std::vector<TOMLMap *> TOMLArray; */
|
||||||
|
/* typedef std::vector<TOMLArray *> TOMLArrayOfMap; */
|
||||||
|
typedef std::map<std::string, toml_node *> TOMLMap; // = JSONObject
|
||||||
|
typedef std::vector<std::string, TOMLMap *> TOMLMapArray;
|
||||||
|
typedef std::vector<toml_node *> TOMLArray;
|
||||||
|
|
||||||
|
|
||||||
|
class toml_node
|
||||||
|
{
|
||||||
|
enum e_type
|
||||||
|
{
|
||||||
|
STRING, NUM, BOOL, ARRAY, MAP, MAPARRAY, NIL
|
||||||
|
} type;
|
||||||
|
|
||||||
|
union u_value
|
||||||
|
{
|
||||||
|
std::string *str;
|
||||||
|
int integer;
|
||||||
|
bool boolean;
|
||||||
|
/* std::vector<toml_node *> *array; */
|
||||||
|
TOMLArray *array;
|
||||||
|
/* std::map<std::string, toml_node *> *map; */
|
||||||
|
TOMLMap *map;
|
||||||
|
/* std::vector<std::map<std::string, toml_node> > *map_array; */
|
||||||
|
TOMLMapArray *map_array;
|
||||||
|
} value;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void setString(std::string *str)
|
||||||
|
{
|
||||||
|
value.str = str;
|
||||||
|
type = STRING;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
93
src/config/TOMLParser.hpp
Normal file
93
src/config/TOMLParser.hpp
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
#include "TOMLNode.hpp"
|
||||||
|
#include "Tokenizer.hpp"
|
||||||
|
|
||||||
|
namespace config
|
||||||
|
{
|
||||||
|
class TOMLParser
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::fstream file;
|
||||||
|
toml_node *root; //root of TOML tree
|
||||||
|
toml_node *current; //node currently being parsed
|
||||||
|
Tokenizer tokenizer;
|
||||||
|
|
||||||
|
public:
|
||||||
|
TOMLParser(const std::string filename) : tokenizer(filename) {}
|
||||||
|
void parse(void);
|
||||||
|
|
||||||
|
toml_node *parseObject(void);
|
||||||
|
toml_node *parseString(void);
|
||||||
|
toml_node *parseNumber(void);
|
||||||
|
toml_node *parseArray(void);
|
||||||
|
toml_node *parseBool(void);
|
||||||
|
toml_node *parseNil(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
void TOMLParser::parse(void)
|
||||||
|
{
|
||||||
|
std::string key;
|
||||||
|
key = "";
|
||||||
|
|
||||||
|
while (tokenizer.hasMoreTokens())
|
||||||
|
{
|
||||||
|
s_token token;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
token = tokenizer.getToken();
|
||||||
|
std::cout << token.to_string() << std::endl;
|
||||||
|
switch (token.type)
|
||||||
|
{
|
||||||
|
case ARR_OPEN:
|
||||||
|
{
|
||||||
|
toml_node *parsedObject = parseObject();
|
||||||
|
/* parsedObject->printNode(0); */
|
||||||
|
if (!root)
|
||||||
|
root = parsedObject;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case NUMBER:
|
||||||
|
{
|
||||||
|
tokenizer.rollBackToken();
|
||||||
|
toml_node *parsedNumber = parseNumber();
|
||||||
|
if (!root)
|
||||||
|
root = parsedNumber;
|
||||||
|
}
|
||||||
|
case STRING:
|
||||||
|
{
|
||||||
|
tokenizer.rollBackToken();
|
||||||
|
toml_node *parsedString = parseString();
|
||||||
|
if (!root)
|
||||||
|
root = parsedString;
|
||||||
|
}
|
||||||
|
case BOOL:
|
||||||
|
{
|
||||||
|
tokenizer.rollBackToken();
|
||||||
|
toml_node *parsedBool = parseBool();
|
||||||
|
if (!root)
|
||||||
|
root = parsedBool;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
throw std::logic_error("JOPA :(");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (std::logic_error err)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toml_node *TOMLParser::parseString(void)
|
||||||
|
{
|
||||||
|
toml_node *node;
|
||||||
|
std::string *sValue;
|
||||||
|
|
||||||
|
std::cout << "Parsing string" << std::endl;
|
||||||
|
s_token token = tokenizer.getToken();
|
||||||
|
sValue = new std::string(token.value);
|
||||||
|
node->setString(sValue);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
145
src/config/Tokenizer.hpp
Normal file
145
src/config/Tokenizer.hpp
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
#include "webserv.hpp"
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
|
||||||
|
namespace config
|
||||||
|
{
|
||||||
|
enum e_token
|
||||||
|
{
|
||||||
|
ASSIGN,
|
||||||
|
STRING,
|
||||||
|
NUMBER,
|
||||||
|
COMMA,
|
||||||
|
BOOL,
|
||||||
|
NIL,
|
||||||
|
ARR_OPEN,
|
||||||
|
ARR_CLOSE,
|
||||||
|
MAP_OPEN,
|
||||||
|
MAP_CLOSE,
|
||||||
|
MAPARRAY_OPEN,
|
||||||
|
MAPARRAY_CLOSE
|
||||||
|
};
|
||||||
|
|
||||||
|
struct s_token
|
||||||
|
{
|
||||||
|
std::string value;
|
||||||
|
e_token type;
|
||||||
|
std::string to_string(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
class Tokenizer
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::fstream file;
|
||||||
|
size_t prev_pos;
|
||||||
|
public:
|
||||||
|
Tokenizer(std::string filename);
|
||||||
|
char getWithoutWhiteSpace();
|
||||||
|
struct s_token getToken();
|
||||||
|
void *hasMoreTokens();
|
||||||
|
void rollBackToken();
|
||||||
|
};
|
||||||
|
|
||||||
|
char Tokenizer::getWithoutWhiteSpace(void)
|
||||||
|
{
|
||||||
|
char c = ' ';
|
||||||
|
while ((c == ' ' || c == '\n'))
|
||||||
|
{
|
||||||
|
file.get(c);
|
||||||
|
if ((c == ' ' || c == '\n') && !file.good())
|
||||||
|
{
|
||||||
|
throw std::logic_error("No more tokens!");
|
||||||
|
}
|
||||||
|
else if (!file.good())
|
||||||
|
return (c);
|
||||||
|
}
|
||||||
|
return (c);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Tokenizer::rollBackToken(void)
|
||||||
|
{
|
||||||
|
if (file.eof())
|
||||||
|
file.clear();
|
||||||
|
file.seekg(prev_pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct s_token Tokenizer::getToken(void)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
struct s_token token;
|
||||||
|
|
||||||
|
if (file.eof())
|
||||||
|
{
|
||||||
|
std::cout << "Tokens exhausted" << std::endl;
|
||||||
|
}
|
||||||
|
prev_pos = file.tellg();
|
||||||
|
c = getWithoutWhiteSpace();
|
||||||
|
|
||||||
|
if (c == '"')
|
||||||
|
{
|
||||||
|
token.type = STRING;
|
||||||
|
token.value = "";
|
||||||
|
/* TODO: maybe do-while? */
|
||||||
|
file.get(c);
|
||||||
|
while (c != '"')
|
||||||
|
{
|
||||||
|
token.value += c;
|
||||||
|
file.get(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == '[')
|
||||||
|
token.type = ARR_OPEN;
|
||||||
|
else if (c == ']')
|
||||||
|
token.type = ARR_CLOSE;
|
||||||
|
else if (c == '=')
|
||||||
|
token.type = ASSIGN;
|
||||||
|
else if (c == '-' || isdigit(c))
|
||||||
|
{
|
||||||
|
std::streampos prevCharPos;
|
||||||
|
token.type = NUMBER;
|
||||||
|
token.value = "";
|
||||||
|
token.value += c;
|
||||||
|
/* prevCharPos = file.tellg(); */
|
||||||
|
while (c == '-' || c == '.' || isdigit(c))
|
||||||
|
{
|
||||||
|
prevCharPos = file.tellg();
|
||||||
|
file.get(c);
|
||||||
|
if (file.eof())
|
||||||
|
break;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (c == '-' || c == '.' || isdigit(c))
|
||||||
|
token.value += c;
|
||||||
|
else
|
||||||
|
file.seekg(prevCharPos);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == 'f')
|
||||||
|
{
|
||||||
|
token.type = BOOL;
|
||||||
|
token.value = "false";
|
||||||
|
file.seekg(4, std::ios_base::cur);
|
||||||
|
}
|
||||||
|
else if (c == 't')
|
||||||
|
{
|
||||||
|
token.type = BOOL;
|
||||||
|
token.value = "true";
|
||||||
|
file.seekg(3, std::ios_base::cur);
|
||||||
|
}
|
||||||
|
else if (c == 'n')
|
||||||
|
{
|
||||||
|
token.type = NIL;
|
||||||
|
file.seekg(3, std::ios_base::cur);
|
||||||
|
}
|
||||||
|
else if (c == ',')
|
||||||
|
token.type = COMMA;
|
||||||
|
return (token);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,28 @@
|
|||||||
#include "webserv.hpp"
|
#include "webserv.hpp"
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
#include "TOMLNode.cpp"
|
||||||
|
#include "TOMLParser.cpp"
|
||||||
|
|
||||||
|
/* struct location */
|
||||||
|
/* { */
|
||||||
|
/* std::string location; */
|
||||||
|
/* std::string root; */
|
||||||
|
|
||||||
|
/* } */
|
||||||
|
|
||||||
|
/* class config */
|
||||||
|
/* { */
|
||||||
|
/* std::string name; */
|
||||||
|
/* } */
|
||||||
|
|
||||||
namespace config
|
namespace config
|
||||||
{
|
{
|
||||||
void parse(
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
6
src/config/parse_types.cpp
Normal file
6
src/config/parse_types.cpp
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#include "webserv.hpp"
|
||||||
|
#include "parse.cpp"
|
||||||
|
|
||||||
|
namespace config
|
||||||
|
{
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user