From cbb1d12d54c6c4f83d9674c611a74dc135b33d99 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 31 Jul 2022 12:59:27 +0200 Subject: [PATCH] modified itoa and its headers --- Makefile | 1 - headers/Webserv.hpp | 1 - headers/utils.hpp | 9 ++++-- srcs/ft_itoa.cpp | 58 ++++----------------------------------- srcs/utils.cpp | 12 +++++++- srcs/webserv/response.cpp | 5 ++-- 6 files changed, 26 insertions(+), 60 deletions(-) diff --git a/Makefile b/Makefile index 52f06ca..871c8e9 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,6 @@ HEADERS_F = 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/headers/Webserv.hpp b/headers/Webserv.hpp index 4e9c307..bc18b87 100644 --- a/headers/Webserv.hpp +++ b/headers/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/headers/utils.hpp b/headers/utils.hpp index 3f3b48c..9aa6162 100644 --- a/headers/utils.hpp +++ b/headers/utils.hpp @@ -1,10 +1,13 @@ - - #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/ft_itoa.cpp b/srcs/ft_itoa.cpp index 364ed65..90f6ff7 100644 --- a/srcs/ft_itoa.cpp +++ b/srcs/ft_itoa.cpp @@ -1,59 +1,13 @@ # include -#include +# include -static int eval_is_negative(int *n) +char* itoa(int n) { - if (*n < 0) - { - *n = *n * -1; - return (1); - } - return (0); -} + std::stringstream strs; + char * str; -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) -{ - -// std::stringstream strs; -// std::string s; -// -// strs << n; -// s = strs.str(); -// return ((char*)(s.c_str())); - - 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; - } + strs << n; + str = (char*)(strs.str().c_str()); return (str); } diff --git a/srcs/utils.cpp b/srcs/utils.cpp index 4061ce3..6423231 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -1,5 +1,5 @@ -#include "Webserv.hpp" +#include "utils.hpp" std::vector split(std::string input, char delimiter) { @@ -13,3 +13,13 @@ std::vector split(std::string input, char delimiter) return answer; } +char* itoa(int n) +{ + std::stringstream strs; + char * str; + + strs << n; + str = (char*)(strs.str().c_str()); + return (str); +} + diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index ce785ed..26a23bd 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -128,9 +128,10 @@ 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 = ::ft_itoa(ifd.gcount()); + tmp = ::itoa(ifd.gcount()); client->response.append(tmp); - delete tmp; + // delete tmp; client->response.append("\r\n"); // Body