still working on integrating the config file stuff

This commit is contained in:
Me
2022-07-25 21:00:04 +02:00
parent b9ccf09089
commit 35ac55b5e8
9 changed files with 142 additions and 36 deletions

View File

@@ -6,7 +6,7 @@
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 22:11:17 by me #+# #+# */
/* Updated: 2022/07/25 02:56:26 by me ### ########.fr */
/* Updated: 2022/07/25 20:56:51 by me ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,6 +20,13 @@ I don't love the use of EMPTY and FAILURE and those
I would rather throw exceptions everywhere...
where do i check if there is only a ";"
we could make a print _content just to make sure it works properly...
need to figure out why return std::vector<ServerConfig> * rather than just simple
not a pointer...
is there a good reason?
*/
@@ -51,11 +58,14 @@ ConfigParser::ConfigParser(const char* path)
{
while (!file.eof())
{
// if we remove the Emtpy lines as well it simplifies things later...
getline(file, buf);
// remove # comments here.
if ((comment = buf.find_first_of("#")) == std::string::npos)
{
_content.append(buf + '\n');
// remove empty lines, i think...
if ((buf.find_first_not_of(" \t")) != std::string::npos)
_content.append(buf + '\n');
}
// else if (comment > 0 && (buf.find_first_not_of(" \t")) != std::string::npos)
// i think this works cuz it will find # no matter what, so it will find
@@ -90,15 +100,17 @@ ConfigParser & ConfigParser::operator=(const ConfigParser& rhs)
*/
// ok what exactly is the benefit of returning a pointer here...
std::vector<Server> * ConfigParser::parse()
{
// is this the best way to do this? new?
std::vector<Server> *ret = new std::vector<Server>();
std::vector<ServerConfig> *ret = new std::vector<Server>();
// yea i could do
std::vector<ServerConfig> ret;
size_t prev = 0;
size_t curr = _content.find_first_not_of(" \t\n", prev);
if (curr == std::string::npos)
throw std::invalid_argument("empty config file");
while (cur != std::string::npos)
@@ -115,16 +127,18 @@ std::vector<Server> * ConfigParser::parse()
ret->push_back(parse_server(&curr);
}
return (ret);
// or
// return (&ret); // wait no, that doesn't work...
}
// could we return a ref instead?
// i think no
Server ConfigParser::parse_server(size_t *start)
ServerConfig ConfigParser::parse_server(size_t *start)
{
Server ret;
size_t key_start;
size_t value_end;
size_t prev = _content.find_first_not_of(" \t\n", *start);
ServerConfig ret;
size_t key_start;
size_t value_end;
size_t prev = _content.find_first_not_of(" \t\n", *start);
if (prev == std::string::npos || _content[prev] != '{')
throw std::invalid_argument("bad config file syntax");
@@ -159,12 +173,14 @@ Server ConfigParser::parse_server(size_t *start)
// why bother with the if.. why not throw exception in check_line...
if ((value_end = check_line_syntax(_content.substr(key_start, curr - key_start))) == FAILED)
throw std::invalid_argument("bad config file arguments");
if ((int)value_end == EMPTY)
continue ;
// if ((int)value_end == EMPTY)
// continue ;
// could i shove this in the _set_server_value() func call?
std::string value = _content.substr(prev, value_end - prev + key_start + 1)
// I might need to catch here, or i could do nothing and trow all the way to the top!
if (_set_server_value(&ret, key, value) == FAILED)
throw std::invalid_argument("bad config file arguments");
// since there's not return anymore
// if (_set_server_value(&ret, key, value) == FAILED)
// throw std::invalid_argument("bad config file arguments");
_set_server_value(&ret, key, value); // it handles the throws
}
}
return (ret);
@@ -172,12 +188,12 @@ Server ConfigParser::parse_server(size_t *start)
Location ConfigParser::parse_location(size_t *start)
LocationConfig ConfigParser::parse_location(size_t *start)
{
Location ret;
size_t key_start;
size_t value_end;
size_t prev = _content.find_first_not_of(" \t\n", *start);
LocationConfig ret;
size_t key_start;
size_t value_end;
size_t prev = _content.find_first_not_of(" \t\n", *start);
if (prev == std::string::npos || _content[prev] != '{')
throw std::invalid_argument("bad config file syntax");
@@ -196,14 +212,18 @@ Location ConfigParser::parse_location(size_t *start)
break ;
default:
_check_proper_line_end(&prev, &curr);
// why bother with the if.. why not throw exception in check_line...
if ((value_end = check_line_syntax(_content.substr(key_start, curr - key_start))) == FAILED)
// why bother with the if.. why not throw exception in check_line...
// ok no we need the if cuz have to set value_end
if ((value_end = _check_for_semicolon(_content.substr(key_start, curr - key_start))) == FAILED)
throw std::invalid_argument("bad config file arguments");
if ((int)value_end == EMPTY)
continue ;
// if we remove all empty lines at the start no need for this
// if ((int)value_end == EMPTY)
// continue ;
// shove this in _set_location_value() func call?
std::string value = _content.substr(prev, value_end - prev + key_start + 1)
if (_set_location_value(&ret, key, value) == FAILED)
throw std::invalid_argument("bad config file arguments");
// if (_set_location_value(&ret, key, value) == FAILED)
// throw std::invalid_argument("bad config file arguments");
_set_location_value(&ret, key, value);
}
}
return (ret);
@@ -212,8 +232,12 @@ Location ConfigParser::parse_location(size_t *start)
// not convinced i want to return an int...
// but could be helpful to get more feed back
// maybe use a throw... but catch in parse_server...
int ConfigParser::_set_server_value(Server *server, const std::string key, const std::string value)
// yea i don't see any reason to return an int
//int ConfigParser::_set_server_value(Server *server, const std::string key, const std::string value)
void ConfigParser::_set_server_value(Server *server, const std::string key, const std::string value)
{
// i could do some checks at the start for value being bad or empty? or is
// that unnecessary since in theory i've already checked that it's good...
switch (key)
{
case "server_name":
@@ -274,7 +298,7 @@ int ConfigParser::_set_server_value(Server *server, const std::string key, const
default :
throw std::invalid_argument("bad config file arguments");
}
return (1); // for now but prolly will change...
// return (1); // for now but prolly will change...
}
// again not sure i want an int ret
@@ -322,9 +346,14 @@ void ConfigParser::_check_proper_line_end(size_t *prev, size_t *curr)
// i'm not sure i like this...
// and it needs to be _check_...
// rework this whole thing...
int ConfigParser::check_line_syntax(std::string line)
{
// no longer returning an int
// fuck no we have to return an int...
// ok keep FAILED as being -1
// i mean we could send a pointer to value_end and then ret void, but why
// bother chaning it now...
int ConfigParser::_check_for_semicolon(std::string line)
{
// line must be end with semicolon
size_t semicol;
size_t find;