From a287db1124beda38507739f892c085bd3654ebd7 Mon Sep 17 00:00:00 2001 From: Rudy Bousset Date: Fri, 17 Jan 2020 19:34:53 +0100 Subject: Added libft --- libft/src/ft_sprintf.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 libft/src/ft_sprintf.c (limited to 'libft/src/ft_sprintf.c') diff --git a/libft/src/ft_sprintf.c b/libft/src/ft_sprintf.c new file mode 100644 index 0000000..0cf5dc2 --- /dev/null +++ b/libft/src/ft_sprintf.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_sprintf.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2020/01/17 15:09:24 by rbousset #+# ## ## #+# */ +/* Updated: 2020/01/17 15:09:26 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include + +static int + ft_printf_return(char *str, t_printflist *pflist) +{ + int ret; + + ft_strlcpy(str, pflist->output, pflist->fulllen + 1); + ret = pflist->fulllen; + ft_memdel(pflist->output); + ft_memdel(pflist->fullflag); + ft_memdel(pflist); + return (ret); +} + +int + ft_sprintf(char *str, const char *format, ...) +{ + t_printflist *pflist; + va_list arg; + int pos; + + if (!format) + return (-1); + if ((pos = ft_strlchr(format, '%')) < 0) + return (ft_strlcpy(str, format, ft_strlen(format) + 1)); + else + { + pflist = ft_printf_init_struct(format); + ft_printf_putpart(format, 0, pos, pflist); + va_start(arg, format); + while (pos >= 0) + { + pos = ft_printf_flags(format, pos, arg, pflist); + ft_printf_reinit_struct(pflist); + } + va_end(arg); + return (ft_printf_return(str, pflist)); + } + return (0); +} -- cgit v1.2.3