diff options
| -rw-r--r-- | ft_printf.c | 5 | ||||
| -rw-r--r-- | header.h | 2 | ||||
| -rw-r--r-- | parse.c | 42 | ||||
| -rw-r--r-- | utils.c | 9 |
4 files changed, 51 insertions, 7 deletions
diff --git a/ft_printf.c b/ft_printf.c index 1c0d8ea..9540489 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -60,5 +60,10 @@ int main() ft_printf("hex lower: %x\n", 0xabcf012); ft_printf("hex upper: %X\n", 0xabcf012); ft_printf("percent: %%\n"); + + /* ft_printf("precision %.2d\n", 43); */ + ft_printf("min width %4d\n", 43); + ft_printf("zero padding %04d\n", 43); + ft_printf("right adjusted %-4d\n", 43); return 0; } @@ -58,6 +58,7 @@ int ft_printf(const char *format, ...); t_list *parse(const char *format); char *isolate_conversion(const char *conversion_start); t_pformat *parse_conversion(char *conversion); +char *parse_arg_position(char *conversion, t_pformat *pformat); /* ** printer.c @@ -78,6 +79,7 @@ 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); /* ** list.c @@ -2,6 +2,7 @@ #include "header.h" #define STRRCHR_CONVERSIONS(c) (ft_strrchr(CONVERSIONS_STR, c)) +#define IS_STANDALONE_FLAG(c) (c == '0' || c == '-') t_list *parse(const char *format) { @@ -37,9 +38,11 @@ char *isolate_conversion(const char *conversion_start) return (ft_strndup(conversion_start, i + 1)); } +// %[position][dollar][flags][width][.precision][length]type t_pformat *parse_conversion(char *conversion) { int i; + char *start; t_pformat *pformat; if (conversion == NULL) @@ -47,12 +50,41 @@ t_pformat *parse_conversion(char *conversion) if ((pformat = (t_pformat*)malloc(sizeof(t_pformat))) == NULL) return (NULL); i = ft_strlen(conversion) - 1; + pformat->len = i + 1; pformat->conversion = strrchr_index(CONVERSIONS_STR, conversion[i]); i--; - pformat->arg_position = 0; - pformat->left_adjusted = FALSE; - pformat->zero_padding = FALSE; - pformat->min_field_width = -1; - pformat->len = 1; + if ((conversion = parse_arg_position(conversion, pformat)) == NULL) + return (NULL); + start = conversion; + while (IS_STANDALONE_FLAG(*start)) + { + if (!pformat->zero_padding) + pformat->zero_padding = *start == '0'; + if (!pformat->left_adjusted) + pformat->left_adjusted = *start == '-'; + start++; + } + if (ft_isdigit(*start)) + pformat->min_field_width = ft_atoi(start); + else + pformat->min_field_width = -1; return (pformat); } + +char *parse_arg_position(char *conversion, t_pformat *pformat) +{ + if (strrchr_index(conversion, '$') != -1) + { + if ((pformat->arg_position = ft_atoi(conversion)) == 0) + { + free(pformat); + return (NULL); + } + while (ft_isdigit(*conversion)) + conversion++; + conversion++; + } + else + pformat->arg_position = -1; + return (conversion); +} @@ -64,7 +64,7 @@ char *ft_strrchr(const char *s, int c) { int i; - i = ft_strlen(s) - 1; + i = ft_strlen((char*)s) - 1; while (s[i] != (char)c) { if (i == 0) @@ -78,7 +78,7 @@ int strrchr_index(const char *s, char c) { int i; - i = ft_strlen(s) - 1; + i = ft_strlen((char*)s) - 1; while (s[i] != c) { if (i == 0) @@ -98,3 +98,8 @@ int ft_strlen(char *str) counter++; return (counter); } + +t_bool ft_isdigit(char c) +{ + return (c >= '0' && c <= '9'); +} |
