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.

96 lines
2.2 KiB

#include "webserv.hpp"
Tokenizer::Tokenizer(string fileName) {
2 years ago
file.open(fileName.c_str(), std::ios::in);
if (!file.good())
cout << "File open error"
2 years ago
<< "\n";
}
bool Tokenizer::hasMoreTokens() { return !file.eof(); }
char Tokenizer::getWithoutWhiteSpace() {
2 years ago
char c = ' ';
while ((c == ' ' || c == '\n') || c == '\t') {
file.get(c); // check
if ((c == ' ' || c == '\n') && !file.good()) {
2 years ago
// cout << file.eof() << " " << file.fail() << "\n";
2 years ago
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"
2 years ago
<< "\n";
2 years ago
// throw std::exception("Exhausted tokens");
}
prevPos = file.tellg();
c = getWithoutWhiteSpace();
2 years ago
Token token;
token.type = NULL_TYPE;
if (c == '"') {
token.type = STRING;
token.value = "";
file.get(c);
while (c != '"') {
token.value += c;
file.get(c);
}
} else if (c == '{') {
token.type = CURLY_OPEN;
} else if (c == '}') {
token.type = CURLY_CLOSE;
} else if (c == '-' || (c >= '0' && c <= '9')) {
// Check if string is numeric
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);
2 years ago
if (file.eof()) {
break;
} else {
if ((c == '-') || (c >= '0' && c <= '9') || (c == '.')) {
token.value += c;
} else {
file.seekg(prevCharPos);
}
}
}
} 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 = ARRAY_OPEN;
} else if (c == ']') {
token.type = ARRAY_CLOSE;
} else if (c == ':') {
token.type = COLON;
} else if (c == ',') {
token.type = COMMA;
}
// cout << token.type << " : " << token.value << "\n";
return token;
}