From 55944fc02b4309928c94b6be904beebf315b7985 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Sat, 8 Apr 2023 23:14:49 +0200 Subject: [PATCH] added sprintf and fprintf --- .gitignore | 3 +- Makefile | 32 ++++-- compile_commands.json | 130 ++++++++++++++++++++++ ft_printf.h => includes/ft_printf.h | 34 ++++-- includes/libft.h | 110 ++++++++++++++++++ libft | 2 +- ft_print_chars.c => srcs/ft_print_chars.c | 55 ++++----- ft_print_hexs.c => srcs/ft_print_hexs.c | 20 ++-- ft_print_nbrs.c => srcs/ft_print_nbrs.c | 21 ++-- ft_print_opts.c => srcs/ft_print_opts.c | 0 ft_print_ptrs.c => srcs/ft_print_ptrs.c | 10 +- srcs/ft_print_strings.c | 45 ++++++++ ft_printf.c => srcs/ft_printf.c | 94 +++++++++++++--- tester | 1 + 14 files changed, 455 insertions(+), 102 deletions(-) create mode 100644 compile_commands.json rename ft_printf.h => includes/ft_printf.h (61%) create mode 100755 includes/libft.h rename ft_print_chars.c => srcs/ft_print_chars.c (59%) rename ft_print_hexs.c => srcs/ft_print_hexs.c (76%) rename ft_print_nbrs.c => srcs/ft_print_nbrs.c (71%) rename ft_print_opts.c => srcs/ft_print_opts.c (100%) rename ft_print_ptrs.c => srcs/ft_print_ptrs.c (84%) create mode 100644 srcs/ft_print_strings.c rename ft_printf.c => srcs/ft_printf.c (55%) create mode 160000 tester diff --git a/.gitignore b/.gitignore index c2ca3cd..0a6137b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ -ft_printf_tester +tester +.cache # ---> C diff --git a/Makefile b/Makefile index 0db9a66..ba6375c 100644 --- a/Makefile +++ b/Makefile @@ -1,33 +1,41 @@ NAME = libftprintf.a +PRINTF = ./lib/libftprintf.a +SPRINTF = ./lib/libftsprintf.a +FPRINTF = ./lib/libftfprintf.a -SRCS = ft_print_chars.c ft_print_hexs.c ft_print_nbrs.c ft_print_opts.c ft_print_ptrs.c ft_printf.c +SRCS = ft_print_chars.c ft_print_hexs.c ft_print_nbrs.c ft_print_opts.c ft_print_ptrs.c ft_printf.c ft_print_strings.c OBJS = ${SRCS:.c=.o} RM = rm -rf CC = gcc CFLAGS = -Werror -Wall -Wextra -AR = ar -rcs + +LIBFT = ./libft/libft.a .c.o: - ${CC} ${CFLAGS} -c $< -o ${<:.c=.o} -g + ${CC} ${CFLAGS} -c $< -o ${<:.c=.o} -g -I./includes all: $(NAME) -bonus: +bonus: $(NAME) -$(NAME): $(OBJS) +$(LIBFT): ${MAKE} -C ./libft - cp ./libft/libft.a libftprintf.a - ${AR} ${NAME} ${OBJS} + cp ./libft/libft.a $(NAME) + cp ./libft/libft.h ./includes/libft.h + +$(PRINTF): $(LIBFT) $(addprefix ./srcs/, ${OBJS}) + ar -rcs $(NAME) $(addprefix ./srcs/, ${OBJS}) + +$(NAME): $(LIBFT) $(PRINTF) clean: - ${RM} ${OBJS} - ${RM} ./libft/*.o + ${RM} srcs/*.o + ${MAKE} clean -C ./libft fclean: clean - ${RM} ${NAME} - ${RM} ./libft/libft.a - + ${RM} ${NAME} $(PRINTF) + ${MAKE} fclean -C ./libft re: fclean all diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..b15e0ea --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,130 @@ +[ + { + "arguments": [ + "/usr/bin/gcc", + "-Werror", + "-Wall", + "-Wextra", + "-c", + "-g", + "-I", + "includes", + "-o", + "srcs/ft_printf.p.o", + "srcs/ft_printf.c" + ], + "directory": "/home/nicolas/Development/ft_printf", + "file": "/home/nicolas/Development/ft_printf/srcs/ft_printf.c", + "output": "/home/nicolas/Development/ft_printf/srcs/ft_printf.p.o" + }, + { + "arguments": [ + "/usr/bin/gcc", + "-Werror", + "-Wall", + "-Wextra", + "-c", + "-g", + "-I", + "includes", + "-o", + "srcs/ft_print_chars.p.o", + "srcs/ft_print_chars.c" + ], + "directory": "/home/nicolas/Development/ft_printf", + "file": "/home/nicolas/Development/ft_printf/srcs/ft_print_chars.c", + "output": "/home/nicolas/Development/ft_printf/srcs/ft_print_chars.p.o" + }, + { + "arguments": [ + "/usr/bin/gcc", + "-Werror", + "-Wall", + "-Wextra", + "-c", + "-g", + "-I", + "includes", + "-o", + "srcs/ft_print_hexs.p.o", + "srcs/ft_print_hexs.c" + ], + "directory": "/home/nicolas/Development/ft_printf", + "file": "/home/nicolas/Development/ft_printf/srcs/ft_print_hexs.c", + "output": "/home/nicolas/Development/ft_printf/srcs/ft_print_hexs.p.o" + }, + { + "arguments": [ + "/usr/bin/gcc", + "-Werror", + "-Wall", + "-Wextra", + "-c", + "-g", + "-I", + "includes", + "-o", + "srcs/ft_print_nbrs.p.o", + "srcs/ft_print_nbrs.c" + ], + "directory": "/home/nicolas/Development/ft_printf", + "file": "/home/nicolas/Development/ft_printf/srcs/ft_print_nbrs.c", + "output": "/home/nicolas/Development/ft_printf/srcs/ft_print_nbrs.p.o" + }, + { + "arguments": [ + "/usr/bin/gcc", + "-Werror", + "-Wall", + "-Wextra", + "-c", + "-g", + "-I", + "includes", + "-o", + "srcs/ft_print_opts.p.o", + "srcs/ft_print_opts.c" + ], + "directory": "/home/nicolas/Development/ft_printf", + "file": "/home/nicolas/Development/ft_printf/srcs/ft_print_opts.c", + "output": "/home/nicolas/Development/ft_printf/srcs/ft_print_opts.p.o" + }, + { + "arguments": [ + "/usr/bin/gcc", + "-Werror", + "-Wall", + "-Wextra", + "-c", + "-g", + "-I", + "includes", + "-o", + "srcs/ft_print_ptrs.p.o", + "srcs/ft_print_ptrs.c" + ], + "directory": "/home/nicolas/Development/ft_printf", + "file": "/home/nicolas/Development/ft_printf/srcs/ft_print_ptrs.c", + "output": "/home/nicolas/Development/ft_printf/srcs/ft_print_ptrs.p.o" + }, + { + "arguments": [ + "/usr/bin/gcc", + "-Werror", + "-Wall", + "-Wextra", + "-c", + "-g", + "-D", + "FPRINTF", + "-I", + "includes", + "-o", + "srcs/ft_printf.f.o", + "srcs/ft_printf.c" + ], + "directory": "/home/nicolas/Development/ft_printf", + "file": "/home/nicolas/Development/ft_printf/srcs/ft_printf.c", + "output": "/home/nicolas/Development/ft_printf/srcs/ft_printf.f.o" + } +] diff --git a/ft_printf.h b/includes/ft_printf.h similarity index 61% rename from ft_printf.h rename to includes/ft_printf.h index 04a54f4..8e0a743 100644 --- a/ft_printf.h +++ b/includes/ft_printf.h @@ -14,10 +14,9 @@ # define MEM_ADD_SIZE 6 -# include "./libft/libft.h" +# include "libft.h" # include - typedef struct s_opts { int len; int width; @@ -30,19 +29,30 @@ typedef struct s_opts { int plus : 1; } t_opts; + +void add_string(char *str, size_t size); +t_dlist *get_last(void); +t_dlist *get_first(void); +void clean_list(void); + char *int_opts_transform(int n, char *nbr, t_opts *opts); char *uint_opts_transform(unsigned int n, char *nbr, t_opts *opts); char *ptr_opts_transform(char *ptr, t_opts *opts); char *str_opts_transform(char *str, t_opts *opts); -int ft_print_char(int ch, t_opts *opts); -int ft_print_str(char *str, t_opts *opts); -int va_print_char(va_list va_ch, const char *str, t_opts *opts); -int va_print_str(va_list va_str, const char *str, t_opts *opts); -int va_print_ptr(va_list va_ptr, const char *str, t_opts *opts); -int va_print_nbr(va_list va_int, const char *str, t_opts *opts); -int va_print_unsign(va_list va_uint, const char *str, t_opts *opts); -int va_print_x(va_list va_uint, const char *str, t_opts *opts); -int va_print_x_cap(va_list va_uint, const char *str, t_opts *opts); -int va_print_perc(va_list va, const char *str, t_opts *opts); +void ft_print_char(int ch, t_opts *opts); +void ft_print_str(char *str, t_opts *opts); +void va_print_char(va_list va_ch, const char *str, t_opts *opts); +void va_print_str(va_list va_str, const char *str, t_opts *opts); +void va_print_ptr(va_list va_ptr, const char *str, t_opts *opts); +void va_print_nbr(va_list va_int, const char *str, t_opts *opts); +void va_print_unsign(va_list va_uint, const char *str, t_opts *opts); +void va_print_x(va_list va_uint, const char *str, t_opts *opts); +void va_print_x_cap(va_list va_uint, const char *str, t_opts *opts); +void va_print_perc(va_list va, const char *str, t_opts *opts); + +void ft_format(const char *str, va_list args); + int ft_printf(const char *str, ...); +int ft_fprintf(int fd, const char *format, ...); +int ft_sprintf(char *ret, const char *format, ...); diff --git a/includes/libft.h b/includes/libft.h new file mode 100755 index 0000000..14abb23 --- /dev/null +++ b/includes/libft.h @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */ +/* Updated: 2022/05/18 16:25:27 by narnaud@stude ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +# include +# include +# include + +typedef struct s_slist +{ + void *content; + struct s_slist *next; +} t_slist; + +typedef struct s_i_slist +{ + int nb; + struct s_i_slist *next; +} t_i_slist; + +typedef struct s_dlist +{ + void *content; + size_t size; + struct s_dlist *next; + struct s_dlist *previous; +} t_dlist; + +int ft_atoi(const char *str); +void ft_bzero(void *s, size_t n); +void *ft_calloc( size_t num, size_t size ); +int ft_isalpha(int ch); +int ft_isascii(int ch); +int ft_isdigit(int ch); +int ft_isprint(int ch); +int ft_isalnum(int ch); +int ft_isspace(int ch); +int ft_isnumber(char *str); +void *ft_memchr(const void *b, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memmove(void *dst, const void *src, size_t n); +void ft_free_split(char **split); + +size_t ft_ilen(int nbr); +size_t ft_ulen(unsigned int nbr); +size_t ft_longbaselen(long nbr, size_t base); +int ft_croissant(int a, int b); +int ft_decroissant(int a, int b); +int ft_max(int a, int b); +int ft_min(int a, int b); + +void *ft_memset(void *b, int c, size_t len); +char *ft_strchr(const char *s, int c); +char *ft_strdup(const char *src); +size_t ft_strlcat(char *dst, const char *src, size_t dstsize); +size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); +size_t ft_strlen(const char *s); +size_t ft_strlen_to(const char *str, char ch); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strnstr(const char *haystack, const char *needle, size_t len); +char *ft_strrchr(const char *s, int c); +void ft_strrev(char **str, unsigned int neg); +int ft_tolower(int c); +int ft_toupper(int c); +char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(const char *s1, const char *s2); +char *ft_strjoin_with(char *s1, char *s2, char *inter); +char *ft_strtrim(char const *s1, char const *set); +char **ft_split(char const *str, char ch); +char *ft_itoa(int n); +char *ft_itox(unsigned long n, const char *base); +char *ft_utoa(unsigned int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putnstr_fd(size_t n, char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +char *ft_append(char *s1, char *s2); +char *ft_strnew(int n, char c); + +t_slist *ft_slst_new(void *content); +void ft_slst_add_front(t_slist **alst, t_slist *new_e); +int ft_slst_size(t_slist *lst); +t_slist *ft_slst_last(t_slist *lst); +void ft_slst_add_back(t_slist **alst, t_slist *new_e); +void ft_slst_delone(t_slist *lst, void (*del)(void *)); +void ft_slst_clear(t_slist **lst, void (*del)(void *)); +void ft_slst_iter(t_slist *lst, void (*f)(void *)); +t_slist *ft_slst_map(t_slist *lst, void *(*f)(void *), void (*del)(void *)); + +size_t ft_ilst_first(t_i_slist *lst, int (*fct)(int a, int b)); +void ft_ilst_free(t_i_slist *list); +int ft_ilst_is_in(int value, t_i_slist lst); + +t_dlist *ft_dlst_add(t_dlist *prev, void *content); +t_dlist *ft_dlst_n(t_dlist *lst, size_t n); +char **ft_dlst_to_arr(t_dlist *ptr); diff --git a/libft b/libft index 9154239..824c048 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 91542393af2673f7cba570fc4b868294810f1abc +Subproject commit 824c048555039c45207bd153a2333a1c2e2174a2 diff --git a/ft_print_chars.c b/srcs/ft_print_chars.c similarity index 59% rename from ft_print_chars.c rename to srcs/ft_print_chars.c index 3b820fb..4d6aecd 100644 --- a/ft_print_chars.c +++ b/srcs/ft_print_chars.c @@ -12,34 +12,38 @@ #include "ft_printf.h" -int ft_print_char(int ch, t_opts *opts) +void ft_print_char(int ch, t_opts *opts) { char c; - int ret; - + char *ret; + int len; c = (char)ch; - ret = 1; + + if (opts->width < 1) + len = 1; + else + len = opts->width; + ret = ft_calloc(len + 1, sizeof(char)); + if (opts->minus) { - ft_putchar_fd(c, 1); - while (opts->width-- > 1 && ++ret) - ft_putchar_fd(' ', 1); + ret[0] = c; + while (opts->width-- > 1) + ret[opts->width] = ' '; } else { - while (opts->width-- > 1 && ++ret) - ft_putchar_fd(' ', 1); - ft_putchar_fd(c, 1); + ret[len - 1] = c; + while (opts->width-- > 1) + ret[opts->width - 1] = ' '; } - return (ret); + + add_string(ret, len); } -int ft_print_str(char *str, t_opts *opts) +void ft_print_str(char *str, t_opts *opts) { - int i; - char *formated; - - i = 0; + char *ret; if (!str) { if (!opts->dot || opts->precision >= 6) @@ -47,29 +51,26 @@ int ft_print_str(char *str, t_opts *opts) else str = ""; } - formated = str_opts_transform(str, opts); - while (formated[i]) - ft_putchar_fd(formated[i++], 1); - free(formated); - return (i); + ret = str_opts_transform(str, opts); + + add_string(ret, ft_strlen(ret)); } -int va_print_char(va_list va_ch, const char *str, t_opts *opts) +void va_print_char(va_list va_ch, const char *str, t_opts *opts) { (void)*str; - return (ft_print_char(va_arg(va_ch, int), opts)); + ft_print_char(va_arg(va_ch, int), opts); } -int va_print_str(va_list va_str, const char *str, t_opts *opts) +void va_print_str(va_list va_str, const char *str, t_opts *opts) { (void)*str; - return (ft_print_str(va_arg(va_str, char *), opts)); + ft_print_str(va_arg(va_str, char *), opts); } -int va_print_perc(va_list va, const char *str, t_opts *opts) +void va_print_perc(va_list va, const char *str, t_opts *opts) { (void)*str; (void)va; ft_print_char('%', opts); - return (1); } diff --git a/ft_print_hexs.c b/srcs/ft_print_hexs.c similarity index 76% rename from ft_print_hexs.c rename to srcs/ft_print_hexs.c index 65ea57e..a671f33 100644 --- a/ft_print_hexs.c +++ b/srcs/ft_print_hexs.c @@ -12,12 +12,11 @@ #include "ft_printf.h" -int ft_print_x(unsigned int n, t_opts *opts) +void ft_print_x(unsigned int n, t_opts *opts) { char *raw; char *nbr; char *tmp; - int ret; raw = ft_itox((unsigned long int)n, "0123456789abcdef"); if (opts->hash && n != 0) @@ -28,18 +27,16 @@ int ft_print_x(unsigned int n, t_opts *opts) } nbr = uint_opts_transform(n, raw, opts); opts->dot = 0; - ret = ft_print_str(nbr, opts); + ft_print_str(nbr, opts); free(raw); free(nbr); - return (ret); } -int ft_print_x_cap(unsigned int n, t_opts *opts) +void ft_print_x_cap(unsigned int n, t_opts *opts) { char *raw; char *tmp; char *nbr; - int ret; raw = ft_itox((unsigned long int)n, "0123456789ABCDEF"); if (opts->hash && n != 0) @@ -50,20 +47,19 @@ int ft_print_x_cap(unsigned int n, t_opts *opts) } nbr = uint_opts_transform(n, raw, opts); opts->dot = 0; - ret = ft_print_str(nbr, opts); + ft_print_str(nbr, opts); free(raw); free(nbr); - return (ret); } -int va_print_x(va_list va_uint, const char *str, t_opts *opts) +void va_print_x(va_list va_uint, const char *str, t_opts *opts) { (void)*str; - return (ft_print_x(va_arg(va_uint, unsigned int), opts)); + ft_print_x(va_arg(va_uint, unsigned int), opts); } -int va_print_x_cap(va_list va_uint, const char *str, t_opts *opts) +void va_print_x_cap(va_list va_uint, const char *str, t_opts *opts) { (void)*str; - return (ft_print_x_cap(va_arg(va_uint, unsigned int), opts)); + ft_print_x_cap(va_arg(va_uint, unsigned int), opts); } diff --git a/ft_print_nbrs.c b/srcs/ft_print_nbrs.c similarity index 71% rename from ft_print_nbrs.c rename to srcs/ft_print_nbrs.c index a7d1f9b..e055d2e 100644 --- a/ft_print_nbrs.c +++ b/srcs/ft_print_nbrs.c @@ -12,44 +12,39 @@ #include "ft_printf.h" -int ft_print_nbr(int n, const char *str, t_opts *opts) +void ft_print_nbr(int n, const char *str, t_opts *opts) { char *nbr; char *raw; - int ret; (void)*str; raw = ft_itoa(n); nbr = int_opts_transform(n, raw, opts); opts->dot = 0; - ret = ft_print_str(nbr, opts); + ft_print_str(nbr, opts); free(raw); free(nbr); - return (ret); } -int ft_print_unsign(unsigned int n, const char *str, t_opts *opts) +void ft_print_unsign(unsigned int n, const char *str, t_opts *opts) { char *raw; char *nbr; - int ret; (void)*str; raw = ft_utoa(n); nbr = uint_opts_transform(n, raw, opts); opts->dot = 0; - ret = ft_print_str(nbr, opts); + ft_print_str(nbr, opts); free(raw); free(nbr); - return (ret); } -int va_print_nbr(va_list va, const char *str, t_opts *opts) +void va_print_nbr(va_list va, const char *str, t_opts *opts) { - return (ft_print_nbr(va_arg(va, int), str, opts)); + ft_print_nbr(va_arg(va, int), str, opts); } - -int va_print_unsign(va_list va, const char *str, t_opts *opts) +void va_print_unsign(va_list va, const char *str, t_opts *opts) { - return (ft_print_unsign(va_arg(va, unsigned int), str, opts)); + ft_print_unsign(va_arg(va, unsigned int), str, opts); } diff --git a/ft_print_opts.c b/srcs/ft_print_opts.c similarity index 100% rename from ft_print_opts.c rename to srcs/ft_print_opts.c diff --git a/ft_print_ptrs.c b/srcs/ft_print_ptrs.c similarity index 84% rename from ft_print_ptrs.c rename to srcs/ft_print_ptrs.c index b05e474..fd5820f 100644 --- a/ft_print_ptrs.c +++ b/srcs/ft_print_ptrs.c @@ -12,11 +12,10 @@ #include "ft_printf.h" -int ft_print_ptr(void *ptr, t_opts *opts) +void ft_print_ptr(void *ptr, t_opts *opts) { char *raw; char *nbr; - int ret; if (ptr == NULL) raw = ft_strdup("(nil)"); @@ -27,14 +26,13 @@ int ft_print_ptr(void *ptr, t_opts *opts) } nbr = ptr_opts_transform(raw, opts); opts->dot = 0; - ret = ft_print_str(nbr, opts); + ft_print_str(nbr, opts); free(raw); free(nbr); - return (ret); } -int va_print_ptr(va_list va_ptr, const char *str, t_opts *opts) +void va_print_ptr(va_list va_ptr, const char *str, t_opts *opts) { (void)*str; - return (ft_print_ptr(va_arg(va_ptr, void *), opts)); + ft_print_ptr(va_arg(va_ptr, void *), opts); } diff --git a/srcs/ft_print_strings.c b/srcs/ft_print_strings.c new file mode 100644 index 0000000..1b58a8e --- /dev/null +++ b/srcs/ft_print_strings.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_strings.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 08:09:49 by narnaud #+# #+# */ +/* Updated: 2021/11/17 09:40:16 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static t_dlist *list = NULL; + +void add_string(char *str, size_t size) +{ + list = ft_dlst_add(list, str); + list->size = size; +} + +t_dlist *get_last(void) +{ + return list; +} + +t_dlist *get_first(void) +{ + t_dlist *tmp = list; + while (tmp->previous) + tmp = tmp->previous; + return tmp; +} + +void clean_list(void) +{ + while (list) + { + free(list->content); + free(list); + list = list->previous; + } + list = NULL; +} diff --git a/ft_printf.c b/srcs/ft_printf.c similarity index 55% rename from ft_printf.c rename to srcs/ft_printf.c index 69e8eae..09df435 100644 --- a/ft_printf.c +++ b/srcs/ft_printf.c @@ -49,11 +49,11 @@ const char *handle_options(va_list args, const char *str, t_opts *opts) return (++str); } -int handle_functions(va_list args, const char *str, t_opts *opts) +void handle_functions(va_list args, const char *str, t_opts *opts) { size_t n; const char *g_types = {"cspdiuxX%"}; - static int (*g_printf_fcts[9])(va_list arg, const char *str, + static void(*g_printf_fcts[9])(va_list arg, const char *str, t_opts *opts) = {va_print_char, va_print_str, va_print_ptr, va_print_nbr, va_print_nbr, va_print_unsign, va_print_x, va_print_x_cap, va_print_perc}; @@ -62,7 +62,7 @@ int handle_functions(va_list args, const char *str, t_opts *opts) while (g_types[n] && g_types[n] != *str && ++n) ; if (!g_types[n]) - return (0); + return ; if (opts->minus) opts->zero = 0; if (opts->precision > 0) @@ -74,30 +74,88 @@ int handle_functions(va_list args, const char *str, t_opts *opts) } if (opts->precision < 0) opts->dot = 0; - return ((g_printf_fcts[n])(args, str, opts)); + (g_printf_fcts[n])(args, str, opts); } -int ft_printf(const char *str, ...) +void ft_format(const char *format, va_list args) { - va_list args; - size_t i; t_opts opts; - i = 0; - va_start(args, str); - while (*str) + while (*format) { - if (*str == '%' && str++) + if (*format == '%' && format++) { opts = (t_opts){0}; - while (*str && !ft_strchr("cspdiuxX%", *str)) - str = handle_options(args, str, &opts); - i += handle_functions(args, str, &opts); + while (*format && !ft_strchr("cspdiuxX%", *format)) + format = handle_options(args, format, &opts); + handle_functions(args, format, &opts); } - else if (++i) - ft_putchar_fd(*str, 1); - str++; + else + add_string(ft_strnew(1, *format), 1); + format++; } +} + +int ft_printf(const char *format, ...) +{ + int ret; + t_dlist *strings; + va_list args; + + va_start(args, format); + ft_format(format, args); + ret = 0; + strings = get_first(); + while (strings) + { + ret += strings->size; + ft_putnstr_fd(strings->size, strings->content, 1); + strings = strings->next; + } + clean_list(); + va_end(args); + return (ret); +} + +int ft_fprintf(int fd, const char *format, ...) +{ + int ret; + t_dlist *strings; + va_list args; + + va_start(args, format); + ft_format(format, args); + ret = 0; + strings = get_first(); + while (strings) + { + ret += strings->size; + ft_putnstr_fd(strings->size, strings->content, fd); + strings = strings->next; + } + clean_list(); va_end(args); - return (i); + return (ret); } + +int ft_sprintf(char *str, const char *format, ...) +{ + int ret; + t_dlist *strings; + va_list args; + + va_start(args, format); + ft_format(format, args); + ret = 0; + strings = get_first(); + while (strings) + { + ret += strings->size; + ft_strlcat(str, strings->content, ret); + strings = strings->next; + } + clean_list(); + va_end(args); + return (ret); +} + diff --git a/tester b/tester new file mode 160000 index 0000000..a053a35 --- /dev/null +++ b/tester @@ -0,0 +1 @@ +Subproject commit a053a3500c9124a5c64c95cd8ff9e78a783932c4