diff --git a/libftasm/includes/ftprintf.h b/libftasm/includes/ftprintf.h index ab3a1d51..1dcb1c76 100644 --- a/libftasm/includes/ftprintf.h +++ b/libftasm/includes/ftprintf.h @@ -3,7 +3,21 @@ # include "libft.h" # include # define ALL_FLAGS "#0- +" -# define ALL_CONVERSIONS "sSpdDioOuUxXcC" +# define ALL_CONVERSIONS "sSpdDioOuUxXcCb" + +typedef struct s_fmt t_fmt; + +typedef char *(t_converter)(t_fmt *fmt, va_list ap); +typedef void (t_sharp_func)(char *str, t_fmt *fmt); + +typedef struct s_conv +{ + char id; + char allowed_flags[6]; + char base[20]; + t_converter *converter; + t_sharp_func *sharp_func; +} t_conv; typedef struct s_fmt { @@ -13,25 +27,21 @@ typedef struct s_fmt char modifier[3]; char conversion; int valid; + t_conv conv; } t_fmt; -typedef char *(*t_converter)(t_fmt *fmt, va_list ap); - -typedef struct s_conv -{ - char id; - char base[20]; - t_converter converter; - char allowed_flags[6]; -} t_conv; - extern t_conv g_convs[]; t_fmt *ft_fmt_init(void); void ft_fmt_print(t_fmt *fmt); +t_fmt *ft_parse(char **format, va_list ap); +void ft_parse_flags(t_fmt *fmt, char **format); +void ft_parse_width(t_fmt *fmt, char **format, va_list ap); +void ft_parse_precision(t_fmt *fmt, char **format, va_list ap); +void ft_parse_modifiers(t_fmt *fmt, char **format); + int ft_printf(const char *format, ...); -t_fmt *ft_parse(char **format); char *ft_transform(t_fmt *fmt, va_list ap); void ft_fmt_error_conv(char conv); @@ -49,9 +59,9 @@ char *ft_unsigned_conversion(t_fmt *fmt, va_list ap); char *ft_str_conversion(t_fmt *fmt, va_list ap); char *ft_char_conversion(t_fmt *fmt, va_list ap); +void ft_pad_sharp_o(char *str, t_fmt *fmt); +void ft_pad_sharp_xb(char *str, t_fmt *fmt); + void ft_pad_left(char *str, t_fmt *fmt); void ft_pad_right(char *str, t_fmt *fmt); -void ft_pad_sharp_o(char *str); -void ft_pad_sharp_x(char *str, t_fmt *fmt); -void ft_pad_sharp(char *str, t_fmt *fmt); #endif diff --git a/libftasm/includes/libft.h b/libftasm/includes/libft.h index 17441022..50e7cacd 100644 --- a/libftasm/includes/libft.h +++ b/libftasm/includes/libft.h @@ -119,4 +119,6 @@ size_t ft_uilen(unsigned int n); int ft_time_isrecent(time_t event); int ft_xattr_print(char *path); int ft_xattr_count(char *path); + +char *ft_path_notdir(char *path); #endif diff --git a/libftasm/src/ft_printf/ft_conversion.c b/libftasm/src/ft_printf/ft_conversion.c index 1179d3d4..cc841b0d 100644 --- a/libftasm/src/ft_printf/ft_conversion.c +++ b/libftasm/src/ft_printf/ft_conversion.c @@ -51,5 +51,7 @@ char *ft_str_conversion(t_fmt *fmt, va_list ap) /* if (ft_strcmp(fmt->modifier, "l") == 0) */ /* va_arg(ap, wchar_t *); */ ret = ft_strdup(va_arg(ap, char *)); + if (fmt->precision && fmt->precision < (int)ft_strlen(ret)) + ret[fmt->precision] = '\0'; return (ret); } diff --git a/libftasm/src/ft_printf/ft_parse.c b/libftasm/src/ft_printf/ft_parse.c index adfccb2e..204e5d6d 100644 --- a/libftasm/src/ft_printf/ft_parse.c +++ b/libftasm/src/ft_printf/ft_parse.c @@ -1,5 +1,25 @@ #include "ftprintf.h" +t_fmt *ft_parse(char **format, va_list ap) +{ + t_fmt *fmt; + + fmt = ft_fmt_init(); + ft_parse_flags(fmt, format); + ft_parse_width(fmt, format, ap); + ft_parse_precision(fmt, format, ap); + ft_parse_modifiers(fmt, format); + fmt->conversion = **format; + (*format)++; + ft_fmt_validate_mod(fmt); + ft_fmt_validate_flags(fmt); + ft_fmt_simplify(fmt); + fmt->valid = ft_fmt_validate_conversion(fmt) ? 0 : 1; + + /* ft_fmt_print(fmt); */ + return (fmt); +} + void ft_parse_flags(t_fmt *fmt, char **format) { int i; @@ -24,31 +44,60 @@ void ft_parse_flags(t_fmt *fmt, char **format) /* fflush(stdout); */ } -void ft_parse_nums(t_fmt *fmt, char **format) +void ft_parse_width(t_fmt *fmt, char **format, va_list ap) { - int i; - char buf[10]; + int i; + char buf[10]; char *str; i = 0; str = *format; - ft_strcpy(buf, "0"); - while (ft_isdigit((int)(str[i]))) - ft_strncat(buf, str + i++, 1); - fmt->width = ft_atoi(buf); - ft_strcpy(buf, "0"); - if (str[i] == '.') + if (str[i] == '*') { i++; - while (ft_isdigit(str[i])) - ft_strncat(buf, str + i++, 1); + fmt->width = va_arg(ap, int); + } + else + { + ft_strcpy(buf, "0"); + while (ft_isdigit((int)(str[i]))) + ft_strncat(buf, str + i++, 1); + fmt->width = ft_atoi(buf); } - fmt->precision = ft_atoi(buf); *format += i; + /* printf("found width: %i\n", fmt->width); */ /* printf("\nparse_nums: %s\n", *format); */ /* fflush(stdout); */ } +void ft_parse_precision(t_fmt *fmt, char **format, va_list ap) +{ + int i; + char buf[10]; + char *str; + + i = 0; + str = *format; + if (str[i] == '.') + { + if (str[++i] == '*') + { + i++; + fmt->precision = va_arg(ap, int); + } + else + { + ft_strcpy(buf, "0"); + while (ft_isdigit(str[i])) + ft_strncat(buf, str + i++, 1); + fmt->precision = ft_atoi(buf); + } + } + /* printf("found preci: %i\n", fmt->precision); */ + *format += i; +} + + void ft_parse_modifiers(t_fmt *fmt, char **format) { char *str; @@ -72,24 +121,3 @@ void ft_parse_modifiers(t_fmt *fmt, char **format) /* printf("\nparse_mods: %s\n", *format); */ /* fflush(stdout); */ } - - -t_fmt *ft_parse(char **format) -{ - t_fmt *fmt; - - fmt = ft_fmt_init(); - ft_parse_flags(fmt, format); - ft_parse_nums(fmt, format); - ft_parse_modifiers(fmt, format); - fmt->conversion = **format; - (*format)++; - - ft_fmt_validate_mod(fmt); - ft_fmt_validate_flags(fmt); - ft_fmt_simplify(fmt); - fmt->valid = ft_fmt_validate_conversion(fmt) ? 0 : 1; - - /* ft_fmt_print(fmt); */ - return (fmt); -} diff --git a/libftasm/src/ft_printf/ft_printf.c b/libftasm/src/ft_printf/ft_printf.c index 407cd68e..32da3bd1 100644 --- a/libftasm/src/ft_printf/ft_printf.c +++ b/libftasm/src/ft_printf/ft_printf.c @@ -2,14 +2,15 @@ t_conv g_convs[] = { - {'d', "0123456789", ft_signed_conversion, "0- +"}, - {'i', "0123456789", ft_signed_conversion, "0- +"}, - {'u', "0123456789", ft_unsigned_conversion, "#0-"}, - {'o', "01234567", ft_unsigned_conversion, "#0-"}, - {'x', "0123456789abcdef", ft_unsigned_conversion, "#0-"}, - {'X', "0123456789ABCDEF", ft_unsigned_conversion, "#0-"}, - {'s', "", ft_str_conversion, "-"}, - {'c', "", ft_char_conversion, "-"}, + {'d', "0- +", "0123456789", &ft_signed_conversion, NULL}, + {'i', "0- +", "0123456789", &ft_signed_conversion, NULL}, + {'u', "0-", "0123456789", &ft_unsigned_conversion, NULL}, + {'o', "#0-", "01234567", &ft_unsigned_conversion, &ft_pad_sharp_o}, + {'x', "#0-", "0123456789abcdef", &ft_unsigned_conversion, &ft_pad_sharp_xb}, + {'X', "#0-", "0123456789ABCDEF", &ft_unsigned_conversion, &ft_pad_sharp_xb}, + {'b', "#0-", "01", &ft_unsigned_conversion, &ft_pad_sharp_xb}, + {'s', "-", "", &ft_str_conversion, NULL}, + {'c', "-", "", &ft_char_conversion, NULL}, }; int ft_printf(const char *format, ...) @@ -22,13 +23,12 @@ int ft_printf(const char *format, ...) va_start(ap1, format); str = ft_strdup(format); ft_bzero(final, 1000); - /* ft_putendl(format); */ while (*str) { if (*str == '%') { str++; - if (!(fmt = ft_parse(&str))) + if (!(fmt = ft_parse(&str, ap1))) return (1); if (!fmt->valid) ft_strncat(final, &fmt->conversion, 1); diff --git a/libftasm/src/ft_printf/ft_tranform.c b/libftasm/src/ft_printf/ft_tranform.c index bd4a2f4a..d6b14839 100644 --- a/libftasm/src/ft_printf/ft_tranform.c +++ b/libftasm/src/ft_printf/ft_tranform.c @@ -9,7 +9,8 @@ char *ft_transform(t_fmt *fmt, va_list ap) i = 0; while (fmt->conversion != g_convs[i].id) i++; - ret = (g_convs[i].converter)(fmt, ap); + fmt->conv = g_convs[i]; + ret = (fmt->conv.converter)(fmt, ap); if (fmt->width > (int)ft_strlen(ret)) { buf = ret; diff --git a/libftasm/src/ft_printf/lib_pad.c b/libftasm/src/ft_printf/lib_pad.c index df2ab857..2dcdd6da 100644 --- a/libftasm/src/ft_printf/lib_pad.c +++ b/libftasm/src/ft_printf/lib_pad.c @@ -2,7 +2,8 @@ void ft_pad_right(char *str, t_fmt *fmt) { - ft_pad_sharp(str, fmt); + if (strchr(fmt->flags, '#')) + (fmt->conv.sharp_func)(str, fmt); while ((int)ft_strlen(str) < fmt->width) ft_strcat(str, " "); } @@ -13,6 +14,7 @@ void ft_pad_left(char *str, t_fmt *fmt) char sign; sign = '\0'; + ft_bzero(buf, 100); if (str[0] == '-' || str[0] == '+' || str[0] == ' ') { sign = str[0]; @@ -29,7 +31,8 @@ void ft_pad_left(char *str, t_fmt *fmt) } if (sign) str--; - ft_pad_sharp(str, fmt); + if (strchr(fmt->flags, '#')) + (fmt->conv.sharp_func)(str, fmt); while ((int)ft_strlen(str) < fmt->width) { ft_strcpy(buf, " "); diff --git a/libftasm/src/ft_printf/lib_pad_sharp.c b/libftasm/src/ft_printf/lib_pad_sharp.c index 9d8cd526..32268b5c 100644 --- a/libftasm/src/ft_printf/lib_pad_sharp.c +++ b/libftasm/src/ft_printf/lib_pad_sharp.c @@ -1,9 +1,11 @@ #include "ftprintf.h" -void ft_pad_sharp_o(char *str) +void ft_pad_sharp_o(char *str, t_fmt *fmt) { char buf[100]; + (void)fmt; + ft_bzero(buf, 100); if (str[0] != '0') { ft_strcpy(buf, "0"); @@ -12,31 +14,19 @@ void ft_pad_sharp_o(char *str) } } -void ft_pad_sharp_x(char *str, t_fmt *fmt) +void ft_pad_sharp_xb(char *str, t_fmt *fmt) { char buf[100]; + int i; - if (str[1] == '0') - str[1] = fmt->conversion; - else - { - ft_strcpy(buf, "0"); - if (str[0] == '0') - str[0] = fmt->conversion; - else - ft_strcat(buf, &fmt->conversion); - ft_strcat(buf, str); - ft_strcpy(str, buf); - } -} - -void ft_pad_sharp(char *str, t_fmt *fmt) -{ - if (ft_strchr(fmt->flags, '#')) - { - if (fmt->conversion == 'x' || fmt->conversion == 'X') - ft_pad_sharp_x(str, fmt); - if (fmt->conversion == 'o') - ft_pad_sharp_o(str); - } + i = 0; + ft_bzero(buf, 100); + ft_strcpy(buf, "0"); + ft_strcat(buf, &fmt->conversion); + if (*str == '0') + i++; + if (*str == '0') + i++; + ft_strcat(buf, str + i); + ft_strcpy(str, buf); } diff --git a/libftasm/src/path/ft_path_notdir.c b/libftasm/src/path/ft_path_notdir.c new file mode 100644 index 00000000..8d788ddb --- /dev/null +++ b/libftasm/src/path/ft_path_notdir.c @@ -0,0 +1,12 @@ +#include "libft.h" + +char *ft_path_notdir(char *path) +{ + char *slash; + char *ret; + + ret = path; + if ((slash = ft_strrchr(path, '/'))) + ret = slash + 1; + return (ret); +} diff --git a/libftasm/src/str/ft_strrchr.c b/libftasm/src/str/ft_strrchr.c index 3c76ce81..f3d77baa 100644 --- a/libftasm/src/str/ft_strrchr.c +++ b/libftasm/src/str/ft_strrchr.c @@ -3,17 +3,15 @@ char *ft_strrchr(const char *s, int c) { char *a; - size_t i; - size_t len; + int i; a = (char *)s; - len = ft_strlen(a); - i = 0; - while (i <= len) + i = ft_strlen(a); + while (i >= 0) { - if (a[len - i] == (char)c) - return (a); - i++; + if (a[i] == (char)c) + return (a + i); + i--; } return (NULL); } diff --git a/libftasm/src/xattr/ft_xattr_count.c b/libftasm/src/xattr/ft_xattr_count.c index e3b6159d..ac0abdc7 100644 --- a/libftasm/src/xattr/ft_xattr_count.c +++ b/libftasm/src/xattr/ft_xattr_count.c @@ -8,6 +8,8 @@ int ft_xattr_count(char *path) int count; i = 0; + ft_bzero(list, FT_XATTR_SIZE); + /* printf("looking for xattr at: %s\n", path); */ listlen = listxattr(path, list, FT_XATTR_SIZE, XATTR_NOFOLLOW); if (listlen == -1) return (-1);