refactor(print): changing the return_value to ret

- The variable name was to long for the norm
This commit is contained in:
Raphael 2025-09-04 11:50:11 +02:00
parent a779c1040e
commit b0f5199eff
No known key found for this signature in database
2 changed files with 45 additions and 46 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/14 17:27:44 by rparodi #+# #+# */
/* Updated: 2025/09/01 17:58:00 by rparodi ### ########.fr */
/* Updated: 2025/09/04 11:46:29 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,33 +15,31 @@
#include <stdlib.h>
#include <unistd.h>
void _print_char(int fd, char c, int *ret_value);
void _print_nbr(int fd, int nb, int *ret_value);
void _print_char(int fd, char c, int *ret);
void _print_nbr(int fd, int nb, int *ret);
void _print_base(\
int fd, unsigned long long nbr, int *ret_value, char c);
void _print_unsigned(int fd, unsigned int nb, int *ret_value);
void _print_str(int fd, char *str, int *ret_value);
int fd, unsigned long long nbr, int *ret, char c);
void _print_unsigned(int fd, unsigned int nb, int *ret);
void _print_str(int fd, char *str, int *ret);
int _check_args(int fd, char c, va_list args, int *ret_value)
int _check_args(int fd, char c, va_list args, int *ret)
{
if (c == 'c')
_print_char(fd, (char)va_arg(args, int), ret_value);
_print_char(fd, (char)va_arg(args, int), ret);
else if (c == 's')
_print_str(fd, (char *)va_arg(args, char *), ret_value);
_print_str(fd, (char *)va_arg(args, char *), ret);
else if (c == 'i' || c == 'd')
_print_nbr(fd, (int)va_arg(args, int), ret_value);
_print_nbr(fd, (int)va_arg(args, int), ret);
else if (c == '%')
_print_char(fd, '%', ret_value);
_print_char(fd, '%', ret);
else if (c == 'u')
_print_unsigned(\
fd, (unsigned int)va_arg(args, unsigned int), ret_value);
_print_unsigned(fd, (unsigned int)va_arg(args, unsigned int), ret);
else if (c == 'x')
_print_base(fd, (unsigned int)va_arg(args, unsigned int), ret_value, c);
_print_base(fd, (unsigned int)va_arg(args, unsigned int), ret, c);
else if (c == 'X')
_print_base(fd, (unsigned int)va_arg(args, unsigned int), ret_value, c);
_print_base(fd, (unsigned int)va_arg(args, unsigned int), ret, c);
else if (c == 'p')
_print_base(fd, (unsigned long long)va_arg(args, unsigned long long), \
ret_value, c);
_print_base(fd, (unsigned int)va_arg(args, unsigned int), ret, c);
va_end(args);
return (1);
}
@ -58,9 +56,9 @@ int ft_dprintf(int fd, const char *s, ...)
size_t i;
va_list args;
char *str;
int ret_value;
int ret;
ret_value = 0;
ret = 0;
str = ft_strdup(s);
va_start(args, s);
i = 0;
@ -68,15 +66,15 @@ int ft_dprintf(int fd, const char *s, ...)
{
if (str[i] == '%')
{
_check_args(fd, str[i + 1], args, &ret_value);
_check_args(fd, str[i + 1], args, &ret);
i++;
}
else
_print_char(fd, str[i], &ret_value);
_print_char(fd, str[i], &ret);
i++;
}
free(str);
return (ret_value);
return (ret);
}
/**
@ -90,9 +88,9 @@ int ft_printf(const char *s, ...)
size_t i;
va_list args;
char *str;
int ret_value;
int ret;
ret_value = 0;
ret = 0;
str = ft_strdup(s);
va_start(args, s);
i = 0;
@ -100,13 +98,13 @@ int ft_printf(const char *s, ...)
{
if (str[i] == '%')
{
_check_args(1, str[i + 1], args, &ret_value);
_check_args(1, str[i + 1], args, &ret);
i++;
}
else
_print_char(1, str[i], &ret_value);
_print_char(1, str[i], &ret);
i++;
}
free(str);
return (ret_value);
return (ret);
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:13:14 by rparodi #+# #+# */
/* Updated: 2025/09/01 17:57:36 by rparodi ### ########.fr */
/* Updated: 2025/09/04 11:46:53 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,54 +14,55 @@
#include <limits.h>
#include <unistd.h>
void _print_char(int fd, char c, int *ret_value)
void _print_char(int fd, char c, int *ret)
{
write(fd, &c, 1);
(*ret_value)++;
(*ret)++;
}
void _print_unsigned(int fd, unsigned int nb, int *ret_value)
void _print_unsigned(int fd, unsigned int nb, int *ret)
{
if (nb >= 10)
{
_print_unsigned(fd, nb / 10, ret_value);
_print_unsigned(fd, nb / 10, ret);
nb = nb % 10;
}
if (nb < 10)
_print_char(fd, nb + 48, ret_value);
_print_char(fd, nb + 48, ret);
}
void _print_str(int fd, char *str, int *ret_value)
void _print_str(int fd, char *str, int *ret)
{
if (!str)
*ret_value += write(fd, "(null)", 6);
*ret += write(fd, "(null)", 6);
else
*ret_value += write(fd, str, ft_strlen(str));
*ret += write(fd, str, ft_strlen(str));
}
void _print_nbr(int fd, int nb, int *ret_value)
void _print_nbr(int fd, int nb, int *ret)
{
if (nb < 0)
{
if (nb == INT_MIN)
{
write(1, "-2147483648", 11);
*ret_value += 11;
*ret += 11;
return ;
}
nb = -nb;
_print_char(fd, '-', ret_value); }
_print_char(fd, '-', ret);
}
if (nb >= 10)
{
_print_nbr(fd, nb / 10, ret_value);
_print_nbr(fd, nb / 10, ret);
nb = nb % 10;
}
if (nb < 10)
_print_char(fd, nb + 48, ret_value);
_print_char(fd, nb + 48, ret);
}
void _print_base(\
int fd, unsigned long long nbr, int *ret_value, char c)
int fd, unsigned long long nbr, int *ret, char c)
{
char base[16];
@ -72,10 +73,10 @@ void _print_base(\
else if (c == 'p')
{
if (nbr != 0)
_print_str(fd, "0x", ret_value);
_print_str(fd, "0x", ret);
else if (nbr == 0)
{
_print_str(fd, "(nil)", ret_value);
_print_str(fd, "(nil)", ret);
return ;
}
else
@ -85,7 +86,7 @@ void _print_base(\
if (c != 'p')
{
if (nbr >= 16)
_print_base(fd, nbr / 16, ret_value, c);
_print_char(fd, base[nbr % 16], ret_value);
_print_base(fd, nbr / 16, ret, c);
_print_char(fd, base[nbr % 16], ret);
}
}