From 60733a2298c7a93fe681f78af9b69e1639a791b5 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 14 Oct 2019 16:26:31 +0200 Subject: WIP: format parsing arguments extraction Changed pformat struct, everything is broken --- extract.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ft_printf.c | 44 +++++++++++++-------------- header.h | 39 +++++++++++++++++------- parse.c | 25 ++++++++++++++++ printer.c | 20 ++++++------- 5 files changed, 184 insertions(+), 43 deletions(-) create mode 100644 extract.c diff --git a/extract.c b/extract.c new file mode 100644 index 0000000..a6a3bba --- /dev/null +++ b/extract.c @@ -0,0 +1,99 @@ +#include "header.h" + +char *extract_ap_index(t_pformat *pformat, char *fmt) +{ + int i; + int tmp; + char *fmt_dup; + + if (!ft_isdigit(*fmt) || *fmt == '0') + return (fmt); + tmp = ft_atoi(fmt); + i = 0; + while (ft_isdigit(fmt[i])) + i++; + if (fmt[i] == '$') + { + pformat->ap_index = tmp; + fmt_dup = ft_strdup(&fmt[++i]); + free(fmt); + return (fmt_dup); + } + return (fmt); +} + +char *extract_standalone_flags(t_pformat *pformat, char *fmt) +{ + int i; + char *fmt_dup; + + i = -1; + while (IS_STANDALONE_FLAG(fmt[++i])) + { + if (!pformat->zero_padding) + pformat->zero_padding = fmt[i] == '0'; + if (!pformat->left_adjusted) + pformat->left_adjusted = fmt[i] == '-'; + } + fmt_dup = ft_strdup(&fmt[i]); + free(fmt); + return (fmt_dup); +} + +char *extract_min_width(t_pformat *pformat, char *fmt) +{ + int i; + int tmp; + char fmt_dup; + + + i = 0; + if (*fmt == '*') + { + pformat->min_width->wildcard->exist = TRUE; + i++; + } + if (ft_isdigit(fmt[i])) + { + tmp = ft_atoi(fmt[i]); + while (ft_isdigit(fmt[i])) + i++; + if (fmt[i] == '$') + pformat->min_width->wildcard->ap_index = tmp; + else + { + pformat->min_width->hardcoded = tmp; + pformat->min_width->wildcard->exist = FALSE; + } + } + fmt_dup = ft_strdup(&fmt[i]); + free(fmt); + return (fmt_dup); +} + +char *extract_precision(t_pformat *pformat, char *fmt) +{ + int i; + char *fmt_dup; + + if (*fmt != '.') + return (fmt); + i = 1; + if (fmt[i] == '*') + { + pformat->precision->wildcard->exist = TRUE; + i++; + } + else if (!ft_isdigit(fmt[i])) + pformat->precision->hardcoded = 0; + tmp = ft_atoi(fmt[i]); + while (ft_isdigit(fmt[i])) + i++; + if (pformat->precision->wildcard->exist && fmt[i] == '$') + pformat->precision->wilcard->ap_index = tmp; + else + pformat->precision->hardcoded = tmp; + fmt_dup = ft_strdup(&fmt[i]); + free(fmt); + return (fmt); +} diff --git a/ft_printf.c b/ft_printf.c index bad3fa3..beabcd7 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -50,28 +50,28 @@ int ft_printf(const char *format, ...) int main() { - /* int test; */ - /* */ - /* ft_printf("char: %c\n", 'r'); */ - /* ft_printf("string: %s\n", "bonjour"); */ - /* ft_printf("pointer: %p\n", &test); */ - /* ft_printf("int: %d or %i\n", 45, 54); */ - /* ft_printf("uint: %u\n", 1 << 31); */ - /* ft_printf("hex lower: %x\n", 0xabcf012); */ - /* ft_printf("hex upper: %X\n", 0xabcf012); */ - /* ft_printf("percent: %%\n"); */ - /* */ - /* ft_printf("precision |%.10d|\n", 43); */ - /* ft_printf("string precision |%.1s|\n", "bonjour"); */ - /* ft_printf("min width |%9d|\n", 43); */ - /* ft_printf("zero padding |%09d|\n", 43); */ - /* ft_printf("left adjusted |%-9d|\n", 43); */ - /* ft_printf("string padding |%9s|\n", "bon"); */ - /* */ - /* ft_printf("width wildcard |%*d|\n", 5, 43); */ - /* ft_printf("precision wildcard |%.*d|\n", 5, 43); */ - /* ft_printf("precision/width wildcard |%*.*d|\n", 5, 3, 43); */ - /* ft_printf("left adjusted |%*d|\n", -5, 43); */ + int test; + + ft_printf("char: %c\n", 'r'); + ft_printf("string: %s\n", "bonjour"); + ft_printf("pointer: %p\n", &test); + ft_printf("int: %d or %i\n", 45, 54); + ft_printf("uint: %u\n", 1 << 31); + ft_printf("hex lower: %x\n", 0xabcf012); + ft_printf("hex upper: %X\n", 0xabcf012); + ft_printf("percent: %%\n"); + + ft_printf("precision |%.10d|\n", 43); + ft_printf("string precision |%.1s|\n", "bonjour"); + ft_printf("min width |%9d|\n", 43); + ft_printf("zero padding |%09d|\n", 43); + ft_printf("left adjusted |%-9d|\n", 43); + ft_printf("string padding |%9s|\n", "bon"); + + ft_printf("width wildcard |%*d|\n", 5, 43); + ft_printf("precision wildcard |%.*d|\n", 5, 43); + ft_printf("precision/width wildcard |%*.*d|\n", 5, 3, 43); + ft_printf("left adjusted |%*d|\n", -5, 43); ft_printf("overwrite |%*3d|\n", 5, 43); ft_printf("overwrite |%*-1d|\n", 0, 43); diff --git a/header.h b/header.h index 8c125bb..c770d35 100644 --- a/header.h +++ b/header.h @@ -31,16 +31,24 @@ typedef int t_bool; typedef struct { - int ap_index; - t_bool left_adjusted; - t_bool zero_padding; - int precision; - t_bool precision_wildcard; - int min_field_width; - t_bool min_field_width_wildcard; - t_conversion conversion; - int len; -} t_pformat; + int hardcoded; + struct + { + t_bool exist; + int ap_index; + } wildcard; +} t_maybe_wildcard; + +typedef struct +{ + int ap_index; + t_bool left_adjusted; + t_bool zero_padding; + t_maybe_wildcard precision; + t_maybe_wildcard min_width; + t_conversion conversion; + int len; +} t_pformat; typedef struct s_list { @@ -82,13 +90,22 @@ void handle_precision(t_pformat *pformat, char *str); ** utils.c */ -int ft_atoi(const char *str); +int ft_atoi(const char *str); char *ft_strndup(const char *s1, int n); char *ft_strrchr(const char *s, int c); int strrchr_index(const char *s, char c); int ft_strlen(char *str); t_bool ft_isdigit(char c); +/* +** extract.c +*/ + +char *extract_ap_index(t_pformat *pformat, char *fmt); +char *extract_min_width(t_pformat *pformat, char *fmt); +char *extract_standalone_flags(t_pformat *pformat, char *fmt); +char *extract_precision(t_pformat *pformat, char *fmt); + /* ** list.c */ diff --git a/parse.c b/parse.c index 66ff9ee..17cbb22 100644 --- a/parse.c +++ b/parse.c @@ -4,6 +4,10 @@ #define STRRCHR_CONVERSIONS(c) (ft_strrchr(CONVERSIONS_STR, c)) #define IS_STANDALONE_FLAG(c) (c == '0' || c == '-') +/* +** %(?:\d+\$)?[-]?(?:[0]|'.{1})?-?\d*(?:\.\d+)?[cdusxX] +*/ + t_list *parse(const char *format) { t_list *format_list; @@ -38,6 +42,27 @@ char *isolate_conversion(const char *conversion_start) return (ft_strndup(conversion_start, i + 1)); } + +t_pformat *parse_reduced_fmt(char *fmt) +{ + t_pformat *pformat; + + if (conversion == NULL) + return (NULL); + if ((pformat = (t_pformat*)malloc(sizeof(t_pformat))) == NULL) + return (NULL); + pformat->ap_index = -1; + pformat->left_adjusted = FALSE; + pformat->zero_padding = FALSE; + pformat->min_width->wildcard->exist = FALSE; + pformat->precision->wildcard->exist = FALSE; + fmt = extrac_ap_index(pformat, fmt); + fmt = extrac_standalone_flags(pformat, fmt); + fmt = extract_min_width(pformat, fmt); + fmt = extract_precision(pformat, fmt); + return (pformat); +} + // %[position][dollar][flags][width][.precision][length]type t_pformat *parse_conversion(char *conversion) { diff --git a/printer.c b/printer.c index 043bf23..de36b4e 100644 --- a/printer.c +++ b/printer.c @@ -118,17 +118,17 @@ char *convert_to_str(t_pformat *pformat, va_list ap) t_conversion conversion = pformat->conversion; str = NULL; - if (pformat->min_field_width_wildcard) + if (pformat->min_width.wildcard.exist) { - if (pformat->min_field_width != -1) + if (pformat->min_width != -1) va_arg(ap, int); else { - pformat->min_field_width = va_arg(ap, int); - if (pformat->min_field_width < 0) + pformat->min_width = va_arg(ap, int); + if (pformat->min_width < 0) { pformat->left_adjusted = TRUE; - pformat->min_field_width = -pformat->min_field_width; + pformat->min_width = -pformat->min_width; } } } @@ -172,15 +172,15 @@ void handle_padding(t_pformat *pformat, char *str) int len; int i; - if (pformat->min_field_width == -1) + if (pformat->min_width == -1) return; - if ((len = ft_strlen(str)) >= pformat->min_field_width) + if ((len = ft_strlen(str)) >= pformat->min_width) return; - tmp = (char*)malloc(sizeof(char) * (pformat->min_field_width + 1)); + tmp = (char*)malloc(sizeof(char) * (pformat->min_width + 1)); if (!pformat->left_adjusted) { i = 0; - while (i <= pformat->min_field_width - len) + while (i <= pformat->min_width - len) tmp[i++] = pformat->zero_padding ? '0' : ' '; ft_strcpy(tmp + i - 1, str); ft_strcpy(str, tmp); @@ -189,7 +189,7 @@ void handle_padding(t_pformat *pformat, char *str) { ft_strcpy(tmp, str); i = len; - while (i < pformat->min_field_width) + while (i < pformat->min_width) tmp[i++] = ' '; ft_strcpy(str, tmp); } -- cgit