diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-19 22:27:11 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-19 22:27:11 +0200 |
commit | 62afe606a355581c5b48cca361478c43fb6ae4cf (patch) | |
tree | a278fe2fdf44c296892616554743b9d380ff7afa /libft/src/ft_printf_put_width.c | |
parent | Added joe-sh_history to gitignore (diff) | |
download | 42-minishell-62afe606a355581c5b48cca361478c43fb6ae4cf.tar.gz 42-minishell-62afe606a355581c5b48cca361478c43fb6ae4cf.tar.bz2 42-minishell-62afe606a355581c5b48cca361478c43fb6ae4cf.tar.xz 42-minishell-62afe606a355581c5b48cca361478c43fb6ae4cf.tar.zst 42-minishell-62afe606a355581c5b48cca361478c43fb6ae4cf.zip |
Well well well that wasn't too bad, now remake everything
Diffstat (limited to 'libft/src/ft_printf_put_width.c')
-rw-r--r-- | libft/src/ft_printf_put_width.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/libft/src/ft_printf_put_width.c b/libft/src/ft_printf_put_width.c new file mode 100644 index 0000000..e649a65 --- /dev/null +++ b/libft/src/ft_printf_put_width.c @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf_put_width.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:06:45 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:06:45 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +static int + ft_printf_get_len(t_printflist *pflist) +{ + int len; + int width; + int prec; + + width = pflist->width; + prec = pflist->precision; + if (pflist->conv == 'd' && prec > pflist->putlen && !pflist->isneg) + len = (width - pflist->putlen - (prec - pflist->putlen)); + else if (pflist->conv == 'd' && prec >= pflist->putlen && (pflist->isneg)) + len = (width - pflist->putlen - 1 - (prec - pflist->putlen)); + else if (pflist->conv == 'p' && prec > pflist->putlen) + len = (width - pflist->putlen - (prec - pflist->putlen + 2)); + else if (pflist->conv == 's' && prec > pflist->putlen && pflist->isaz == 1) + len = (width - pflist->putlen - (prec - pflist->putlen)); + else + len = (width - pflist->putlen); + if (pflist->conv == 'd' && pflist->isaspace == 1 && !pflist->isaplus + && (pflist->putlen < 3 && !pflist->isneg)) + len -= 1; + (len < 0) ? (len = 0) : 0; + return (len); +} + +/* +** Puts the (correct) amount of spaces +** when needed using pflist->width +*/ + +void + ft_printf_put_width_pre(t_printflist *pflist) +{ + char *str; + + if (pflist->conv == 'd' && pflist->isaspace == 1 && !pflist->isaplus + && (pflist->putlen < 3 && !pflist->isneg)) + ft_printf_cat_output(" ", 1, pflist); + if (pflist->width - pflist->putlen > 0 && pflist->width - pflist->putlen + - (pflist->precision - pflist->putlen) >= 0 && !pflist->isreverse) + { + if (!(str = malloc((ft_printf_get_len(pflist) + 1) * sizeof(char)))) + return ; + str = ft_memset(str, ' ', ft_printf_get_len(pflist)); + *(str + ft_printf_get_len(pflist)) = '\0'; + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel((void**)&str); + } + else if (pflist->conv == 's' && pflist->width - pflist->putlen > 0 + && !pflist->isreverse) + { + if (!(str = malloc((ft_printf_get_len(pflist) + 1) * sizeof(char)))) + return ; + str = ft_memset(str, ' ', ft_printf_get_len(pflist)); + *(str + ft_printf_get_len(pflist)) = '\0'; + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel((void**)&str); + } +} + +void + ft_printf_put_width_post(t_printflist *pflist) +{ + char *str; + + if (pflist->width - pflist->putlen > 0 && pflist->width - pflist->putlen + - (pflist->precision - pflist->putlen) >= 0 && pflist->isreverse) + { + if (!(str = (char*)malloc((ft_printf_get_len(pflist) + 1) + * sizeof(char)))) + return ; + str = ft_memset(str, ' ', ft_printf_get_len(pflist)); + *(str + ft_printf_get_len(pflist)) = '\0'; + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel((void**)&str); + } + else if (pflist->conv == 's' && pflist->width - pflist->putlen > 0 + && pflist->isreverse) + { + if (!(str = (char*)malloc((ft_printf_get_len(pflist) + 1) + * sizeof(char)))) + return ; + str = ft_memset(str, ' ', ft_printf_get_len(pflist)); + *(str + ft_printf_get_len(pflist)) = '\0'; + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel((void**)&str); + } +} |