mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
Sort
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
#define TOKENIZER_HPP
|
||||
|
||||
#include "webserv.hpp"
|
||||
#include "tomlstuff.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
@@ -9,6 +11,7 @@
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
|
||||
|
||||
namespace config
|
||||
{
|
||||
enum e_token
|
||||
@@ -21,10 +24,9 @@ namespace config
|
||||
COMMA,
|
||||
BOOL,
|
||||
NIL,
|
||||
ARR_OPEN,
|
||||
ARR_CLOSE,
|
||||
MAP_OPEN,
|
||||
MAP_CLOSE,
|
||||
OPEN_BRACKET,
|
||||
CLOSE_BRACKET,
|
||||
MAP_DECL,
|
||||
MAPARRAY_DECL
|
||||
};
|
||||
|
||||
@@ -35,21 +37,9 @@ namespace config
|
||||
/* std::string to_string(void); */
|
||||
};
|
||||
|
||||
bool isspace(char c)
|
||||
{
|
||||
if (c == ' ' || c == '\t')
|
||||
return (true);
|
||||
else
|
||||
return (false);
|
||||
}
|
||||
bool isspace(char c);
|
||||
|
||||
bool istomlkey(char c)
|
||||
{
|
||||
if (isalnum(c) || c == '-' || c == '_')
|
||||
return (true);
|
||||
else
|
||||
return (false);
|
||||
}
|
||||
bool istomlkey(char c);
|
||||
|
||||
class Tokenizer
|
||||
{
|
||||
@@ -57,57 +47,18 @@ namespace config
|
||||
std::fstream file;
|
||||
size_t prev_pos;
|
||||
e_token last_token;
|
||||
|
||||
public:
|
||||
Tokenizer(std::string filename)
|
||||
{
|
||||
file.open(filename.c_str(), std::ios::in);
|
||||
if (!file.good())
|
||||
{
|
||||
std::cerr << "file didn't open" << std::endl;
|
||||
}
|
||||
}
|
||||
Tokenizer(std::string filename);
|
||||
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);
|
||||
}
|
||||
bool firstToken();
|
||||
void rollBackToken();
|
||||
void set_last(e_token type);
|
||||
|
||||
};
|
||||
|
||||
char Tokenizer::getWithoutWhiteSpace(void)
|
||||
{
|
||||
char c = ' ';
|
||||
while (config::isspace(c))
|
||||
{
|
||||
file.get(c);
|
||||
if ((c == ' ') && !file.good())
|
||||
{
|
||||
throw std::logic_error("No more tokens!");
|
||||
}
|
||||
else if (!file.good())
|
||||
return (c);
|
||||
}
|
||||
return (c);
|
||||
}
|
||||
|
||||
bool Tokenizer::hasMoreTokens(void)
|
||||
{
|
||||
return (!file.eof());
|
||||
}
|
||||
|
||||
void Tokenizer::rollBackToken(void)
|
||||
{
|
||||
if (file.eof())
|
||||
file.clear();
|
||||
file.seekg(prev_pos);
|
||||
}
|
||||
|
||||
/* struct s_token Tokenizer::getKey(void) */
|
||||
/* { */
|
||||
/* char c; */
|
||||
@@ -118,128 +69,6 @@ namespace config
|
||||
/* } */
|
||||
/* } */
|
||||
|
||||
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 (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 = "";
|
||||
/* TODO: maybe do-while? */
|
||||
file.get(c);
|
||||
while (c != '"')
|
||||
{
|
||||
token.value += c;
|
||||
file.get(c);
|
||||
}
|
||||
}
|
||||
else if (c == '[')
|
||||
{
|
||||
std::streampos prev_pos = file.tellg();
|
||||
file.get(c);
|
||||
if (c == '[')
|
||||
{
|
||||
token.type = MAPARRAY_DECL;
|
||||
file.get(c);
|
||||
while (c != ']')
|
||||
{
|
||||
token.value += c;
|
||||
file.get(c);
|
||||
}
|
||||
if (c == ']')
|
||||
file.get(c);
|
||||
if (c != ']')
|
||||
throw std::logic_error("error in MAPARRAY_DECL");
|
||||
}
|
||||
else
|
||||
{
|
||||
token.type = ARR_OPEN;
|
||||
file.seekg(prev_pos);
|
||||
}
|
||||
|
||||
}
|
||||
else if (c == ']')
|
||||
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;
|
||||
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);
|
||||
|
||||
/* token.value = ""; */
|
||||
/* while (std::isalpha(c)) */
|
||||
/* { */
|
||||
/* token.value += c; */
|
||||
/* file.get(c); */
|
||||
/* } */
|
||||
std::cerr << "value is: " << token.value << std::endl;
|
||||
std::cerr << "c is: " << c << std::endl;
|
||||
}
|
||||
else if (c == 't')
|
||||
{
|
||||
token.type = BOOL;
|
||||
while (std::isalpha(c))
|
||||
{
|
||||
token.value += c;
|
||||
file.get(c);
|
||||
}
|
||||
/* 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;
|
||||
last_token = token.type;
|
||||
return (token);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user