mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-28 21:07:59 +03:00
feat: first working simple version
This commit is contained in:
2
config/simple.toml
Normal file
2
config/simple.toml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
name = "jopa"
|
||||||
|
port = 6969
|
||||||
@@ -8,6 +8,8 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -33,6 +35,12 @@ class toml_node
|
|||||||
} value;
|
} value;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
enum e_type get_type(void)
|
||||||
|
{
|
||||||
|
return (type);
|
||||||
|
}
|
||||||
|
|
||||||
TOMLMap *getMap(void)
|
TOMLMap *getMap(void)
|
||||||
{
|
{
|
||||||
return (value.map);
|
return (value.map);
|
||||||
@@ -68,6 +76,26 @@ class toml_node
|
|||||||
value.map = obj;
|
value.map = obj;
|
||||||
type = MAP;
|
type = MAP;
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return ( new std::string("Not implemented :)"));
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace config
|
|||||||
TOMLParser(const std::string filename) : tokenizer(filename) {}
|
TOMLParser(const std::string filename) : tokenizer(filename) {}
|
||||||
toml_node *parse(void);
|
toml_node *parse(void);
|
||||||
|
|
||||||
toml_node *parseObject(void)
|
toml_node *parseMap(void)
|
||||||
{
|
{
|
||||||
std::cout << "Parsing object" << std::endl;
|
std::cout << "Parsing object" << std::endl;
|
||||||
toml_node *node = new toml_node;
|
toml_node *node = new toml_node;
|
||||||
@@ -29,10 +29,20 @@ namespace config
|
|||||||
{
|
{
|
||||||
if (tokenizer.hasMoreTokens())
|
if (tokenizer.hasMoreTokens())
|
||||||
{
|
{
|
||||||
s_token nextToken = tokenizer.getToken();
|
s_token nextToken;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
nextToken = tokenizer.getToken();
|
||||||
|
}
|
||||||
|
catch (std::logic_error e)
|
||||||
|
{
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
std::string key = nextToken.value;
|
std::string key = nextToken.value;
|
||||||
std::cout << key << std::endl;
|
std::cout << key << std::endl;
|
||||||
tokenizer.getToken();
|
if (tokenizer.getToken().type != ASSIGN)
|
||||||
|
throw std::logic_error("EXPECTED assign!");
|
||||||
nextToken = tokenizer.getToken();
|
nextToken = tokenizer.getToken();
|
||||||
switch (nextToken.type)
|
switch (nextToken.type)
|
||||||
{
|
{
|
||||||
@@ -49,6 +59,7 @@ namespace config
|
|||||||
}
|
}
|
||||||
case NUMBER:
|
case NUMBER:
|
||||||
{
|
{
|
||||||
|
tokenizer.rollBackToken();
|
||||||
(*mapObject)[key] = parseNumber();
|
(*mapObject)[key] = parseNumber();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -59,16 +70,28 @@ namespace config
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
/* throw std::logic_error("jopa in parseObject"); */
|
/* throw std::logic_error("jopa in parseMap"); */
|
||||||
std::cerr << "Unknown token, marking as complete" << std::endl;
|
std::cerr << "Unknown token, marking as complete" << std::endl;
|
||||||
completed = true;
|
completed = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (tokenizer.hasMoreTokens())
|
||||||
|
nextToken = tokenizer.getToken();
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
if (nextToken.type != NEWLINE)
|
||||||
|
{
|
||||||
|
throw std::logic_error("EXPECTED newline");
|
||||||
|
}
|
||||||
|
/* if (tokenizer.getToken().type == NEWLINE) */
|
||||||
|
/* completed = true; */
|
||||||
|
/* else */
|
||||||
|
/* throw std::logic_error("EXPECTED newline"); */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw std::logic_error("parseObject: no more tokens");
|
throw std::logic_error("parseMap: no more tokens");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
node->setObject(mapObject);
|
node->setObject(mapObject);
|
||||||
@@ -194,6 +217,9 @@ namespace config
|
|||||||
std::string key;
|
std::string key;
|
||||||
key = "";
|
key = "";
|
||||||
|
|
||||||
|
root = parseMap();
|
||||||
|
return (root);
|
||||||
|
|
||||||
while (tokenizer.hasMoreTokens())
|
while (tokenizer.hasMoreTokens())
|
||||||
{
|
{
|
||||||
s_token token;
|
s_token token;
|
||||||
@@ -203,9 +229,16 @@ namespace config
|
|||||||
/* std::cout << token.to_string() << std::endl; */
|
/* std::cout << token.to_string() << std::endl; */
|
||||||
switch (token.type)
|
switch (token.type)
|
||||||
{
|
{
|
||||||
|
case KEY:
|
||||||
|
{
|
||||||
|
toml_node *parsedObject = parseString();
|
||||||
|
if (!root)
|
||||||
|
root = parsedObject;
|
||||||
|
}
|
||||||
|
|
||||||
case ARR_OPEN:
|
case ARR_OPEN:
|
||||||
{
|
{
|
||||||
toml_node *parsedObject = parseObject();
|
toml_node *parsedObject = parseMap();
|
||||||
/* parsedObject->printNode(0); */
|
/* parsedObject->printNode(0); */
|
||||||
if (!root)
|
if (!root)
|
||||||
root = parsedObject;
|
root = parsedObject;
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ namespace config
|
|||||||
{
|
{
|
||||||
enum e_token
|
enum e_token
|
||||||
{
|
{
|
||||||
|
KEY,
|
||||||
|
NEWLINE,
|
||||||
ASSIGN,
|
ASSIGN,
|
||||||
STRING,
|
STRING,
|
||||||
NUMBER,
|
NUMBER,
|
||||||
@@ -34,11 +36,20 @@ namespace config
|
|||||||
/* std::string to_string(void); */
|
/* std::string to_string(void); */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool istomlkey(char c)
|
||||||
|
{
|
||||||
|
if (isalnum(c) || c == '-' || c == '_')
|
||||||
|
return (true);
|
||||||
|
else
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
|
||||||
class Tokenizer
|
class Tokenizer
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::fstream file;
|
std::fstream file;
|
||||||
size_t prev_pos;
|
size_t prev_pos;
|
||||||
|
e_token last_token;
|
||||||
public:
|
public:
|
||||||
Tokenizer(std::string filename)
|
Tokenizer(std::string filename)
|
||||||
{
|
{
|
||||||
@@ -51,16 +62,24 @@ namespace config
|
|||||||
char getWithoutWhiteSpace();
|
char getWithoutWhiteSpace();
|
||||||
struct s_token getToken();
|
struct s_token getToken();
|
||||||
bool hasMoreTokens();
|
bool hasMoreTokens();
|
||||||
|
bool firstToken()
|
||||||
|
{
|
||||||
|
// doesn't account for indent!
|
||||||
|
if (file.tellg() == 0 || file.tellg() == 1 || (last_token == NEWLINE))
|
||||||
|
return (true);
|
||||||
|
else
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
void rollBackToken();
|
void rollBackToken();
|
||||||
};
|
};
|
||||||
|
|
||||||
char Tokenizer::getWithoutWhiteSpace(void)
|
char Tokenizer::getWithoutWhiteSpace(void)
|
||||||
{
|
{
|
||||||
char c = ' ';
|
char c = ' ';
|
||||||
while ((c == ' ' || c == '\n'))
|
while (c == ' ')
|
||||||
{
|
{
|
||||||
file.get(c);
|
file.get(c);
|
||||||
if ((c == ' ' || c == '\n') && !file.good())
|
if ((c == ' ') && !file.good())
|
||||||
{
|
{
|
||||||
throw std::logic_error("No more tokens!");
|
throw std::logic_error("No more tokens!");
|
||||||
}
|
}
|
||||||
@@ -82,6 +101,16 @@ namespace config
|
|||||||
file.seekg(prev_pos);
|
file.seekg(prev_pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* struct s_token Tokenizer::getKey(void) */
|
||||||
|
/* { */
|
||||||
|
/* char c; */
|
||||||
|
/* struct s_token token; */
|
||||||
|
/* if (file.eof()) */
|
||||||
|
/* { */
|
||||||
|
/* std::cout << "Tokens exhausted" << std::endl; */
|
||||||
|
/* } */
|
||||||
|
/* } */
|
||||||
|
|
||||||
struct s_token Tokenizer::getToken(void)
|
struct s_token Tokenizer::getToken(void)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
@@ -94,7 +123,16 @@ namespace config
|
|||||||
prev_pos = file.tellg();
|
prev_pos = file.tellg();
|
||||||
c = getWithoutWhiteSpace();
|
c = getWithoutWhiteSpace();
|
||||||
|
|
||||||
if (c == '"')
|
if (firstToken() && config::istomlkey(c))
|
||||||
|
{
|
||||||
|
token.type = KEY;
|
||||||
|
while (config::istomlkey(c))
|
||||||
|
{
|
||||||
|
token.value += c;
|
||||||
|
file.get(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == '"')
|
||||||
{
|
{
|
||||||
token.type = STRING;
|
token.type = STRING;
|
||||||
token.value = "";
|
token.value = "";
|
||||||
@@ -112,6 +150,8 @@ namespace config
|
|||||||
token.type = ARR_CLOSE;
|
token.type = ARR_CLOSE;
|
||||||
else if (c == '=')
|
else if (c == '=')
|
||||||
token.type = ASSIGN;
|
token.type = ASSIGN;
|
||||||
|
else if (c == '\n')
|
||||||
|
token.type = NEWLINE;
|
||||||
else if (c == '-' || isdigit(c))
|
else if (c == '-' || isdigit(c))
|
||||||
{
|
{
|
||||||
std::streampos prevCharPos;
|
std::streampos prevCharPos;
|
||||||
@@ -154,6 +194,7 @@ namespace config
|
|||||||
}
|
}
|
||||||
else if (c == ',')
|
else if (c == ',')
|
||||||
token.type = COMMA;
|
token.type = COMMA;
|
||||||
|
last_token = token.type;
|
||||||
return (token);
|
return (token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ namespace config
|
|||||||
void display(TOMLMap *config)
|
void display(TOMLMap *config)
|
||||||
{
|
{
|
||||||
std::cout << "printing config:" << std::endl;
|
std::cout << "printing config:" << std::endl;
|
||||||
std::cout << (*config)["name"] << std::endl;
|
std::cout << "name: " << *(*config)["name"]->toString() << std::endl;
|
||||||
std::cout << (*config)["port"] << std::endl;
|
std::cout << "port: " << *(*config)["port"]->toString() << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,10 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
Server server;
|
Server server;
|
||||||
|
|
||||||
server.readConfig();
|
/* server.readConfig(); */
|
||||||
server.setupConfig();
|
/* server.setupConfig(); */
|
||||||
server.start();
|
/* server.start(); */
|
||||||
|
parse();
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user