changed itoa to c++ style and put it in utils, and change utils dependencies to not depend on webserv

This commit is contained in:
hugogogo
2022-07-31 13:19:22 +02:00
parent af8dc072c1
commit 537572e2d0
7 changed files with 17 additions and 64 deletions

View File

@@ -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 \

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<std::string> tmp_val = split(value, ' ');
std::vector<std::string> 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<std::string> tmp2 = split(tmp_val[0], ':');
std::vector<std::string> 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");

View File

@@ -24,7 +24,6 @@
# include <string> // string
# include <cstdio> // perror
# include <cstdlib> // atoi (athough it's already cover by <string>)
char *ft_itoa(int n);
# include "Client.hpp"
# include "ServerConfig.hpp"

View File

@@ -1,50 +0,0 @@
#include <string.h>
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);
}

View File

@@ -1,8 +1,5 @@
#include "Webserv.hpp"
#include "utils.hpp"
std::vector<std::string> split(std::string input, char delimiter)
{
@@ -16,4 +13,11 @@ std::vector<std::string> 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<char*>( strs.str().c_str() ) );
}

View File

@@ -1,10 +1,12 @@
#ifndef UTILS_HPP
# define UTILS_HPP
# include <vector>
# include <string>
# include <sstream>
std::vector<std::string> split(std::string input, char delimiter);
std::vector<std::string> split(std::string input, char delimiter);
char* itoa(int n);
#endif

View File

@@ -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