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