From f7e6b618115d69ba61fb781a9b7edc4a44ce487f Mon Sep 17 00:00:00 2001 From: Eric LAZO Date: Thu, 4 Aug 2022 19:33:38 +0200 Subject: [PATCH] LocationConfig sorter works as intended, tho should prolly check things thoroughly later --- srcs/config/LocationConfig.hpp | 70 ++++++++-------------------------- srcs/config/postProcessing.cpp | 32 +--------------- 2 files changed, 16 insertions(+), 86 deletions(-) diff --git a/srcs/config/LocationConfig.hpp b/srcs/config/LocationConfig.hpp index 07ce034..6d1478d 100644 --- a/srcs/config/LocationConfig.hpp +++ b/srcs/config/LocationConfig.hpp @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/23 16:08:00 by me #+# #+# */ -/* Updated: 2022/08/04 01:40:40 by me ### ########.fr */ +/* Updated: 2022/08/04 19:32:40 by erlazo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,11 +71,9 @@ public: // problem is the logic bool operator<(const LocationConfig& rhs) const { -// size_t len_lhs = 0; -// size_t len_rhs = 0; - int len_lhs = 0; - int len_rhs = 0; -// size_t tmp = 0; + int comp_lhs = 0; + int comp_rhs = 0; + size_t tmp = 0; // consider adding 1 to path that ends in a file not folder. @@ -84,72 +82,34 @@ public: // and /test/test1 is bigger than test // so we want to count / -// len_lhs = _count_chars(this->path); -// len_rhs = _count_chars(rhs.path); - const char *tmp1 = this->path.c_str(); - size_t i = 0; - - while (tmp1[i]) - { - if (tmp1[i] == '/') - ++len_lhs; - ++i; - } - - const char *tmp2 = rhs.path.c_str(); - i = 0; - - while (tmp2[i]) - { - if (tmp2[i] == '/') - ++len_rhs; - ++i; - } - -/* while ((tmp = this->path.find_first_of("/", tmp)) != std::string::npos) { - std::cout << "tmp_lhs: " << tmp << "\n"; ++tmp; - ++len_lhs; + ++comp_lhs; } + if (path[path.find_last_of("/") + 1] != '\0') + ++comp_lhs; tmp = 0; while ((tmp = rhs.path.find_first_of("/", tmp)) != std::string::npos) { - std::cout << "tmp_rhs: " << tmp << "\n"; ++tmp; - ++len_rhs; + ++comp_rhs; } -*/ - std::cout << "len_lhs: " << len_lhs << " len_rhs: " << len_rhs \ - << " bool res: " << (len_lhs > len_rhs) << "\n"; + if (rhs.path[rhs.path.find_last_of("/") + 1] != '\0') + ++comp_rhs; + + std::cout << "comp_lhs: " << comp_lhs << " comp_rhs: " << comp_rhs \ + << " bool res: " << (comp_lhs > comp_rhs) << "\n"; // i honestly can't tell you how or why but using > rather than < // fixed all my problems // the fact that the bool was always 0 before, and the correct order // i want... super weird... - return (len_lhs > len_rhs); // right comparison ? not <= ? + // return (comp_lhs > comp_rhs); // right comparison ? not <= ? + return (comp_lhs < comp_rhs); // right comparison ? not <= ? }; -/* - size_t _count_chars(std::string str) - { - const char *tmp = str.c_str(); - size_t i = 0; - size_t count = 0; - - while (i < str.lenght()) - { - if (tmp[i] == '/') - ++count; - ++i; - } - - return (count); - } -*/ - }; diff --git a/srcs/config/postProcessing.cpp b/srcs/config/postProcessing.cpp index 0b0cfcf..8fbeb98 100644 --- a/srcs/config/postProcessing.cpp +++ b/srcs/config/postProcessing.cpp @@ -139,8 +139,6 @@ void ConfigParser::_post_processing(std::vector *servers) ++it_l; } -// Then put locations in order... -// may change how the order is set later // ok we can sort in order and reverse... @@ -148,38 +146,10 @@ void ConfigParser::_post_processing(std::vector *servers) // std::sort(it->locations.begin(), it->locations.end(), compareLocationConfigs); std::sort(it->locations.begin(), it->locations.end()); -// for some reason no need to reverse... -// std::reverse(it->locations.begin(), it->locations.end()); + std::reverse(it->locations.begin(), it->locations.end()); -/* -// let's test sort - int myints[] = {32,71,12,45,26,80,53,33}; -// std::vector myvector (myints, myints+8); -// std::vector myvector(10, 5); - std::vector myvector; - - myvector.resize(6); - - myvector[0].i = 32; - myvector[1].i = 71; - myvector[2].i = 12; - myvector[3].i = 45; - myvector[4].i = 26; - myvector[5].i = 80; - - - std::sort(myvector.begin(), myvector.end()); - - std::cout << "myvector contains:"; -// for (std::vector::iterator it=myvector.begin(); it!=myvector.end(); ++it) - for (std::vector::iterator it=myvector.begin(); it!=myvector.end(); ++it) - std::cout << ' ' << *it; - std::cout << '\n'; -*/ - - ++it; }