From 07c9232121c8d6cb1e473bd7b623792253375d93 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 29 Oct 2019 19:41:25 +0100 Subject: Added '+' flag and delt with some special cases --- extract.c | 5 ++++- ft_printf.c | 4 +++- header.h | 6 ++++-- printer.c | 47 ++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/extract.c b/extract.c index 2f64b51..11bff1d 100644 --- a/extract.c +++ b/extract.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:10:36 by cacharle #+# #+# */ -/* Updated: 2019/10/29 00:11:02 by cacharle ### ########.fr */ +/* Updated: 2019/10/29 18:17:38 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,8 @@ char *extract_standalone_flags(t_pformat *pformat, char *fmt) pformat->flags |= FLAG_ZERO_PADDING; if (*fmt == '-') pformat->flags |= FLAG_LEFT_ADJUSTED; + if (*fmt == '+') + pformat->flags |= FLAG_SIGNED; fmt++; } return (fmt); @@ -51,6 +53,7 @@ char *extract_precision(t_pformat *pformat, char *fmt) { if (*fmt == 0 || *fmt != '.') return (fmt); + pformat->flags &= ~FLAG_ZERO_PADDING; fmt++; if (*fmt == '*') { diff --git a/ft_printf.c b/ft_printf.c index 5bfd2e8..fa9ba1d 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:15:28 by cacharle #+# #+# */ -/* Updated: 2019/10/29 05:17:55 by cacharle ### ########.fr */ +/* Updated: 2019/10/29 18:28:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,8 @@ int ft_printf(const char *format, ...) int print_len; int i; + if (format == NULL) + return (-1); str = ft_strdup(format); if ((format_list = parse(str)) == NULL) return (-1); diff --git a/header.h b/header.h index 72108ab..af66bd8 100644 --- a/header.h +++ b/header.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:06:46 by cacharle #+# #+# */ -/* Updated: 2019/10/29 05:01:00 by cacharle ### ########.fr */ +/* Updated: 2019/10/29 16:09:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,8 @@ # define HEX_SYMBOLS "0123456789abcdef" # define HEX_MAJ_SYMBOLS "0123456789ABCDEF" -# define IS_STANDALONE_FLAG(c) (c == '0' || c == '-') +# define IN_STR(str, c) (ft_strchr(str, c) != NULL) +# define IS_STANDALONE_FLAG(c) (IN_STR("0-+", c)) # define CONVERSIONS_STR "cspdiuxX%" @@ -41,6 +42,7 @@ # define FLAG_MIN_WIDTH_WILDCARD 0b00000100 # define FLAG_PRECISION_WILDCARD 0b00001000 # define FLAG_MIN_WIDTH_OVERWRITE 0b00010000 +# define FLAG_SIGNED 0b00100000 #include diff --git a/printer.c b/printer.c index 62c00eb..e002551 100644 --- a/printer.c +++ b/printer.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/28 23:19:24 by cacharle #+# #+# */ -/* Updated: 2019/10/29 05:20:14 by cacharle ### ########.fr */ +/* Updated: 2019/10/29 18:46:48 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,8 +18,6 @@ #define ITOA_HEX_LOWER(x) (ft_itoa_base(x, "0123456789abcdef")) #define ITOA_HEX_UPPER(x) (ft_itoa_base(x, "0123456789ABCDEF")) -#define IN_STR(str, c) (ft_strchr(str, c) != NULL) - char *convert(t_pformat *pformat, va_list ap) { char *str; @@ -40,6 +38,13 @@ char *convert(t_pformat *pformat, va_list ap) pformat->precision = va_arg(ap, int); if ((str = convert_type(pformat->conversion, ap)) == NULL) return (NULL); + if (pformat->flags & FLAG_SIGNED && (pformat->conversion == 'd' || pformat->conversion == 'i') + && str[0] != '-') + { + char *tmp = ft_strjoin("+", str); + free(str); + str = tmp; + } if ((str = handle_precision(pformat, str)) == NULL) return (NULL); if (pformat->conversion == 'p') @@ -62,7 +67,10 @@ char *convert_type(t_conversion conversion, va_list ap) str[0] = (char)va_arg(ap, int); } else if (conversion == 's') - str = ft_strdup(va_arg(ap, char*)); + { + char *tmp = va_arg(ap, char*); + str = tmp == NULL ? ft_strdup("(null)") : ft_strdup(tmp); + } else if (conversion == 'p') str = ITOA_HEX_LOWER((long int)va_arg(ap, void*)); else if (conversion == 'd' || conversion == 'i') @@ -94,10 +102,23 @@ char *handle_padding(t_pformat *pformat, char *str) i = len; while (i < pformat->min_width) tmp[i++] = ' '; + tmp[i] = 0; ft_strcpy(str, tmp); } else { + if (IN_STR("+-", str[0]) && pformat->flags & FLAG_ZERO_PADDING) + { + tmp[0] = str[0]; + len--; + i = 1; + while (i < pformat->min_width - len) + tmp[i++] = pformat->flags & FLAG_ZERO_PADDING ? '0' : ' '; + ft_strcpy(tmp + i , str + 1); + ft_strcpy(str, tmp); + free(tmp); + return (str); + } i = 0; while (i <= pformat->min_width - len) tmp[i++] = pformat->flags & FLAG_ZERO_PADDING ? '0' : ' '; @@ -114,16 +135,28 @@ char *handle_precision(t_pformat *pformat, char *str) char *tmp; len = ft_strlen(str); + if (pformat->precision == 0 && str[0] == '0') + return (ft_strdup("")); if (pformat->conversion == 's' && pformat->precision < len) str[pformat->precision] = '\0'; else if (IN_STR("diuxXp", pformat->conversion) && len < pformat->precision) { if ((tmp = (char*)malloc(sizeof(char) * (pformat->precision + 1))) == NULL) return (NULL); + if (IN_STR("+-", str[0])) + { + tmp[0] = str[0]; + len--; + ft_strcpy(tmp + pformat->precision - len + 1, str + 1); + while (pformat->precision-- > len) + tmp[pformat->precision - len + 1] = '0'; + ft_strcpy(str, tmp); + free(tmp); + return (str); + } ft_strcpy(tmp + pformat->precision - len, str); - pformat->precision -= len; - while (pformat->precision-- > 0) - tmp[pformat->precision] = '0'; + while (pformat->precision-- > len) + tmp[pformat->precision - len] = '0'; ft_strcpy(str, tmp); free(tmp); } -- cgit