diff options
| -rw-r--r-- | extract.c | 57 | ||||
| -rw-r--r-- | ft_printf.c | 74 | ||||
| -rw-r--r-- | header.h | 67 | ||||
| -rw-r--r-- | list.c | 49 | ||||
| -rw-r--r-- | parse.c | 39 | ||||
| -rw-r--r-- | printer.c | 4 |
6 files changed, 113 insertions, 177 deletions
@@ -1,32 +1,9 @@ #include <stdlib.h> #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 = 0; while (IS_STANDALONE_FLAG(fmt[i])) @@ -37,22 +14,18 @@ char *extract_standalone_flags(t_pformat *pformat, char *fmt) pformat->left_adjusted = fmt[i] == '-'; i++; } - fmt_dup = ft_strdup(&fmt[i]); - free(fmt); - return (fmt_dup); + return (fmt + i); } 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; + pformat->min_width.wildcard = TRUE; i++; } else @@ -62,33 +35,24 @@ char *extract_min_width(t_pformat *pformat, char *fmt) tmp = ft_atoi(&fmt[i]); while (ft_isdigit(fmt[i])) i++; - /* printf("%d\n", tmp); */ - if (fmt[i] == '$') - pformat->min_width.wildcard.ap_index = tmp; - else - { - pformat->min_width.value = tmp; - pformat->min_width.wildcard.exist = FALSE; - } + pformat->min_width.value = tmp; + pformat->min_width.wildcard = FALSE; } } - fmt_dup = ft_strdup(&fmt[i]); - free(fmt); - return (fmt_dup); + return (fmt + i); } char *extract_precision(t_pformat *pformat, char *fmt) { int i; int tmp; - char *fmt_dup; if (*fmt != '.') return (fmt); i = 1; if (fmt[i] == '*') { - pformat->precision.wildcard.exist = TRUE; + pformat->precision.wildcard = TRUE; i++; } else if (!ft_isdigit(fmt[i])) @@ -96,11 +60,6 @@ char *extract_precision(t_pformat *pformat, char *fmt) tmp = ft_atoi(&fmt[i]); while (ft_isdigit(fmt[i])) i++; - if (pformat->precision.wildcard.exist && fmt[i] == '$') - pformat->precision.wildcard.ap_index = tmp; - else - pformat->precision.value = tmp; - fmt_dup = ft_strdup(&fmt[i]); - free(fmt); - return (fmt); + pformat->precision.value = tmp; + return (fmt + i); } diff --git a/ft_printf.c b/ft_printf.c index 27b4734..ce7f15c 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -5,47 +5,35 @@ int ft_printf(const char *format, ...) { - int print_len; - /* va_list ap; */ - /* #<{(| va_list ap_start; |)}># */ - /* #<{(| int ap_index; |)}># */ - /* t_list *format_list; */ - /* char *str; */ - /* */ - /* if ((format_list = parse(format)) == NULL) */ - /* return (-1); */ - /* #<{(| ap_index = 0; |)}># */ - /* va_start(ap, format); */ - /* */ - /* #<{(| va_copy(ap_start, ap); |)}># */ - /* print_len = -1; */ - /* while (format[++print_len]) */ - /* { */ - /* if (format[print_len] == '%') */ - /* { */ - /* if (format_list->data->ap_index != -1) */ - /* { */ - /* ap_index = format_list->data->ap_index; */ - /* } */ - /* else */ - /* 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->content, ap)) == NULL) */ - /* return (-1); */ - /* ft_putstr(str); */ - /* free(str); */ - /* */ - /* print_len += format_list->content->len; */ - /* list_pop_front(&format_list); */ - /* } */ - /* else */ - /* write(STDOUT_FILENO, format + print_len, 1); */ - /* } */ - /* va_end(ap); */ - (void)format; - print_len = 0; - return (print_len); + va_list ap; + t_flist *format_list; + char *str; + int print_len; + int i; + + if ((format_list = parse(format)) == NULL) + return (-1); + 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); } @@ -29,78 +29,71 @@ typedef int t_bool; typedef struct { - int value; - struct - { - t_bool exist; - int ap_index; - } wildcard; + int value; + t_bool 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_pformat_list + 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_flist { - struct s_pformat_list *next; - t_pformat *content; -} t_pformat_list; + struct s_flist *next; + t_pformat *content; +} t_flist; /* ** ft_printf.c */ -int ft_printf(const char *format, ...); +int ft_printf(const char *format, ...); /* ** parse.c */ -t_pformat_list *parse(const char *format); -char *isolate_conversion(const char *conversion_start); -t_pformat *parse_reduced(char *fmt); +t_flist *parse(const char *format); +char *isolate_conversion(const char *conversion_start); +t_pformat *parse_reduced(char *fmt); /* ** printer.c */ -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); +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); /* ** utils.c */ -int strrchr_index(const char *s, char c); +int strrchr_index(const char *s, 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); +char *extract_standalone_flags(t_pformat *pformat, char *fmt); +char *extract_min_width(t_pformat *pformat, char *fmt); +char *extract_precision(t_pformat *pformat, char *fmt); /* ** list.c */ -t_pformat_list *list_new(t_pformat *content); -void *list_destroy(t_pformat_list **lst); -void list_push_front(t_pformat_list **lst, t_pformat_list *new); -void list_push_back(t_pformat_list **lst, t_pformat_list *new); -void list_pop_front(t_pformat_list **lst); +t_flist *list_new(t_pformat *content); +void *list_destroy(t_flist **lst); +void list_push_front(t_flist **lst, t_flist *new); +void list_pop_front(t_flist **lst); +void list_reverse(t_flist **lst); #endif @@ -1,18 +1,18 @@ #include <stdlib.h> #include "header.h" -t_pformat_list *list_new(t_pformat *content) +t_flist *list_new(t_pformat *content) { - t_pformat_list *lst; + t_flist *lst; - if ((lst = (t_pformat_list*)malloc(sizeof(t_pformat_list))) == NULL) + if ((lst = (t_flist*)malloc(sizeof(t_flist))) == NULL) return NULL; lst->content = content; lst->next = NULL; return (lst); } -void *list_destroy(t_pformat_list **lst) +void *list_destroy(t_flist **lst) { if (lst == NULL) return (NULL); @@ -22,7 +22,7 @@ void *list_destroy(t_pformat_list **lst) } -void list_push_front(t_pformat_list **lst, t_pformat_list *new) +void list_push_front(t_flist **lst, t_flist *new) { if (lst == NULL || new == NULL) return ; @@ -30,26 +30,9 @@ void list_push_front(t_pformat_list **lst, t_pformat_list *new) *lst = new; } -void list_push_back(t_pformat_list **lst, t_pformat_list *new) +void list_pop_front(t_flist **lst) { - t_pformat_list *cursor; - - if (lst == NULL || new == NULL) - return ; - if (*lst == NULL) - { - *lst = new; - return ; - } - cursor = *lst; - while (cursor->next != NULL) - cursor = cursor->next; - cursor->next = new; -} - -void list_pop_front(t_pformat_list **lst) -{ - t_pformat_list *tmp; + t_flist *tmp; if (lst == NULL || *lst == NULL) return ; @@ -58,3 +41,21 @@ void list_pop_front(t_pformat_list **lst) free(*lst); *lst = tmp; } + +void list_reverse(t_flist **lst) +{ + t_flist *cursor; + t_flist *prev; + t_flist *tmp; + + prev = NULL; + cursor = *lst; + while (cursor != NULL) + { + tmp = cursor; + cursor->next = prev; + prev = cursor; + cursor = tmp->next; + } + *lst = prev; +} @@ -8,10 +8,10 @@ */ #include <stdio.h> -t_pformat_list *parse(const char *format) +t_flist *parse(const char *format) { - t_pformat_list *format_list; - t_pformat_list *tmp; + t_flist *format_list; + t_flist *tmp; t_pformat *parsed; format_list = NULL; @@ -27,40 +27,35 @@ t_pformat_list *parse(const char *format) return (list_destroy(&format_list)); if ((tmp = list_new(parsed)) == NULL) return (list_destroy(&format_list)); - list_push_back(&format_list, tmp); + list_push_front(&format_list, tmp); } + list_reverse(&format_list); return (format_list); } -char *isolate_conversion(const char *conversion_start) -{ - int i; - - i = 0; - while (strrchr_index(CONVERSIONS_STR, conversion_start[i]) == -1) - i++; - return (ft_strndup(conversion_start, i + 1)); -} - t_pformat *parse_reduced(char *fmt) { t_pformat *pformat; + int i; - if (fmt == NULL) + i = 0; + while (strrchr_index(CONVERSIONS_STR, fmt[i]) == -1) + i++; + if ((fmt = ft_strndup(fmt, i + 1)) == 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; pformat->precision.value = -1; + pformat->precision.wildcard = FALSE; + pformat->min_width.value = -1; + pformat->min_width.wildcard = FALSE; pformat->len = ft_strlen(fmt); pformat->conversion = strrchr_index(CONVERSIONS_STR, fmt[pformat->len - 1]); - fmt[pformat->len - 1] = 0; - fmt = extract_standalone_flags(pformat, fmt); - fmt = extract_min_width(pformat, fmt); - fmt = extract_precision(pformat, fmt); + fmt[pformat->len - 1] = '\0'; + fmt = extract_standalone_flags(pformat, fmt + i); + fmt = extract_min_width(pformat, fmt + i); + fmt = extract_precision(pformat, fmt + i); return (pformat); } @@ -13,7 +13,7 @@ char *convert_to_str(t_pformat *pformat, va_list ap) t_conversion conversion = pformat->conversion; str = NULL; - if (pformat->min_width.wildcard.exist) + if (pformat->min_width.wildcard) { pformat->min_width.value = va_arg(ap, int); if (pformat->min_width.value < 0) @@ -22,7 +22,7 @@ char *convert_to_str(t_pformat *pformat, va_list ap) pformat->min_width.value *= -1; } } - if (pformat->precision.wildcard.exist) + if (pformat->precision.wildcard) pformat->precision.value = va_arg(ap, int); if (conversion == CONVERSION_CHAR) { |
