Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ SRCS = ft_get_/get_next_line.c \
ft_mem_/ft_memdup.c \
ft_mem_/ft_realloc.c \
ft_pri_/ft_printf.c \
ft_pri_/ft_printf_array.c \
ft_pri_/ft_printf_display.c\
ft_pri_/ft_printf_fill.c\
ft_pri_/ft_printf_array_fill.c\
ft_pri_/ft_printf_fill_default.c\
ft_pri_/ft_printf_fill_empty.c\
ft_pri_/ft_printf_fill_d.c\
Expand Down Expand Up @@ -334,6 +336,12 @@ $(DIROBJ)ft_printf.o: srcs/ft_pri_/ft_printf.c incs/libftprintf.h incs/libft.h
@printf "compiling ./srcs/ft_pri_/ft_printf.c\n"
@$(CC) -c ./srcs/ft_pri_/ft_printf.c -o ./.objs/ft_printf.o $(CPPFLAGS) $(CFLAGS)

$(DIROBJ)ft_printf_array.o: srcs/ft_pri_/ft_printf_array.c incs/libftprintf.h \
incs/libft.h
@printf "$(C_GRE)[ libft.a ] [ %-6s ]$(C_DFL) " "clang"
@printf "compiling ./srcs/ft_pri_/ft_printf_array.c\n"
@$(CC) -c ./srcs/ft_pri_/ft_printf_array.c -o ./.objs/ft_printf_array.o $(CPPFLAGS) $(CFLAGS)

$(DIROBJ)ft_printf_display.o: srcs/ft_pri_/ft_printf_display.c incs/libftprintf.h \
incs/libft.h
@printf "$(C_GRE)[ libft.a ] [ %-6s ]$(C_DFL) " "clang"
Expand All @@ -346,6 +354,12 @@ $(DIROBJ)ft_printf_fill.o: srcs/ft_pri_/ft_printf_fill.c incs/libftprintf.h \
@printf "compiling ./srcs/ft_pri_/ft_printf_fill.c\n"
@$(CC) -c ./srcs/ft_pri_/ft_printf_fill.c -o ./.objs/ft_printf_fill.o $(CPPFLAGS) $(CFLAGS)

$(DIROBJ)ft_printf_array_fill.o: srcs/ft_pri_/ft_printf_array_fill.c \
incs/libftprintf.h incs/libft.h
@printf "$(C_GRE)[ libft.a ] [ %-6s ]$(C_DFL) " "clang"
@printf "compiling ./srcs/ft_pri_/ft_printf_array_fill.c\n"
@$(CC) -c ./srcs/ft_pri_/ft_printf_array_fill.c -o ./.objs/ft_printf_array_fill.o $(CPPFLAGS) $(CFLAGS)

$(DIROBJ)ft_printf_fill_default.o: srcs/ft_pri_/ft_printf_fill_default.c \
incs/libftprintf.h incs/libft.h
@printf "$(C_GRE)[ libft.a ] [ %-6s ]$(C_DFL) " "clang"
Expand Down
17 changes: 17 additions & 0 deletions incs/libftprintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef struct s_printf_args
} t_printf_args;

int ft_printf(char const *format, ...);
int ft_printf_array(char const *format, void **array);
int ft_printf_params(t_printf_params **params);
char *ft_printf_empty_string(void);
void ft_printf_exit_malloc(void);
Expand All @@ -51,6 +52,8 @@ int ft_printf_return_free(t_printf_params *params,
t_printf_args *args, int ret);
int ft_printf_fill(t_printf_params **params,
t_printf_args *el, va_list ap);
int ft_printf_array_fill(t_printf_params **params,
t_printf_args *el, void *ap, int *index);
void ft_printf_conv_fmt(char const *fmt,
size_t *i,
size_t *j,
Expand All @@ -68,12 +71,26 @@ char *ft_printf_strsuffix(char *str,
char *ft_printf_fill_default(t_printf_args *el);
char *ft_printf_fill_empty(t_printf_args *el, char c);
char *ft_printf_fill_d(t_printf_args *el, va_list ap);
char *ft_printf_array_fill_d(t_printf_args *el, void *ap,
int *index);
char *ft_printf_fill_b(t_printf_args *el, va_list ap);
char *ft_printf_array_fill_b(t_printf_args *el, void *ap,
int *index);
char *ft_printf_fill_u(t_printf_args *el, va_list ap);
char *ft_printf_array_fill_u(t_printf_args *el, void *ap,
int *index);
char *ft_printf_fill_s(t_printf_args *el, va_list ap);
char *ft_printf_array_fill_s(t_printf_args *el, void *ap,
int *index);
char *ft_printf_fill_c(t_printf_args *el, va_list ap);
char *ft_printf_array_fill_c(t_printf_args *el, void *ap,
int *index);
char *ft_printf_fill_x(t_printf_args *el, va_list ap);
char *ft_printf_array_fill_x(t_printf_args *el, void *ap,
int *index);
char *ft_printf_fill_o(t_printf_args *el, va_list ap);
char *ft_printf_array_fill_o(t_printf_args *el, void *ap,
int *index);
char *ft_printf_fill_p(t_printf_args *el,
unsigned long long n);
char *ft_printf_fill_s_long(t_printf_args *el,
Expand Down
89 changes: 89 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "libftprintf.h"

int is_conversion_symbol(char c)
{
if (c == '%' || c == 'p' || c == 'n' || c == 'a' || c == 'r' || c == 's' || c == 'S' || c == 'c' || c == 'C' || c == 'u' || c == 'U' || c == 'd' || c == 'i' || c == 'D' || c == 'b' || c == 'B' || c == 'x' || c == 'X' || c == 'o' || c == 'O')
return (1);
return (0);
}
int count_symbols(char const *format)
{
int i;
int cnt;

cnt = 0;
i = 0;
while (format[i])
{
if (format[i] == '%')
{
i++;
while (format[i] && is_conversion_symbol(format[i]) == 0)
i++;
if (format[i])
cnt++;
else
break ;
}
i++;
}
return (cnt);
}

void my_logger(char const *format, ...)
{
void **array;
va_list pa;
int total;
int i;
uintmax_t val;

total = count_symbols(format);
array = (void **)malloc(sizeof(void *) * (total + 1));
array[0] = NULL;
i = 0;
va_start(pa, format);
while(i < total)
{
val = va_arg(pa, uintmax_t);
array[i++] = (void *)&val;
array[i] = NULL;
}
va_end(pa);
ft_printf_array(format, array);
}

int main(void)
{
int i;
void **array;
char character = '!';
char *str = "je suis là";
wchar_t *str2 = L"ζéЩ";
int numeric = 42;
int numeric2 = 298;
unsigned int unsigned_numeric = 4294967295;
long long numeric3 = 9223372036854775807;
char *ptr = str;

i = 0;
array = (void **)malloc(sizeof(void *) * 20);
array[i++] = (void *)&character;
array[i++] = (void *)&str;
array[i++] = (void *)&str2;
array[i++] = (void *)&numeric;
array[i++] = (void *)&numeric; //octal
array[i++] = (void *)&numeric; //hexa
array[i++] = (void *)&numeric; //binary representation
array[i++] = (void *)&numeric2;
array[i++] = (void *)&numeric3;
array[i++] = (void *)&unsigned_numeric;
array[i++] = (void *)&ptr;
array[i++] = NULL;

ft_printf_array("---------\n%c\n%s\n%S\n%d\n%o\n%x\n%b\n%hhd\n%lld\n%u\n%p\n", array);
my_logger("---------\n%c\n%s\n%S\n%d\n%o\n%x\n%b\n%hhd\n%lld\n%u\n%p\n", character, str, str2, numeric, numeric, numeric, numeric, numeric2, numeric3, unsigned_numeric, ptr);
ft_printf("---------\n%c\n%s\n%S\n%d\n%o\n%x\n%b\n%hhd\n%lld\n%u\n%p\n", character, str, str2, numeric, numeric, numeric, numeric, numeric2, numeric3, unsigned_numeric, ptr);

return (0);
}
24 changes: 24 additions & 0 deletions srcs/ft_pri_/ft_printf_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "libftprintf.h"

int ft_printf_array(char const *format, void **array)
{
t_printf_params *params;
t_printf_args *args;
t_printf_args *el;
int i;

args = NULL;
if (!array)
return (-1);
if (ft_printf_init(format, &args) == -1 || ft_printf_params(&params) == -1)
return (-1);
el = args;
i = 0;
while (el)
{
if (ft_printf_array_fill(&params, el, array[i], &i) != 0)
return (ft_printf_return_free(params, args, -1));
el = el->next;
}
return (ft_printf_display(params, args));
}
104 changes: 104 additions & 0 deletions srcs/ft_pri_/ft_printf_array_fill.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include <stdarg.h>
#include "libftprintf.h"

static int ft_printf_set_r(t_printf_params **params, int n, int *index)
{
(*index)++;
if (n == -1)
return (-1);
(*params)->fd = n;
(*params)->fd_dynamic = 1;
return (0);
}

static void ft_printf_set_a(t_printf_params **params,
t_printf_args *el, char **ptr, int *index)
{
(*index)++;
if ((*params)->fd_dynamic == 0)
(*params)->fd_dynamic = -1;
el->export_dynamic = ptr;
(*params)->export_dynamic = 1;
}

static void ft_printf_wildcard(t_printf_args *el, void *ap, int *index)
{
int value;

(*index)++;
if (el->wi_wc > 0)
{
value = (int)ap;
if (el->wi_wc == 1)
{
if (value < 0)
{
el->al = 1;
value = -value;
}
el->wi = (size_t)value;
}
}
if (el->acc_wc > 0)
{
value = (int)ap;
if (el->acc_wc == 1 && value >= 0)
el->acc = (size_t)value;
else
el->acc_d = 0;
}
}

static int ft_printf_fill2(t_printf_params **params,
t_printf_args *el, void *ap, int *index)
{
if (el->type == 'p')
{
(*index)++;
el->export = ft_printf_fill_p(el, *((unsigned long long *)ap));
}
else if (el->type == 'n')
el->n = (int *)ap;
else if (el->type == 'a')
ft_printf_set_a(params, el, (char **)ap, index);
else if (el->type == 'r')
{
if (ft_printf_set_r(params, (int)ap, index) == -1)
return (-1);
}
else if (el->type != ' ')
el->export = ft_printf_fill_empty(el, el->type);
if (el->export == NULL && el->type != 'n' && el->type != 'r'
&& el->type != 'a')
return (-1);
return (0);
}

int ft_printf_array_fill(t_printf_params **params,
t_printf_args *el, void *ap, int *index)
{
if (el->wi_wc > 0 || el->acc_wc > 0)
ft_printf_wildcard(el, ap, index);
if (el->type == '%')
el->export = ft_printf_fill_default(el);
else if (el->type == 's' || el->type == 'S')
el->export = ft_printf_array_fill_s(el, ap, index);
else if (el->type == 'c' || el->type == 'C')
el->export = ft_printf_array_fill_c(el, ap, index);
else if (el->type == 'u' || el->type == 'U')
el->export = ft_printf_array_fill_u(el, ap, index);
else if (el->type == 'd' || el->type == 'i' || el->type == 'D')
el->export = ft_printf_array_fill_d(el, ap, index);
else if (el->type == 'b' || el->type == 'B')
el->export = ft_printf_array_fill_b(el, ap, index);
else if (el->type == 'x' || el->type == 'X')
el->export = ft_printf_array_fill_x(el, ap, index);
else if (el->type == 'o' || el->type == 'O')
el->export = ft_printf_array_fill_o(el, ap, index);
else
return (ft_printf_fill2(params, el, ap, index));
if (el->export == NULL && el->type != 'n' && el->type != 'r'
&& el->type != 'a')
return (-1);
return (0);
}
23 changes: 23 additions & 0 deletions srcs/ft_pri_/ft_printf_fill_b.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,26 @@ char *ft_printf_fill_b(t_printf_args *el, va_list ap)
return (ft_printf_fill_b_intmax(el, va_arg(ap, ssize_t)));
return (ft_printf_fill_b_int(el, va_arg(ap, int)));
}

char *ft_printf_array_fill_b(t_printf_args *el, void *ap,
int *index)
{
(*index)++;
if (el->type == 'B')
el->si = 'l';
if (el->si == 'l')
return (ft_printf_fill_b_long(el, *((long *)ap)));
if (el->si == ' ')
return (ft_printf_fill_b_int(el, *((int *)ap)));
else if (el->si == 'H')
return (ft_printf_fill_b_char(el, (char)(*((int *)ap))));
else if (el->si == 'h')
return (ft_printf_fill_b_short(el, (short)(*((int *)ap))));
else if (el->si == 'L')
return (ft_printf_fill_b_llong(el, *((long long *)ap)));
else if (el->si == 'j')
return (ft_printf_fill_b_intmax(el, *((intmax_t *)ap)));
else if (el->si == 'z')
return (ft_printf_fill_b_intmax(el, *((ssize_t *)ap)));
return (ft_printf_fill_b_int(el, *((int *)ap)));
}
18 changes: 18 additions & 0 deletions srcs/ft_pri_/ft_printf_fill_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,21 @@ char *ft_printf_fill_c(t_printf_args *el, va_list ap)
str = ft_printf_fill_c_default(el, va_arg(ap, int));
return (str);
}

char *ft_printf_array_fill_c(t_printf_args *el, void *ap,
int *index)
{
char *str;

(*index)++;
str = NULL;
if (el->type == 'C' || el->si == 'l')
str = ft_printf_fill_c_long(el, (wchar_t)(*((wint_t *)ap)));
else if (el->si == 'H')
str = ft_printf_fill_c_default(el, (char)(*((int *)ap)));
else if (el->si == 'h')
str = ft_printf_fill_c_default(el, (short)(*((int *)ap)));
else
str = ft_printf_fill_c_default(el, *((int *)ap));
return (str);
}
23 changes: 23 additions & 0 deletions srcs/ft_pri_/ft_printf_fill_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,26 @@ char *ft_printf_fill_d(t_printf_args *el, va_list ap)
return (ft_printf_fill_d_intmax(el, va_arg(ap, ssize_t)));
return (ft_printf_fill_d_int(el, va_arg(ap, int)));
}

char *ft_printf_array_fill_d(t_printf_args *el, void *ap,
int *index)
{
(*index)++;
if (el->type == 'D')
el->si = 'l';
if (el->si == 'l')
return (ft_printf_fill_d_long(el, *((long *)ap)));
else if (el->si == ' ')
return (ft_printf_fill_d_int(el, *((int *)ap)));
else if (el->si == 'H')
return (ft_printf_fill_d_short(el, (char)(*((int *)ap))));
else if (el->si == 'h')
return (ft_printf_fill_d_short(el, (short)(*((int *)ap))));
else if (el->si == 'L')
return (ft_printf_fill_d_llong(el, *((long long *)ap)));
else if (el->si == 'j')
return (ft_printf_fill_d_intmax(el, *((intmax_t *)ap)));
else if (el->si == 'z')
return (ft_printf_fill_d_intmax(el, *((ssize_t *)ap)));
return (ft_printf_fill_d_int(el, *((int *)ap)));
}
Loading