diff --git a/includes/ft_printf.h b/includes/ft_printf.h index c5633ab..d00ee8e 100644 --- a/includes/ft_printf.h +++ b/includes/ft_printf.h @@ -6,7 +6,7 @@ /* By: alsantia +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/03/13 15:11:01 by alsantia #+# #+# */ -/* Updated: 2021/04/25 17:00:51 by alsantia ### ########.fr */ +/* Updated: 2021/04/25 19:39:10 by alsantia ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,7 @@ typedef struct s_flags char type; } t_flags; +void ft_fetch(const char *str, char *buf); size_t ft_fill(size_t n, char c); int ft_isspecifier(int c); int ft_isdigit(int c); @@ -39,7 +40,6 @@ size_t ft_putchar(char c); void ft_putnbr(int n, char *res, int is_neg, int mod); void ft_putnbr_u(unsigned int nb, char *res); void ft_putnbr_h(unsigned long nb, char *res, char c, int is_pointer); -//void ft_putnbr_h(unsigned int nb, char *res, char c); size_t ft_putstr(char *s); size_t ft_putstr_n(char *s, size_t n); void ft_str_reverse(char *str); @@ -50,7 +50,7 @@ size_t ft_treat_type_d(t_flags *flag, va_list ap, char *buf, size_t count); size_t ft_treat_type_p(t_flags *flag, va_list ap, char *buf, size_t count); size_t ft_treat_type_perc(t_flags *flag); size_t ft_treat_type_s(t_flags *flag, va_list ap, size_t count); -size_t ft_treat_type_x(t_flags *flag, va_list ap, char *buf); +size_t ft_treat_type_x(t_flags *flag, va_list ap, char *buf, char ch); size_t ft_treat_type_u(t_flags *flag, va_list ap, char *buf); #endif diff --git a/main.c b/main.c index 3a45b3b..f02dbdf 100644 --- a/main.c +++ b/main.c @@ -6,11 +6,49 @@ int main() { -// static char a01; + + puts("123456789012345678901234567890"); + printf("% *.5i|\n", 4, 42); + ft_printf("% *.5i|\n", 4, 42); +/* + int a = -4; + int b = 0; + char c = 'a'; + int d = 2147483647; + int e = -2147483648; + int f = 42; + int g = 25; + int h = 4200; + int i = 8; + int j = -12; + int k = 123456789; + int l = 0; + int m = -12345678; + char *n = "abcdefghijklmnop"; + char *o = "-a"; + char *p = "-12"; + char *q = "0"; + char *r = "%%"; + char *s = "-2147483648"; + char *t = "0x12345678"; + char *u = "-0"; puts("123456789012345678901234567890"); - printf("%*.*x|\n", 10, 21, -10); - ft_printf("%*.*x|\n", 10, 21, -10); -//x 392 + a = -2; + while(a < 5) //T70-177 + { + b = -2; + while (b < 5) + { + printf("%*.*i, %*.*d, %*.*d, %*.*d, %*.*d, %*.*d, %*.*d, %*.*d|\n", a, b, i, a, b, j, a, b, k, a, b, l, a, b, m, a, b, c, a, b, e, a, b, d); + ft_printf("%*.*i, %*.*d, %*.*d, %*.*d, %*.*d, %*.*d, %*.*d, %*.*d|\n\n", a, b, i, a, b, j, a, b, k, a, b, l, a, b, m, a, b, c, a, b, e, a, b, d); +*/ /* PRINT(" --- Return : %d\n", PRINT("%-*.*i, %-*.*d, %-*.*d, %-*.*d, %-*.*d, %-*.*d, %-*.*d, %-*.*d", a, b, i, a, b, j, a, b, k, a, b, l, a, b, m, a, b, c, a, b, e, a, b, d)); + PRINT(" --- Return : %d\n", PRINT("%0*.*i, %0*.*d, %0*.*d, %0*.*d, %0*.*d, %0*.*d, %0*.*d, %0*.*d", a, b, i, a, b, j, a, b, k, a, b, l, a, b, m, a, b, c, a, b, e, a, b, d)); +*/ /* b++; + } + a++; + } +*/ + return 0; } diff --git a/srcs/ft_fetch.c b/srcs/ft_fetch.c new file mode 100644 index 0000000..892f78e --- /dev/null +++ b/srcs/ft_fetch.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_fetch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: alsantia +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/04/25 19:37:57 by alsantia #+# #+# */ +/* Updated: 2021/04/25 19:40:13 by alsantia ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/ft_printf.h" + +void ft_fetch(const char *str, char *buf) +{ + str++; + while (!ft_isspecifier(*str) && *str) + *buf++ = *str++; + if (*str != '\0') + { + *buf++ = *str; + *buf = '\0'; + } +} diff --git a/srcs/ft_printf.c b/srcs/ft_printf.c index 00c5bab..4f01721 100644 --- a/srcs/ft_printf.c +++ b/srcs/ft_printf.c @@ -6,26 +6,16 @@ /* By: alsantia +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/03/13 17:01:24 by alsantia #+# #+# */ -/* Updated: 2021/04/25 15:38:36 by alsantia ### ########.fr */ +/* Updated: 2021/04/25 19:41:57 by alsantia ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/ft_printf.h" -/*static int analyse_str(const char *str, va_list ap) - if (*str == 'u') - ft_putstr(ft_putnbr_u(va_arg(ap, int), buffer)); - if (*str == 'x') - ft_putstr(ft_putnbr_h(va_arg(ap, int), buffer, 'x')); - if (*str == 'X') - ft_putstr(ft_putnbr_h(va_arg(ap, int), buffer, 'X')); -}*/ - -void update_flags(t_flags *flag, char *s) +static char *update_flags_2(t_flags *flag, char *s) { -//parte 1 da maq. de estados - while (*s == '#' || *s == '0' || *s == '-' || - *s == ' ' || *s == '+') + while (*s == '#' || *s == '0' || *s == '-' + || *s == ' ' || *s == '+') { if (*s == '#') flag->sharp = 1; @@ -39,7 +29,12 @@ void update_flags(t_flags *flag, char *s) flag->plus = 1; s++; } -//parte 2 da maq. de estados + return (s); +} + +static void update_flags(t_flags *flag, char *s) +{ + s = update_flags_2(flag, s); if (*s == '*') { flag->placehold_w = 1; @@ -47,12 +42,8 @@ void update_flags(t_flags *flag, char *s) } else while (ft_isdigit(*s)) - { - flag->width = flag->width * 10 + *s - '0'; - s++; - } -//parte 3 da maq. de estados - if(*s == '.') + flag->width = flag->width * 10 + *s++ - '0'; + if (*s == '.') { s++; flag->precision = 0; @@ -63,20 +54,14 @@ void update_flags(t_flags *flag, char *s) } else while (ft_isdigit(*s)) - { - flag->precision = flag->precision * 10 + *s - '0'; - s++; - } + flag->precision = flag->precision * 10 + *s++ - '0'; } -//parte 4 da maq. de estados para bonus COMPLETAR -//estamos no type if (ft_isspecifier(*s)) flag->type = *s; } -void init_flags(t_flags *flag) +static void init_flags(t_flags *flag) { - //falta inicializar o resto flag->sharp = 0; flag->zero = 0; flag->space = 0; @@ -88,27 +73,9 @@ void init_flags(t_flags *flag) flag->placehold_p = 0; } -void fetch(const char *str, char *buf) -{ - str++; - while (!ft_isspecifier(*str) && *str) - *buf++ = *str++; - //*str tem specifier OU '/0' //tratar - if (*str != '\0') - { - *buf++ = *str; - *buf = '\0'; - } -} -void fill(size_t n, char c) -{ - while (n--) - ft_putchar(c); -} - -size_t treat_types(t_flags *flag, va_list ap, char *buf) +static size_t treat_types(t_flags *flag, va_list ap, char *buf) { - size_t count; + size_t count; count = 0; if (flag->type == 'c') @@ -124,40 +91,37 @@ size_t treat_types(t_flags *flag, va_list ap, char *buf) if (flag->type == '%') count = ft_treat_type_perc(flag); if (flag->type == 'x') - count = ft_treat_type_x(flag, ap, buf); + count = ft_treat_type_x(flag, ap, buf, 'x'); + if (flag->type == 'X') + count = ft_treat_type_x(flag, ap, buf, 'X'); return (count); } int ft_printf(const char *str, ...) { va_list ap; - t_flags flag; - size_t count; - char *buffer; + t_flags flag; + size_t count; + char *buffer; count = 0; - buffer = malloc(20);//rever 20? - if (!buffer) - return (-1); + buffer = malloc(20); va_start(ap, str); while (*str) { if (*str != '%') + count += ft_putchar(*str); + else { - ft_putchar(*str); - count++; + ft_fetch(str, buffer); + str += ft_strlen(buffer); + init_flags(&flag); + update_flags(&flag, buffer); + count += treat_types(&flag, ap, buffer); } - else - { - fetch(str, buffer); - str += ft_strlen(buffer); - init_flags(&flag); - update_flags(&flag, buffer); - count += treat_types(&flag, ap, buffer); - } str++; } va_end(ap); free(buffer); - return (count);//count + return (count); } diff --git a/srcs/ft_putnbr_h.c b/srcs/ft_putnbr_h.c index d999b85..e0715d0 100644 --- a/srcs/ft_putnbr_h.c +++ b/srcs/ft_putnbr_h.c @@ -6,7 +6,7 @@ /* By: alsantia +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/04/15 18:08:10 by alsantia #+# #+# */ -/* Updated: 2021/04/25 17:00:12 by alsantia ### ########.fr */ +/* Updated: 2021/04/25 17:26:08 by alsantia ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,6 @@ static char to_upper_h(char c) } void ft_putnbr_h(unsigned long n, char *res, char c, int is_pointer) -//void ft_putnbr_h(unsigned int n, char *res, char c) { char *hex; char *p; diff --git a/srcs/ft_treat_type_x.c b/srcs/ft_treat_type_x.c index d1edf77..f8f9ea5 100644 --- a/srcs/ft_treat_type_x.c +++ b/srcs/ft_treat_type_x.c @@ -6,7 +6,7 @@ /* By: alsantia +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/04/25 15:46:54 by alsantia #+# #+# */ -/* Updated: 2021/04/25 17:00:11 by alsantia ### ########.fr */ +/* Updated: 2021/04/25 17:26:07 by alsantia ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,14 +57,13 @@ static void ft_check_flag(t_flags *flag, va_list ap) flag->zero = 0; } -size_t ft_treat_type_x(t_flags *flag, va_list ap, char *buf) +size_t ft_treat_type_x(t_flags *flag, va_list ap, char *buf, char ch) { int count; count = 0; ft_check_flag(flag, ap); - //ft_putnbr_h(va_arg(ap, unsigned long), buf, 'x');//HERE - ft_putnbr_h(va_arg(ap, int), buf, 'x', 0); + ft_putnbr_h(va_arg(ap, int), buf, ch, 0); if (*buf == '0' && flag->precision == 0) return (ft_pad(flag, ft_strlen(buf) - 1)); if (!flag->minus && !flag->zero)