From 537572e2d0226170b70e5dffdae205891d479aac Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 31 Jul 2022 13:19:22 +0200 Subject: [PATCH] changed itoa to c++ style and put it in utils, and change utils dependencies to not depend on webserv --- Makefile | 1 - srcs/ConfigParser.cpp | 6 ++--- srcs/Webserv.hpp | 1 - srcs/ft_itoa.cpp | 50 --------------------------------------- srcs/utils.cpp | 12 ++++++---- srcs/utils.hpp | 8 ++++--- srcs/webserv/response.cpp | 3 +-- 7 files changed, 17 insertions(+), 64 deletions(-) delete mode 100644 srcs/ft_itoa.cpp diff --git a/Makefile b/Makefile index 6ea9ce1..ef28cb5 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,6 @@ HEADERS = Webserv.hpp \ SRCS_D = srcs \ srcs/webserv SRCS = main.cpp \ - ft_itoa.cpp \ base.cpp init.cpp close.cpp epoll_update.cpp signal.cpp \ accept.cpp request.cpp response.cpp \ run_loop.cpp \ diff --git a/srcs/ConfigParser.cpp b/srcs/ConfigParser.cpp index d7a24a3..d5c2326 100644 --- a/srcs/ConfigParser.cpp +++ b/srcs/ConfigParser.cpp @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/13 22:11:17 by me #+# #+# */ -/* Updated: 2022/07/30 23:07:42 by lperrey ### ########.fr */ +/* Updated: 2022/07/31 13:18:14 by simplonco ### ########.fr */ /* */ /* ************************************************************************** */ @@ -239,7 +239,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \ //value = value.substr(0, i - 1); value = value.substr(0, i); - std::vector tmp_val = split(value, ' '); + std::vector tmp_val = ::split(value, ' '); size_t size = tmp_val.size(); // would if if be more optimized? @@ -260,7 +260,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \ else { // maybe do this differently? - std::vector tmp2 = split(tmp_val[0], ':'); + std::vector tmp2 = ::split(tmp_val[0], ':'); // i might take issue with this, will see if (server->host != "" && server->host != tmp2[0]) throw std::invalid_argument("bad listen"); diff --git a/srcs/Webserv.hpp b/srcs/Webserv.hpp index 0b88cbc..b5a7b8b 100644 --- a/srcs/Webserv.hpp +++ b/srcs/Webserv.hpp @@ -24,7 +24,6 @@ # include // string # include // perror # include // atoi (athough it's already cover by ) -char *ft_itoa(int n); # include "Client.hpp" # include "ServerConfig.hpp" diff --git a/srcs/ft_itoa.cpp b/srcs/ft_itoa.cpp deleted file mode 100644 index 1e996a9..0000000 --- a/srcs/ft_itoa.cpp +++ /dev/null @@ -1,50 +0,0 @@ - -#include - -static int eval_is_negative(int *n) -{ - if (*n < 0) - { - *n = *n * -1; - return (1); - } - return (0); -} - -static int eval_digit_nbr(int n) -{ - int digit_nbr; - - if (n == 0) - return (1); - digit_nbr = 0; - while (n != 0) - { - digit_nbr++; - n = n / 10; - } - return (digit_nbr); -} - -char *ft_itoa(int n) -{ - int i; - char *str; - int is_negative; - - if (n == -2147483648) - return (strdup("-2147483648")); - is_negative = eval_is_negative(&n); - i = eval_digit_nbr(n) + is_negative; - str = new char[i+1]; - if (is_negative) - str[0] = '-'; - str[i] = '\0'; - while (i > 0 + is_negative) - { - i--; - str[i] = (n % 10) + '0'; - n = n / 10; - } - return (str); -} diff --git a/srcs/utils.cpp b/srcs/utils.cpp index b66cd62..004a461 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -1,8 +1,5 @@ - -#include "Webserv.hpp" - - +#include "utils.hpp" std::vector split(std::string input, char delimiter) { @@ -16,4 +13,11 @@ std::vector split(std::string input, char delimiter) return answer; } +char* itoa(int n) +{ + std::stringstream strs; + strs << n; + // casts : https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used + return ( const_cast( strs.str().c_str() ) ); +} diff --git a/srcs/utils.hpp b/srcs/utils.hpp index 3f3b48c..9d47a02 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -1,10 +1,12 @@ - - #ifndef UTILS_HPP # define UTILS_HPP +# include +# include +# include -std::vector split(std::string input, char delimiter); +std::vector split(std::string input, char delimiter); +char* itoa(int n); #endif diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index ce785ed..2c74f7b 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -128,9 +128,8 @@ void Webserv::_get_ressource(Client *client) client->response.append("Content-Type: text/html; charset=UTF-8\r\n"); client->response.append("Content-Length: "); - tmp = ::ft_itoa(ifd.gcount()); + tmp = ::itoa(ifd.gcount()); client->response.append(tmp); - delete tmp; client->response.append("\r\n"); // Body