|
@ -6,7 +6,7 @@ |
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|
|
/* +#+#+#+#+#+ +#+ */ |
|
|
/* +#+#+#+#+#+ +#+ */ |
|
|
/* Created: 2021/10/27 08:09:49 by narnaud #+# #+# */ |
|
|
/* Created: 2021/10/27 08:09:49 by narnaud #+# #+# */ |
|
|
/* Updated: 2021/11/17 09:40:16 by narnaud ### ########.fr */ |
|
|
/* Updated: 2023/04/13 12:20:28 by narnaud ### ########.fr */ |
|
|
/* */ |
|
|
/* */ |
|
|
/* ************************************************************************** */ |
|
|
/* ************************************************************************** */ |
|
|
|
|
|
|
|
@ -49,14 +49,14 @@ const char *handle_options(va_list args, const char *str, t_opts *opts) |
|
|
return (++str); |
|
|
return (++str); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void handle_functions(va_list args, const char *str, t_opts *opts) |
|
|
void handle_functions(va_list args, const char *str, t_opts *opts) |
|
|
{ |
|
|
{ |
|
|
size_t n; |
|
|
size_t n; |
|
|
const char *g_types = {"cspdiuxX%"}; |
|
|
const char *g_types = {"cspdiuxX%"}; |
|
|
static void(*g_printf_fcts[9])(va_list arg, const char *str, |
|
|
static void (*g_printf_fcts[9])(va_list arg, t_opts *opts) |
|
|
t_opts *opts) = {va_print_char, va_print_str, va_print_ptr, |
|
|
= {va_print_char, va_print_str, va_print_ptr, |
|
|
va_print_nbr, va_print_nbr, va_print_unsign, va_print_x, |
|
|
va_print_nbr, va_print_nbr, va_print_unsign, va_print_x, va_print_x_cap, |
|
|
va_print_x_cap, va_print_perc}; |
|
|
va_print_perc}; |
|
|
|
|
|
|
|
|
n = 0; |
|
|
n = 0; |
|
|
while (g_types[n] && g_types[n] != *str && ++n) |
|
|
while (g_types[n] && g_types[n] != *str && ++n) |
|
@ -74,10 +74,10 @@ void handle_functions(va_list args, const char *str, t_opts *opts) |
|
|
} |
|
|
} |
|
|
if (opts->precision < 0) |
|
|
if (opts->precision < 0) |
|
|
opts->dot = 0; |
|
|
opts->dot = 0; |
|
|
(g_printf_fcts[n])(args, str, opts); |
|
|
(g_printf_fcts[n])(args, opts); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void ft_format(const char *format, va_list args) |
|
|
void ft_format(const char *format, va_list args) |
|
|
{ |
|
|
{ |
|
|
t_opts opts; |
|
|
t_opts opts; |
|
|
|
|
|
|
|
@ -88,74 +88,72 @@ void ft_format(const char *format, va_list args) |
|
|
opts = (t_opts){0}; |
|
|
opts = (t_opts){0}; |
|
|
while (*format && !ft_strchr("cspdiuxX%", *format)) |
|
|
while (*format && !ft_strchr("cspdiuxX%", *format)) |
|
|
format = handle_options(args, format, &opts); |
|
|
format = handle_options(args, format, &opts); |
|
|
handle_functions(args, format, &opts); |
|
|
handle_functions(args, format, &opts); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
add_string(ft_strnew(1, *format), 1); |
|
|
add_string(ft_strnew(1, *format), 1); |
|
|
format++; |
|
|
format++; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int ft_printf(const char *format, ...) |
|
|
int ft_printf(const char *format, ...) |
|
|
{ |
|
|
{ |
|
|
int ret; |
|
|
int ret; |
|
|
t_dlist *strings; |
|
|
t_dlist *strings; |
|
|
va_list args; |
|
|
va_list args; |
|
|
|
|
|
|
|
|
va_start(args, format); |
|
|
va_start(args, format); |
|
|
ft_format(format, args); |
|
|
ft_format(format, args); |
|
|
ret = 0; |
|
|
ret = 0; |
|
|
strings = get_first(); |
|
|
strings = get_first(); |
|
|
while (strings) |
|
|
while (strings) |
|
|
{ |
|
|
{ |
|
|
ret += strings->size; |
|
|
ret += strings->size; |
|
|
ft_putnstr_fd(strings->size, strings->content, 1); |
|
|
ft_putnstr_fd(strings->size, strings->content, 1); |
|
|
strings = strings->next; |
|
|
strings = strings->next; |
|
|
} |
|
|
} |
|
|
clean_list(); |
|
|
clean_list(); |
|
|
va_end(args); |
|
|
va_end(args); |
|
|
return (ret); |
|
|
return (ret); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int ft_fprintf(FILE *file, const char *format, ...) |
|
|
int ft_fprintf(FILE *file, const char *format, ...) |
|
|
{ |
|
|
{ |
|
|
int ret; |
|
|
int ret; |
|
|
t_dlist *strings; |
|
|
t_dlist *strings; |
|
|
va_list args; |
|
|
va_list args; |
|
|
|
|
|
|
|
|
va_start(args, format); |
|
|
va_start(args, format); |
|
|
ft_format(format, args); |
|
|
ft_format(format, args); |
|
|
ret = 0; |
|
|
ret = 0; |
|
|
strings = get_first(); |
|
|
strings = get_first(); |
|
|
while (strings) |
|
|
while (strings) |
|
|
{ |
|
|
{ |
|
|
ret += strings->size; |
|
|
ret += strings->size; |
|
|
fwrite(strings->content, sizeof(char), strings->size, file); |
|
|
fwrite(strings->content, sizeof(char), strings->size, file); |
|
|
strings = strings->next; |
|
|
strings = strings->next; |
|
|
} |
|
|
} |
|
|
clean_list(); |
|
|
clean_list(); |
|
|
va_end(args); |
|
|
va_end(args); |
|
|
return (ret); |
|
|
return (ret); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int ft_sprintf(char *str, const char *format, ...) |
|
|
int ft_sprintf(char *str, const char *format, ...) |
|
|
{ |
|
|
{ |
|
|
int ret; |
|
|
int ret; |
|
|
t_dlist *strings; |
|
|
t_dlist *strings; |
|
|
va_list args; |
|
|
va_list args; |
|
|
|
|
|
|
|
|
va_start(args, format); |
|
|
va_start(args, format); |
|
|
ft_format(format, args); |
|
|
ft_format(format, args); |
|
|
ret = 0; |
|
|
ret = 0; |
|
|
strings = get_first(); |
|
|
strings = get_first(); |
|
|
while (strings) |
|
|
while (strings) |
|
|
{ |
|
|
{ |
|
|
ret += strings->size; |
|
|
ret += strings->size; |
|
|
ft_strlcat(str, strings->content, ret); |
|
|
ft_strlcat(str, strings->content, ret); |
|
|
strings = strings->next; |
|
|
strings = strings->next; |
|
|
} |
|
|
} |
|
|
clean_list(); |
|
|
clean_list(); |
|
|
va_end(args); |
|
|
va_end(args); |
|
|
return (ret); |
|
|
return (ret); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|