Skip to content

Commit

Permalink
mandatory working and norm ok
Browse files Browse the repository at this point in the history
  • Loading branch information
alsantia@c2r5s1.42lisboa.com committed Apr 25, 2021
1 parent 2ba2572 commit 21c4bfd
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 80 deletions.
6 changes: 3 additions & 3 deletions includes/ft_printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: alsantia <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
46 changes: 42 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
25 changes: 25 additions & 0 deletions srcs/ft_fetch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fetch.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alsantia <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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';
}
}
98 changes: 31 additions & 67 deletions srcs/ft_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,16 @@
/* By: alsantia <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
Expand All @@ -39,20 +29,21 @@ 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;
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;
Expand All @@ -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;
Expand All @@ -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')
Expand All @@ -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);
}
3 changes: 1 addition & 2 deletions srcs/ft_putnbr_h.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: alsantia <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions srcs/ft_treat_type_x.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: alsantia <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 21c4bfd

Please sign in to comment.