summaryrefslogtreecommitdiffstats
path: root/libft/src/ft_printf_put_precision.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-04-19 22:27:11 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-04-19 22:27:11 +0200
commit62afe606a355581c5b48cca361478c43fb6ae4cf (patch)
treea278fe2fdf44c296892616554743b9d380ff7afa /libft/src/ft_printf_put_precision.c
parentAdded joe-sh_history to gitignore (diff)
download42-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_precision.c')
-rw-r--r--libft/src/ft_printf_put_precision.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/libft/src/ft_printf_put_precision.c b/libft/src/ft_printf_put_precision.c
new file mode 100644
index 0000000..af7f7b7
--- /dev/null
+++ b/libft/src/ft_printf_put_precision.c
@@ -0,0 +1,66 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_printf_put_precision.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)
+{
+ if (pflist->conv == 'p' && pflist->precision > pflist->putlen)
+ return (pflist->precision - pflist->putlen + 2);
+ else if (pflist->precision > pflist->putlen && pflist->isneg == 0
+ && pflist->isaplus == 0)
+ return (pflist->precision - pflist->putlen);
+ else if (pflist->precision >= pflist->putlen && pflist->isneg == 1)
+ {
+ if (pflist->isaz == 1)
+ return (pflist->precision - pflist->putlen - 1);
+ else
+ return (pflist->precision - pflist->putlen);
+ }
+ else if (pflist->precision >= pflist->putlen && pflist->isneg == 0
+ && pflist->isaplus == 1)
+ {
+ if (pflist->isaz == 1)
+ return (pflist->precision - pflist->putlen);
+ else
+ return (pflist->precision - pflist->putlen + 1);
+ }
+ return (0);
+}
+
+/*
+** Puts the correct amount of 0's when
+** needed using pflist->precision.
+** Used for %p, %d, %i, %u, %x and %X
+*/
+
+int
+ ft_printf_put_precision(t_printflist *pflist)
+{
+ char *str;
+ int len;
+
+ len = 0;
+ if (pflist->precision >= pflist->putlen)
+ {
+ len = ft_printf_get_len(pflist);
+ if (!(str = (char*)malloc((len + 1) * sizeof(char))))
+ return (1);
+ str = ft_memset(str, '0', len);
+ *(str + len) = '\0';
+ ft_printf_cat_output(str, ft_strlen(str), pflist);
+ ft_memdel((void**)&str);
+ }
+ return (0);
+}