aboutsummaryrefslogtreecommitdiff
path: root/printer.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-29 01:57:41 +0100
committerCharles <sircharlesaze@gmail.com>2019-10-29 02:02:09 +0100
commit87bce91050b56915dcf5964f6f66d5f47299e7f3 (patch)
tree8c8fef15ffb962cfa860181ffad3fdd52c71b4f0 /printer.c
parentf6ee1462e26d723cf5d53157eadaff2804d18c3a (diff)
downloadft_printf-87bce91050b56915dcf5964f6f66d5f47299e7f3.tar.gz
ft_printf-87bce91050b56915dcf5964f6f66d5f47299e7f3.tar.bz2
ft_printf-87bce91050b56915dcf5964f6f66d5f47299e7f3.zip
Reworking, binary flags, extract advance
- Binary flags attribute instead of multiple bool - Extract doesnst strdup fmt each time, just advance the current pointer - In parsing push front then reverse - Moved some functions to libf
Diffstat (limited to 'printer.c')
-rw-r--r--printer.c69
1 files changed, 42 insertions, 27 deletions
diff --git a/printer.c b/printer.c
index 8884c00..2bc0116 100644
--- a/printer.c
+++ b/printer.c
@@ -1,3 +1,15 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* printer.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/28 23:19:24 by cacharle #+# #+# */
+/* Updated: 2019/10/29 00:13:53 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
@@ -9,21 +21,25 @@
char *convert_to_str(t_pformat *pformat, va_list ap)
{
- char *str;
- t_conversion conversion = pformat->conversion;
+ char *str;
+ t_conversion conversion;
+ conversion = pformat->conversion;
str = NULL;
- if (pformat->min_width.wildcard)
+ if (pformat->flags & FLAG_MIN_WIDTH_WILDCARD)
{
- pformat->min_width.value = va_arg(ap, int);
- if (pformat->min_width.value < 0)
+ if (pformat->flags & FLAG_MIN_WIDTH_OVERWRITE)
+ (void)va_arg(ap, int);
+ else
+ pformat->min_width = va_arg(ap, int);
+ if (pformat->min_width < 0)
{
- pformat->left_adjusted = TRUE;
- pformat->min_width.value *= -1;
+ pformat->flags |= FLAG_LEFT_ADJUSTED;
+ pformat->min_width *= -1;
}
}
- if (pformat->precision.wildcard)
- pformat->precision.value = va_arg(ap, int);
+ if (pformat->flags & FLAG_PRECISION_WILDCARD)
+ pformat->precision = va_arg(ap, int);
if (conversion == CONVERSION_CHAR)
{
if ((str = ft_strnew(2)) == NULL)
@@ -66,16 +82,17 @@ void handle_padding(t_pformat *pformat, char *str)
int len;
int i;
- if (pformat->min_width.value == -1)
+ if (pformat->min_width == -1)
return;
- if ((len = ft_strlen(str)) >= pformat->min_width.value)
+ if ((len = ft_strlen(str)) >= pformat->min_width)
return;
- tmp = (char*)malloc(sizeof(char) * (pformat->min_width.value + 1));
- if (!pformat->left_adjusted)
+ //protect this
+ tmp = (char*)malloc(sizeof(char) * (pformat->min_width + 1));
+ if (!(pformat->flags & FLAG_LEFT_ADJUSTED))
{
i = 0;
- while (i <= pformat->min_width.value - len)
- tmp[i++] = pformat->zero_padding ? '0' : ' ';
+ while (i <= pformat->min_width - len)
+ tmp[i++] = pformat->flags & FLAG_ZERO_PADDING ? '0' : ' ';
ft_strcpy(tmp + i - 1, str);
ft_strcpy(str, tmp);
}
@@ -83,39 +100,37 @@ void handle_padding(t_pformat *pformat, char *str)
{
ft_strcpy(tmp, str);
i = len;
- while (i < pformat->min_width.value)
+ while (i < pformat->min_width)
tmp[i++] = ' ';
ft_strcpy(str, tmp);
}
free(tmp);
}
-/* #include <stdio.h> */
void handle_precision(t_pformat *pformat, char *str)
{
int len;
char *tmp;
t_conversion conv;
- /* printf("\n%d\n", pformat->precision.value); */
- if (pformat->precision.value == -1)
+ if (pformat->precision == -1)
return ;
len = ft_strlen(str);
conv = pformat->conversion;
if (conv == CONVERSION_STR)
- str[pformat->precision.value] = '\0';
+ str[pformat->precision] = '\0';
else if (conv == CONVERSION_DECIMAL || conv == CONVERSION_INT
|| conv == CONVERSION_UINT || conv == CONVERSION_HEX_LOWER
|| conv == CONVERSION_HEX_UPPER)
{
- if (len >= pformat->precision.value)
+ if (len >= pformat->precision)
return ;
- tmp = (char*)malloc(sizeof(char) * (pformat->precision.value + 1));
- ft_strcpy(tmp + pformat->precision.value - len, str);
- /* printf("\n>%s< %d %d\n", str, len, pformat->precision); */
- pformat->precision.value -= len;
- while (pformat->precision.value-- > 0)
- tmp[pformat->precision.value] = '0';
+ // protect this
+ tmp = (char*)malloc(sizeof(char) * (pformat->precision + 1));
+ ft_strcpy(tmp + pformat->precision - len, str);
+ pformat->precision -= len;
+ while (pformat->precision-- > 0)
+ tmp[pformat->precision] = '0';
ft_strcpy(str, tmp);
free(tmp);
}