From 87bce91050b56915dcf5964f6f66d5f47299e7f3 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 29 Oct 2019 01:57:41 +0100 Subject: Reworking, binary flags, extract advance - Binary flags attribute instead of multiple bool - Extract doesnst strdup fmt each time, just advance the current pointer - In parsing push front then reverse - Moved some functions to libf --- ft_printf.c | 71 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 27 deletions(-) (limited to 'ft_printf.c') diff --git a/ft_printf.c b/ft_printf.c index ce7f15c..5a57a23 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -1,39 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/29 00:15:28 by cacharle #+# #+# */ +/* Updated: 2019/10/29 00:16:41 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include #include #include +#include "libft.h" #include "header.h" -int ft_printf(const char *format, ...) +int ft_printf(const char *format, ...) { - va_list ap; + va_list ap; t_flist *format_list; char *str; - int print_len; - int i; + int print_len; + int i; - if ((format_list = parse(format)) == NULL) + str = ft_strdup(format); + if ((format_list = parse(str)) == NULL) return (-1); - va_start(ap, format); - print_len = 0; + free(str); + va_start(ap, format); + print_len = 0; i = -1; - while (format[++i]) - { - if (format[i] == '%') - { - if ((str = convert_to_str(format_list->content, ap)) == NULL) - { - list_destroy(&format_list); - return (-1); - } - ft_putstr(str); - print_len += ft_strlen(str); - free(str); - i += format_list->content->len; - list_pop_front(&format_list); - } - else - write(STDOUT_FILENO, format + i, 1); - } - va_end(ap); - return (print_len + i); + while (format[++i]) + { + if (format[i] != '%') + { + write(STDOUT_FILENO, format + i, 1); + continue ; + } + str = convert_to_str(format_list->content, ap); + if (str == NULL) + { + list_destroy(&format_list); + return (-1); + } + ft_putstr(str); + print_len += ft_strlen(str); + free(str); + i += format_list->content->len; + list_pop_front(&format_list); + } + list_destroy(&format_list); + va_end(ap); + return (print_len + i); } -- cgit