Files
42_INT_12_webserv/srcs/ConfigParserUtils.cpp

110 lines
2.6 KiB
C++

#include "Webserv.hpp"
std::string ConfigParser::_pre_set_val_check(const std::string key, \
const std::string value)
{
// check key for ;
// check values for ; at end and right number of words depending on key
// std::cout << "pre check\n";
if (key.find_first_of(";") != std::string::npos)
throw std::invalid_argument("bad config file arguments 2");
// there shouldn't be any tabs, right? not between values...
if (value.find_first_of("\t") != std::string::npos)
{
std::cout << value << "\n";
throw std::invalid_argument("bad config file arguments 3");
}
size_t i = value.find_first_of(";");
// so you can't have no ;
// you can't have just ;
// and you can't have a ; not at the end or several ;
// in theory value_find_last_of should find the only ;
if (i == std::string::npos || (value.find_last_not_of(" \n")) != i \
|| value.compare(";") == 0)
throw std::invalid_argument("bad config file arguments 4");
// we Trim value.
// is this valid?
// would it be better to shove the result directly in tmp_val?
// like call substr in split?
//value = value.substr(0, i - 1);
return (value.substr(0, i));
}
// assumes curr is on a space or \t or \n
// get first word? next word? word?
std::string ConfigParser::_get_first_word(size_t *curr)
{
size_t start;
// are these checks excessive?
if ((start = _content.find_first_not_of(" \t\n", *curr)) == std::string::npos)
throw std::invalid_argument("bad config file arguments");
if ((*curr = _content.find_first_of(" \t\n", start)) == std::string::npos)
throw std::invalid_argument("bad config file arguments");
std::string key = _content.substr(start, *curr - start);
return (key);
}
// also assumes curr is on a space \t or \n
std::string ConfigParser::_get_rest_of_line(size_t *curr)
{
size_t start;
if ((start = _content.find_first_not_of(" \t\n", *curr)) == std::string::npos)
throw std::invalid_argument("bad config file arguments");
// std::cout << "start + 4 = " << _content.substr(start, 4) << "\n";
// std::cout << "curr + 4 = " << _content.substr(*curr, 4) << "\n";
if ((*curr = _content.find_first_of("\n", start)) == std::string::npos)
throw std::invalid_argument("bad config file arguments");
std::string values = _content.substr(start, *curr - start);
// std::cout << "rest of Line values: " << values << "\n";
return (values);
}
MethodType ConfigParser::_str_to_method_type(std::string str)
{
if (str == "GET")
return GET;
else if (str == "POST")
return POST;
else if (str == "DELETE")
return DELETE;
return INVALID;
}
void ConfigParser::_print_content() const
{
std::cout << _content;
}
// I might need to make my own Exceptions to throw...