diff options
author | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-01-17 19:34:53 +0100 |
---|---|---|
committer | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-01-17 19:34:53 +0100 |
commit | a287db1124beda38507739f892c085bd3654ebd7 (patch) | |
tree | c439a25efe0309de08087d439a597f84583b257f /libft/src/ft_printf_put_width.c | |
parent | Removed libft (diff) | |
download | 42-cub3d-a287db1124beda38507739f892c085bd3654ebd7.tar.gz 42-cub3d-a287db1124beda38507739f892c085bd3654ebd7.tar.bz2 42-cub3d-a287db1124beda38507739f892c085bd3654ebd7.tar.xz 42-cub3d-a287db1124beda38507739f892c085bd3654ebd7.tar.zst 42-cub3d-a287db1124beda38507739f892c085bd3654ebd7.zip |
Added libft
Diffstat (limited to 'libft/src/ft_printf_put_width.c')
-rw-r--r-- | libft/src/ft_printf_put_width.c | 105 |
1 files changed, 105 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..30ae97f --- /dev/null +++ b/libft/src/ft_printf_put_width.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_put_width.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:56 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:56 by rbousset ### #+. /#+ ###.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(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(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(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(str); + } +} |