diff options
| -rw-r--r-- | ft_printf.c | 39 | ||||
| -rw-r--r-- | header.h | 4 | ||||
| -rw-r--r-- | parse.c | 20 | ||||
| -rw-r--r-- | printer.c | 21 |
4 files changed, 57 insertions, 27 deletions
diff --git a/ft_printf.c b/ft_printf.c index 9d82f62..f154685 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -31,12 +31,13 @@ int ft_printf(const char *format, ...) /* ap_index++; */ /* printf("\n%d\n", format_list->data->left_adjusted); */ /* printf("\n%d\n", format_list->data->precision); */ + /* printf("\n%d\n", format_list->data->precision_wildcard); */ + /* printf("\n%d\n", format_list->data->min_field_width); */ if ((str = convert_to_str(format_list->data, ap)) == NULL) return (-1); ft_putstr(str); free(str); - print_len += format_list->data->len; list_pop_front(&format_list); } @@ -49,22 +50,26 @@ 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"); + /* 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("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", 3, 43); + ft_printf("precision wildcard |%*.*d|\n", 5, 3, 43); return 0; } @@ -35,7 +35,9 @@ typedef struct 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; @@ -72,7 +74,7 @@ char *ft_itoa(int n); char *ft_strnew(int size); char *ft_strdup(char *s); char *ft_strcpy(char *dest, const char *src); -void add_padding(t_pformat *pformat, char *str); +void handle_padding(t_pformat *pformat, char *str); char *convert_to_str(t_pformat *pformat, va_list ap); void handle_precision(t_pformat *pformat, char *str); @@ -66,6 +66,12 @@ t_pformat *parse_conversion(char *conversion) pformat->left_adjusted = *start == '-'; start++; } + pformat->min_field_width_wildcard = FALSE; + if (*start == '*') + { + pformat->min_field_width_wildcard = TRUE; + start++; + } if (ft_isdigit(*start)) { pformat->min_field_width = ft_atoi(start); @@ -75,12 +81,20 @@ t_pformat *parse_conversion(char *conversion) else pformat->min_field_width = -1; pformat->precision = -1; + pformat->precision_wildcard = FALSE; if (*start == '.') { start++; - pformat->precision = ft_atoi(start); - while (ft_isdigit(*start)) - start++; + /* printf("\n%s\n", start); */ + if (*start == '*') + pformat->precision_wildcard = TRUE; + else + { + pformat->precision_wildcard = FALSE; + pformat->precision = ft_atoi(start); + while (ft_isdigit(*start)) + start++; + } } return (pformat); } @@ -118,6 +118,12 @@ char *convert_to_str(t_pformat *pformat, va_list ap) t_conversion conversion = pformat->conversion; str = NULL; + if (pformat->min_field_width_wildcard) + pformat->min_field_width = va_arg(ap, int); + if (pformat->precision_wildcard) + pformat->precision = va_arg(ap, int); + /* printf("\n%d\n", pformat->precision); */ + /* pformat->precision = 45; */ if (conversion == CONVERSION_CHAR) { if ((str = ft_strnew(2)) == NULL) @@ -127,7 +133,8 @@ char *convert_to_str(t_pformat *pformat, va_list ap) else if (conversion == CONVERSION_STR) str = ft_strdup(va_arg(ap, char*)); else if (conversion == CONVERSION_PTR) - str = ITOA_HEX_UPPER(va_arg(ap, int)); else if (conversion == CONVERSION_DECIMAL || conversion == CONVERSION_INT) + str = ITOA_HEX_UPPER(va_arg(ap, int)); + else if (conversion == CONVERSION_DECIMAL || conversion == CONVERSION_INT) str = ITOA_DEC(va_arg(ap, int)); else if (conversion == CONVERSION_UINT) str = ITOA_DEC(va_arg(ap, int)); @@ -142,11 +149,11 @@ char *convert_to_str(t_pformat *pformat, va_list ap) str[0] = '%'; } handle_precision(pformat, str); - add_padding(pformat, str); + handle_padding(pformat, str); return (str); } -void add_padding(t_pformat *pformat, char *str) +void handle_padding(t_pformat *pformat, char *str) { char *tmp; int len; @@ -195,9 +202,11 @@ void handle_precision(t_pformat *pformat, char *str) if (len >= pformat->precision) return ; tmp = (char*)malloc(sizeof(char) * (pformat->precision + 1)); - ft_strcpy(tmp + len, str); - while (len-- > 0) - tmp[len] = '0'; + ft_strcpy(tmp + pformat->precision - len, str); + /* printf("\n>%s< %d %d\n", str, len, pformat->precision); */ + pformat->precision -= len; + while (pformat->precision-- > 0) + tmp[pformat->precision] = '0'; ft_strcpy(str, tmp); free(tmp); } |
