Add hexadecimal support w/ ft_itoa_base

This commit is contained in:
gbaconni
2022-04-15 00:15:26 +02:00
parent 55044bb017
commit cab84b5f28
6 changed files with 126 additions and 35 deletions

View File

@@ -6,7 +6,7 @@
# By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/01/18 15:11:16 by gbaconni #+# #+# #
# Updated: 2022/04/13 23:03:45 by gbaconni ### lausanne.ch #
# Updated: 2022/04/15 00:01:26 by gbaconni ### lausanne.ch #
# #
# **************************************************************************** #
#
@@ -44,6 +44,9 @@ RMFLAGS = -f
VALGRIND = valgrind
VALGRINDFLAGS = --quiet --leak-check=full --show-leak-kinds=all
ifeq ($(DEBUG), 1)
PREFIX += $(VALGRIND) $(VALGRINDFLAGS)
endif
NORMINETTE = norminette
NORMINETTEFLAGS = -o
@@ -76,28 +79,17 @@ fast:
@$(MAKE) re FAST=1
test: clean $(NAME)
@./$(NAME) "%c" C || true
@./$(NAME) "%s" "42 Lausanne" || true
@./$(NAME) "%d" 42 || true
@$(PREFIX) ./$(NAME) "%c" C || true
@$(PREFIX) ./$(NAME) "%s" "42 Lausanne" || true
@$(PREFIX) ./$(NAME) "%d" 42 || true
@$(PREFIX) ./$(NAME) "%x" 42 || true
@$(PREFIX) ./$(NAME) "%X" 42 || true
test2:
ifneq ($(DEBUG), 1)
test2: debug
@$(MAKE) test DEBUG=1
endif
test3:
ifneq ($(FAST), 1)
test3: fast
@$(MAKE) test FAST=1
endif
test4: clean $(NAME)
ifneq ($(DEBUG), 1)
@$(MAKE) test4 DEBUG=1
else
@$(VALGRIND) $(VALGRINDFLAGS) ./$(NAME) "%c" C || true
@$(VALGRIND) $(VALGRINDFLAGS) ./$(NAME) "%s" "42 Lausanne" || true
@$(VALGRIND) $(VALGRINDFLAGS) ./$(NAME) "%d" 42 || true
endif
update: config online pull

View File

@@ -6,7 +6,7 @@
# By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/01/18 15:11:16 by gbaconni #+# #+# #
# Updated: 2022/04/13 01:20:20 by gbaconni ### lausanne.ch #
# Updated: 2022/04/14 23:54:03 by gbaconni ### lausanne.ch #
# #
# **************************************************************************** #
#
@@ -30,6 +30,7 @@ SRC = \
$(LIBFT)/ft_calloc.c \
$(LIBFT)/ft_strlen.c \
$(LIBFT)/ft_itoa.c \
ft_itoa_base.c \
ft_vprintf_char.c \
ft_vprintf_string.c \
ft_vprintf_pointer.c \

View File

@@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbaconni <gbaconni@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/16 13:51:16 by gbaconni #+# #+# */
/* Updated: 2022/04/15 00:08:27 by gbaconni ### lausanne.ch */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static int ft_intlen_base(int n, int nbase);
static char *ft_strrev(char *s);
char *ft_itoa_base(int n, char *base)
{
char *s;
int i;
long nbr;
long nbase;
nbase = ft_strlen(base);
s = (char *) ft_calloc((ft_intlen_base(n, nbase) + 1), sizeof(char));
if (s == NULL)
return (NULL);
i = 0;
nbr = n;
if (nbr == 0)
s[i++] = '0';
if (nbr < 0)
nbr = -nbr;
while (nbr > 0)
{
s[i++] = base[nbr % nbase];
nbr /= nbase;
}
if (n < 0)
s[i++] = '-';
s[i] = '\0';
return (ft_strrev(s));
}
static int ft_intlen_base(int n, int nbase)
{
size_t len;
len = 0;
if (n < 0 || n == 0)
len++;
while (!(n < 1 && n > -1))
{
n /= nbase;
len++;
}
return (len);
}
static char *ft_strrev(char *s)
{
int len;
int i;
int j;
char c;
len = ft_strlen(s);
i = 0;
while (i < (len / 2))
{
j = len - 1 - i;
c = s[i];
s[i] = s[j];
s[j] = c;
i++;
}
return (s);
}

View File

@@ -6,7 +6,7 @@
/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/20 11:55:02 by gbaconni #+# #+# */
/* Updated: 2022/04/12 23:51:56 by gbaconni ### lausanne.ch */
/* Updated: 2022/04/14 21:49:13 by gbaconni ### lausanne.ch */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/20 11:55:33 by gbaconni #+# #+# */
/* Updated: 2022/04/13 08:00:40 by gbaconni ### lausanne.ch */
/* Updated: 2022/04/15 00:13:02 by gbaconni ### lausanne.ch */
/* */
/* ************************************************************************** */
@@ -14,7 +14,22 @@
int ft_vprintf_hexadecimal(const char *format, va_list ap)
{
(void) ap;
(void) format;
return (0);
int ret;
int len;
int d;
char *s;
(void) format;
ret = 0;
len = 0;
d = va_arg(ap, int);
if (format[0] == 'x')
s = ft_itoa_base(d, "0123456789abcdef");
else
s = ft_itoa_base(d, "0123456789ABCDEF");
len = ft_strlen(s);
write(1, s, len);
ret += len;
free(s);
return (ret);
}

View File

@@ -6,7 +6,7 @@
/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/18 15:26:06 by gbaconni #+# #+# */
/* Updated: 2022/04/12 07:10:26 by gbaconni ### lausanne.ch */
/* Updated: 2022/04/14 23:56:24 by gbaconni ### lausanne.ch */
/* */
/* ************************************************************************** */
@@ -17,17 +17,21 @@
# include <stdarg.h>
# include <unistd.h>
/* Helper */
char *ft_itoa_base(int n, char *base);
/* Mandatory */
int ft_printf(const char *format, ...);
int ft_vprintf(char const *format, va_list ap);
int ft_vprintf_char(const char *format, va_list ap);
int ft_vprintf_string(const char *format, va_list ap);
int ft_vprintf_pointer(const char *format, va_list ap);
int ft_vprintf_decimal(const char *format, va_list ap);
int ft_vprintf_hexadecimal(const char *format, va_list ap);
int ft_vprintf_percent(const char *format, va_list ap);
int ft_vprintf_other(const char *format, va_list ap);
int ft_printf(const char *format, ...);
int ft_vprintf(char const *format, va_list ap);
int ft_vprintf_char(const char *format, va_list ap);
int ft_vprintf_string(const char *format, va_list ap);
int ft_vprintf_pointer(const char *format, va_list ap);
int ft_vprintf_decimal(const char *format, va_list ap);
int ft_vprintf_hexadecimal(const char *format, va_list ap);
int ft_vprintf_percent(const char *format, va_list ap);
int ft_vprintf_other(const char *format, va_list ap);
/* Bonus */
#endif