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 --- printer.c | 69 ++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 27 deletions(-) (limited to 'printer.c') diff --git a/printer.c b/printer.c index 8884c00..2bc0116 100644 --- a/printer.c +++ b/printer.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* printer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/28 23:19:24 by cacharle #+# #+# */ +/* Updated: 2019/10/29 00:13:53 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include #include #include @@ -9,21 +21,25 @@ char *convert_to_str(t_pformat *pformat, va_list ap) { - char *str; - t_conversion conversion = pformat->conversion; + char *str; + t_conversion conversion; + conversion = pformat->conversion; str = NULL; - if (pformat->min_width.wildcard) + if (pformat->flags & FLAG_MIN_WIDTH_WILDCARD) { - pformat->min_width.value = va_arg(ap, int); - if (pformat->min_width.value < 0) + if (pformat->flags & FLAG_MIN_WIDTH_OVERWRITE) + (void)va_arg(ap, int); + else + pformat->min_width = va_arg(ap, int); + if (pformat->min_width < 0) { - pformat->left_adjusted = TRUE; - pformat->min_width.value *= -1; + pformat->flags |= FLAG_LEFT_ADJUSTED; + pformat->min_width *= -1; } } - if (pformat->precision.wildcard) - pformat->precision.value = va_arg(ap, int); + if (pformat->flags & FLAG_PRECISION_WILDCARD) + pformat->precision = va_arg(ap, int); if (conversion == CONVERSION_CHAR) { if ((str = ft_strnew(2)) == NULL) @@ -66,16 +82,17 @@ void handle_padding(t_pformat *pformat, char *str) int len; int i; - if (pformat->min_width.value == -1) + if (pformat->min_width == -1) return; - if ((len = ft_strlen(str)) >= pformat->min_width.value) + if ((len = ft_strlen(str)) >= pformat->min_width) return; - tmp = (char*)malloc(sizeof(char) * (pformat->min_width.value + 1)); - if (!pformat->left_adjusted) + //protect this + tmp = (char*)malloc(sizeof(char) * (pformat->min_width + 1)); + if (!(pformat->flags & FLAG_LEFT_ADJUSTED)) { i = 0; - while (i <= pformat->min_width.value - len) - tmp[i++] = pformat->zero_padding ? '0' : ' '; + while (i <= pformat->min_width - len) + tmp[i++] = pformat->flags & FLAG_ZERO_PADDING ? '0' : ' '; ft_strcpy(tmp + i - 1, str); ft_strcpy(str, tmp); } @@ -83,39 +100,37 @@ void handle_padding(t_pformat *pformat, char *str) { ft_strcpy(tmp, str); i = len; - while (i < pformat->min_width.value) + while (i < pformat->min_width) tmp[i++] = ' '; ft_strcpy(str, tmp); } free(tmp); } -/* #include */ void handle_precision(t_pformat *pformat, char *str) { int len; char *tmp; t_conversion conv; - /* printf("\n%d\n", pformat->precision.value); */ - if (pformat->precision.value == -1) + if (pformat->precision == -1) return ; len = ft_strlen(str); conv = pformat->conversion; if (conv == CONVERSION_STR) - str[pformat->precision.value] = '\0'; + str[pformat->precision] = '\0'; else if (conv == CONVERSION_DECIMAL || conv == CONVERSION_INT || conv == CONVERSION_UINT || conv == CONVERSION_HEX_LOWER || conv == CONVERSION_HEX_UPPER) { - if (len >= pformat->precision.value) + if (len >= pformat->precision) return ; - tmp = (char*)malloc(sizeof(char) * (pformat->precision.value + 1)); - ft_strcpy(tmp + pformat->precision.value - len, str); - /* printf("\n>%s< %d %d\n", str, len, pformat->precision); */ - pformat->precision.value -= len; - while (pformat->precision.value-- > 0) - tmp[pformat->precision.value] = '0'; + // protect this + tmp = (char*)malloc(sizeof(char) * (pformat->precision + 1)); + ft_strcpy(tmp + pformat->precision - len, str); + pformat->precision -= len; + while (pformat->precision-- > 0) + tmp[pformat->precision] = '0'; ft_strcpy(str, tmp); free(tmp); } -- cgit