diff --git a/Makefile b/Makefile index 977714a..6b9a490 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/17 12:35:27 by adjoly #+# #+# # -# Updated: 2023/11/17 19:59:14 by adjoly ### ########.fr # +# Updated: 2023/11/20 10:07:45 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -14,15 +14,17 @@ NAME = libftprintf.a CC = cc -SRCS = ft_printf.c\ - +SRCS = ft_printf.c \ + ft_putchar.c \ + ft_putnbr.c \ + ft_putnbrbase.c \ + ft_putstr.c \ OBJS = $(SRCS:.c=.o) FLAGS = -Werror -Wall -Wextra HEADER = libftprintf.h \ - $(NAME): $(OBJS) ar -rcs $(NAME) $(OBJS) diff --git a/ft_printf.c b/ft_printf.c index 411c456..3348e71 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -6,32 +6,33 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/17 16:48:37 by adjoly #+# #+# */ -/* Updated: 2023/11/18 20:56:29 by adjoly ### ########.fr */ +/* Updated: 2023/11/20 11:53:52 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft/libft.h" #include "libftprintf.h" -#include void ft_putaddr(void *ptr) { - + ft_putstr((char *)ptr); } -void ft_putnbrulong(int n) +void ft_putnbrulong(unsigned long n) { if (n < 10) write(1, &(char){n + '0'}, 1); else { - ft_putnbr_fd(n / 10, 1); + ft_putnbrulong(n / 10); write(1, &(char){n % 10 + '0'}, 1); } } int ft_printconversion(char conversion, va_list args) { + int count; + + count = 0; if (conversion == '%') ft_putchar('%'); else if (conversion == 's') @@ -48,26 +49,29 @@ int ft_printconversion(char conversion, va_list args) ft_putnbrbase(va_arg(args, int), "0123456789abcdef"); else if (conversion == 'X') ft_putnbrbase(va_arg(args, int), "0123456789ABCDEF"); + return (count); } int ft_printf(const char *format, ...) { - int i; - int j; - va_list args; + int i; + int j; + va_list args; + int count; va_start(args, format); + count = 0; while (format[i]) { if (format[i] == '%') { i++; - ft_printconversion(format[i], args); + count += ft_printconversion(format[i], args); } else - ft_putchar_fd(format[i], i); + ft_putchar(format[i]); i++; } va_end(args); - return (ft_strlen(result)); + return (count); } diff --git a/ft_putchar.c b/ft_putchar.c new file mode 100644 index 0000000..426b046 --- /dev/null +++ b/ft_putchar.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 10:49:00 by adjoly #+# #+# */ +/* Updated: 2023/11/20 10:04:41 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_putchar(char c) +{ + write(1, &c, 1); + return (1); +} diff --git a/ft_putnbr.c b/ft_putnbr.c new file mode 100644 index 0000000..01e92b3 --- /dev/null +++ b/ft_putnbr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/31 11:52:46 by adjoly #+# #+# */ +/* Updated: 2023/11/20 10:40:57 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +void ft_putnbr_fd(int n, int fd) +{ + unsigned int nbr; + int i; + + if (n < 0) + { + write(fd, "-", 1); + nbr = -n; + } + else + nbr = n; + if (nbr < 10) + { + write(fd, &(char){nbr + '0'}, 1); + i++; + } + else + { + ft_putnbr_fd(nbr / 10, fd); + write(fd, &(char){nbr % 10 + '0'}, 1); + } +} diff --git a/ft_putnbrbase_fd.c b/ft_putnbrbase_fd.c new file mode 100644 index 0000000..bd9ccb2 --- /dev/null +++ b/ft_putnbrbase_fd.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbrbase_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 10:57:44 by adjoly #+# #+# */ +/* Updated: 2023/11/18 11:10:32 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbrbase_fd(int n, char *base, int fd) +{ + unsigned int nbr; + size_t base_len; + + base_len = ft_strlen(base); + if (n < 0) + { + write(fd, "-", 1); + nbr = -n; + } + else + nbr = n; + if (nbr < base_len) + write(fd, &base[nbr % base_len], 1); + else + { + ft_putnbrbase_fd(nbr / base_len, base, fd); + write(fd, &base[nbr % base_len], 1); + } +} diff --git a/ft_putstr.c b/ft_putstr.c new file mode 100644 index 0000000..123a0e7 --- /dev/null +++ b/ft_putstr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/31 11:45:55 by adjoly #+# #+# */ +/* Updated: 2023/11/20 10:07:08 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_putstr(char *s) +{ + int i; + + i = 0; + if (s == NULL) + { + write(1, "(null)", 6); + return (6); + } + while (s[i]) + { + write(1, &s[i], 1); + i++; + } + return (i); +} diff --git a/libft b/libft deleted file mode 160000 index 6486eb4..0000000 --- a/libft +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6486eb4b3f2f40db0eb7ebdf1178d468a98ff782 diff --git a/libftprintf.h b/libftprintf.h index 6b7c519..d9ad512 100644 --- a/libftprintf.h +++ b/libftprintf.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/17 16:50:36 by adjoly #+# #+# */ -/* Updated: 2023/11/18 18:51:53 by adjoly ### ########.fr */ +/* Updated: 2023/11/20 10:04:12 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,16 @@ # include # include -# include "libft/libft.h" +# include int ft_printf(const char *format, ...); int ft_printconversion(char conversion, va_list args); -void ft_putnbrulong(int n); +void ft_putnbrulong(unsigned long n); void ft_putaddr(void *ptr); +int ft_putstr(char *s); +void ft_putnbrbase(int n, char *base); +int ft_putchar(char c); +void ft_putnbr(int n); + #endif \ No newline at end of file