a few good ideas as to how to better handle the config parsing

This commit is contained in:
Me
2022-07-28 18:07:16 +02:00
parent dfb8be3017
commit 6265019d3e
2 changed files with 59 additions and 12 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 20:56:51 by me ### ########.fr */
/* Updated: 2022/07/27 19:27:55 by me ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,18 +16,20 @@
/***** Stuf to rework
I don't love the use of EMPTY and FAILURE and those
slowly transitioning away...
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?
I need to better understand what check_proper_line does
*/
@@ -133,6 +135,7 @@ std::vector<Server> * ConfigParser::parse()
// could we return a ref instead?
// i think no
// might need new names for Prev and Curr, not super descriptive...
ServerConfig ConfigParser::parse_server(size_t *start)
{
ServerConfig ret;
@@ -144,8 +147,11 @@ ServerConfig ConfigParser::parse_server(size_t *start)
throw std::invalid_argument("bad config file syntax");
size_t curr = _content.find_first_of(" \t\n", ++prev);
if (curr == std::string::npos) // are there other things to check for?
throw std::invalid_argument("bad config file syntax");
while (curr != std::string::npos)
{
_get_key(&word_start, &blank_start);
_check_proper_line_end(&prev, &curr); // nope, not an option i think...
/* if ((prev = _content.find_first_not_of(" \t\n", curr)) == std::string::npos)
throw std::invalid_argument("bad config file arguments");
@@ -154,6 +160,7 @@ ServerConfig ConfigParser::parse_server(size_t *start)
*/ key_start = prev;
std::string key = _content.substr(prev, curr - prev);
std::string key = _get_key(&word_start, &blank_start);
switch (key)
{
case "}":
@@ -164,6 +171,15 @@ ServerConfig ConfigParser::parse_server(size_t *start)
// the name but it's so clear...
ret.location.push_back(parse_location(&curr));
default:
// ok i figured it out, there's a slight difference!
// curr below is this actually, she does go all the way to the end of the line...
// there has to be a better way of doing this, just like
// get me the first word, get me all the rest... but like in sub functions...
if ((curr = _content.find_first_of("\n", prev)) == std::string::npos)
throw std::invalid_argument("bad config file arguments");
_check_proper_line_end(&prev, &curr);
/* if ((prev = _content.find_first_not_of(" \t\n", curr)) == std::string::npos)
throw std::invalid_argument("bad config file arguments");
@@ -171,7 +187,7 @@ ServerConfig ConfigParser::parse_server(size_t *start)
throw std::invalid_argument("bad config file arguments");
*/
// 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)
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 ;
@@ -229,13 +245,16 @@ LocationConfig ConfigParser::parse_location(size_t *start)
return (ret);
}
// 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...
// 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)
// yea ok new plan, this is where a lot of the checks will happen...
void ConfigParser::_set_server_value(Server *server, const std::string key, const std::string value)
{
// so turns out here is where i should be checking for the semicolon i think
// and also making sure there are the right number of values depending on which key it is...
// like for error page there can be a bunch but you can't have say 1 error code and several error files
// it's 1 error file per line and
// 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)
@@ -335,6 +354,9 @@ int ConfigParser::_set_location_value(Location *location, const std::string key,
// shit, this doesn't work...
// wait it might...
// definitely need a better name...
// _check_proper_word_break ?
// get next word ?
void ConfigParser::_check_proper_line_end(size_t *prev, size_t *curr)
{
if ((*prev = _content.find_first_not_of(" \t\n", *curr)) == std::string::npos)
@@ -343,9 +365,6 @@ void ConfigParser::_check_proper_line_end(size_t *prev, size_t *curr)
throw std::invalid_argument("bad config file arguments");
}
// i'm not sure i like this...
// and it needs to be _check_...
// rework this whole thing...
// no longer returning an int
// fuck no we have to return an int...
@@ -354,6 +373,8 @@ void ConfigParser::_check_proper_line_end(size_t *prev, size_t *curr)
// bother chaning it now...
int ConfigParser::_check_for_semicolon(std::string line)
{
// yea ok i just don't like this...
// line must be end with semicolon
size_t semicol;
size_t find;
@@ -369,6 +390,13 @@ int ConfigParser::_check_for_semicolon(std::string line)
}
void ConfigParser::_print_content() const
{
std::cout << _content;
}
// I might need to make my own Exceptions to throw...