You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.3 KiB

/**
* @file Token.cpp
* @brief The tokenizer read json file and when asked for give us the next token encounter and his type.
* @author Narnaud
* @version 0.1
* @date 2023-01-17
*/
#include "webserv.hpp"
/**
* @brief Constructor
* Open conf file if he is readeable.
*
* @param fileName conf file path.
*/
Tokenizer::Tokenizer(string fileName) {
2 years ago
file.open(fileName.c_str(), std::ios::in);
if (!file.good()) std::cerr << "File open error.\n";
}
bool Tokenizer::hasMoreTokens() { return !file.eof(); }
char Tokenizer::getWithoutWhiteSpace() {
2 years ago
char c = ' ';
while ((c == ' ' || c == '\n') || c == '\t') {
file.get(c);
2 years ago
if ((c == ' ' || c == '\n') && !file.good()) {
throw std::logic_error("Ran out of tokens");
} else if (!file.good()) {
return c;
}
}
2 years ago
return c;
}
void Tokenizer::rollBackToken() {
2 years ago
if (file.eof()) file.clear();
2 years ago
file.seekg(prevPos);
}
Token Tokenizer::getToken() {
2 years ago
char c;
if (file.eof()) { cout << "Exhaused tokens\n"; }
2 years ago
prevPos = file.tellg();
c = getWithoutWhiteSpace();
Token token;
token.type = NULL_TYPE;
if (c == '"') {
token.type = STRING;
token.value = "";
file.get(c);
while (c != '"') {
if (c == '}' || c == ']' || c == ',') throw std::logic_error("Invalid json syntax: a string is not close");
2 years ago
token.value += c;
file.get(c);
}
} else if (c == '-' || (c >= '0' && c <= '9')) {
token.type = NUMBER;
token.value = "";
token.value += c;
std::streampos prevCharPos = file.tellg();
while ((c == '-') || (c >= '0' && c <= '9') || c == '.') {
prevCharPos = file.tellg();
file.get(c);
if (file.eof()) {
break;
} else {
if ((c == '-') || (c >= '0' && c <= '9') || (c == '.')) token.value += c;
else file.seekg(prevCharPos);
2 years ago
}
}
} else if (c == 'f') {
token.type = BOOLEAN;
token.value = "False";
file.seekg(4, std::ios_base::cur);
} else if (c == 't') {
token.type = BOOLEAN;
token.value = "True";
file.seekg(3, std::ios_base::cur);
} else if (c == 'n') {
token.type = NULL_TYPE;
file.seekg(3, std::ios_base::cur);
} else if (c == '{') token.type = CURLY_OPEN;
else if (c == '}') token.type = CURLY_CLOSE;
else if (c == '[') token.type = ARRAY_OPEN;
else if (c == ']') token.type = ARRAY_CLOSE;
else if (c == ':') token.type = COLON;
else if (c == ',') token.type = COMMA;
2 years ago
return token;
}