aboutsummaryrefslogtreecommitdiffstats
path: root/libft/src/ft_printf_put_precision.c
diff options
context:
space:
mode:
Diffstat (limited to 'libft/src/ft_printf_put_precision.c')
-rw-r--r--libft/src/ft_printf_put_precision.c67
1 files changed, 67 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..7898102
--- /dev/null
+++ b/libft/src/ft_printf_put_precision.c
@@ -0,0 +1,67 @@
+/* ************************************************************************** */
+/* LE - / */
+/* / */
+/* ft_printf_put_precision.c .:: .:/ . .:: */
+/* +:+:+ +: +: +:+:+ */
+/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */
+/* #+# #+ #+ #+# */
+/* Created: 2019/12/31 14:40:49 by rbousset #+# ## ## #+# */
+/* Updated: 2019/12/31 14:40:50 by rbousset ### #+. /#+ ###.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(str);
+ }
+ return (0);
+}