aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-15 13:57:21 +0200
committerCharles <sircharlesaze@gmail.com>2019-10-15 13:57:21 +0200
commit1b4df01bfa793fe91a58192a4b79917909bf1614 (patch)
tree4ef767bb5c04279743e97231b518ecca02b19a87
parentfa4bf89263e897695dbf48061369a23d695fef8b (diff)
downloadft_printf-1b4df01bfa793fe91a58192a4b79917909bf1614.tar.gz
ft_printf-1b4df01bfa793fe91a58192a4b79917909bf1614.tar.bz2
ft_printf-1b4df01bfa793fe91a58192a4b79917909bf1614.zip
Wildcard handling
Back where I was before rewrite
-rw-r--r--Makefile4
-rw-r--r--extract.c59
-rw-r--r--ft_printf.c22
-rw-r--r--header.h2
-rw-r--r--parse.c2
-rw-r--r--printer.c55
6 files changed, 73 insertions, 71 deletions
diff --git a/Makefile b/Makefile
index d0b3f82..7011d89 100644
--- a/Makefile
+++ b/Makefile
@@ -26,3 +26,7 @@ fclean: clean
$(RM) $(OBJ)
re: fclean all
+
+test: fclean
+ make -C ../schooltest
+ ../schooltest/all_tests
diff --git a/extract.c b/extract.c
index 27f1078..0ca4aed 100644
--- a/extract.c
+++ b/extract.c
@@ -50,24 +50,27 @@ char *extract_min_width(t_pformat *pformat, char *fmt)
i = 0;
- /* if (*fmt == '*') */
- /* { */
- /* pformat->min_width.wildcard.exist = TRUE; */
- /* i++; */
- /* } */
- if (ft_isdigit(fmt[i]))
+ if (*fmt == '*')
{
- tmp = ft_atoi(&fmt[i]);
- while (ft_isdigit(fmt[i]))
- i++;
- /* printf("%d\n", tmp); */
- /* if (fmt[i] == '$') */
- /* pformat->min_width.wildcard.ap_index = tmp; */
- /* else */
- /* { */
- pformat->min_width.hardcoded = tmp;
- pformat->min_width.wildcard.exist = FALSE;
- /* } */
+ pformat->min_width.wildcard.exist = TRUE;
+ i++;
+ }
+ else
+ {
+ if (ft_isdigit(fmt[i]))
+ {
+ tmp = ft_atoi(&fmt[i]);
+ while (ft_isdigit(fmt[i]))
+ i++;
+ /* printf("%d\n", tmp); */
+ if (fmt[i] == '$')
+ pformat->min_width.wildcard.ap_index = tmp;
+ else
+ {
+ pformat->min_width.value = tmp;
+ pformat->min_width.wildcard.exist = FALSE;
+ }
+ }
}
fmt_dup = ft_strdup(&fmt[i]);
free(fmt);
@@ -83,20 +86,20 @@ char *extract_precision(t_pformat *pformat, char *fmt)
if (*fmt != '.')
return (fmt);
i = 1;
- /* if (fmt[i] == '*') */
- /* { */
- /* pformat->precision.wildcard.exist = TRUE; */
- /* i++; */
- /* } */
- /* else if (!ft_isdigit(fmt[i])) */
- /* pformat->precision.hardcoded = 0; */
+ if (fmt[i] == '*')
+ {
+ pformat->precision.wildcard.exist = TRUE;
+ i++;
+ }
+ else if (!ft_isdigit(fmt[i]))
+ pformat->precision.value = 0;
tmp = ft_atoi(&fmt[i]);
while (ft_isdigit(fmt[i]))
i++;
- /* if (pformat->precision.wildcard.exist && fmt[i] == '$') */
- /* pformat->precision.wildcard.ap_index = tmp; */
- /* else */
- pformat->precision.hardcoded = tmp;
+ if (pformat->precision.wildcard.exist && fmt[i] == '$')
+ pformat->precision.wildcard.ap_index = tmp;
+ else
+ pformat->precision.value = tmp;
fmt_dup = ft_strdup(&fmt[i]);
free(fmt);
return (fmt);
diff --git a/ft_printf.c b/ft_printf.c
index 3ae03c0..96641b1 100644
--- a/ft_printf.c
+++ b/ft_printf.c
@@ -63,18 +63,18 @@ int main()
/* ft_printf("hex upper: %X\n", 0xabcf012); */
/* ft_printf("percent: %%\n"); */
- ft_printf("precision |%.9d|\n", 43);
- ft_printf("string precision |%.9s|\n", "jesuisbonjourbonjour");
- ft_printf("min width |%9d|\n", 43);
- ft_printf("zero padding |%09d|\n", 43);
- ft_printf("left adjusted |%-9d|\n", 43);
- ft_printf("string padding |%9s|\n", "bon");
+ /* ft_printf("precision |%.9d|\n", 43); */
+ /* ft_printf("string precision |%.9s|\n", "jesuisbonjourbonjour"); */
+ /* ft_printf("min width |%9d|\n", 43); */
+ /* ft_printf("zero padding |%09d|\n", 43); */
+ /* ft_printf("left adjusted |%-9d|\n", 43); */
+ /* ft_printf("string padding |%9s|\n", "bon"); */
+
+ ft_printf("width wildcard |%*d|\n", 5, 43);
+ ft_printf("precision wildcard |%.*d|\n", 5, 43);
+ ft_printf("precision/width wildcard |%*.*d|\n", 5, 3, 43);
+ ft_printf("left adjusted |%*d|\n", -5, 43);
- /* ft_printf("width wildcard |%*d|\n", 5, 43); */
- /* ft_printf("precision wildcard |%.*d|\n", 5, 43); */
- /* ft_printf("precision/width wildcard |%*.*d|\n", 5, 3, 43); */
- /* ft_printf("left adjusted |%*d|\n", -5, 43); */
- /* */
/* ft_printf("overwrite |%*3d|\n", 5, 43); */
/* ft_printf("overwrite |%*-1d|\n", 0, 43); */
return 0;
diff --git a/header.h b/header.h
index 11b73d9..ed3d248 100644
--- a/header.h
+++ b/header.h
@@ -30,7 +30,7 @@ typedef int t_bool;
typedef struct
{
- int hardcoded;
+ int value;
struct
{
t_bool exist;
diff --git a/parse.c b/parse.c
index b07b436..0fa82ed 100644
--- a/parse.c
+++ b/parse.c
@@ -55,7 +55,7 @@ t_pformat *parse_reduced(char *fmt)
pformat->zero_padding = FALSE;
pformat->min_width.wildcard.exist = FALSE;
pformat->precision.wildcard.exist = FALSE;
- pformat->precision.hardcoded = -1;
+ pformat->precision.value = -1;
pformat->len = ft_strlen(fmt);
pformat->conversion = strrchr_index(CONVERSIONS_STR, fmt[pformat->len - 1]);
fmt[pformat->len - 1] = 0;
diff --git a/printer.c b/printer.c
index 7863c8f..22d0dcd 100644
--- a/printer.c
+++ b/printer.c
@@ -19,22 +19,17 @@ char *convert_to_str(t_pformat *pformat, va_list ap)
t_conversion conversion = pformat->conversion;
str = NULL;
- /* if (pformat->min_width.wildcard.exist) */
- /* { */
- /* if (pformat->min_width.hardcoded != -1) */
- /* va_arg(ap, int); */
- /* else */
- /* { */
- /* pformat->min_width.hardcoded = va_arg(ap, int); */
- /* if (pformat->min_width.hardcoded < 0) */
- /* { */
- /* pformat->left_adjusted = TRUE; */
- /* pformat->min_width.hardcoded *= -1; */
- /* } */
- /* } */
- /* } */
- /* if (pformat->precision.wildcard.exist) */
- /* pformat->precision.hardcoded = va_arg(ap, int); */
+ if (pformat->min_width.wildcard.exist)
+ {
+ pformat->min_width.value = va_arg(ap, int);
+ if (pformat->min_width.value < 0)
+ {
+ pformat->left_adjusted = TRUE;
+ pformat->min_width.value *= -1;
+ }
+ }
+ if (pformat->precision.wildcard.exist)
+ pformat->precision.value = va_arg(ap, int);
if (conversion == CONVERSION_CHAR)
{
if ((str = ft_strnew(2)) == NULL)
@@ -77,15 +72,15 @@ void handle_padding(t_pformat *pformat, char *str)
int len;
int i;
- if (pformat->min_width.hardcoded == -1)
+ if (pformat->min_width.value == -1)
return;
- if ((len = ft_strlen(str)) >= pformat->min_width.hardcoded)
+ if ((len = ft_strlen(str)) >= pformat->min_width.value)
return;
- tmp = (char*)malloc(sizeof(char) * (pformat->min_width.hardcoded + 1));
+ tmp = (char*)malloc(sizeof(char) * (pformat->min_width.value + 1));
if (!pformat->left_adjusted)
{
i = 0;
- while (i <= pformat->min_width.hardcoded - len)
+ while (i <= pformat->min_width.value - len)
tmp[i++] = pformat->zero_padding ? '0' : ' ';
ft_strcpy(tmp + i - 1, str);
ft_strcpy(str, tmp);
@@ -94,7 +89,7 @@ void handle_padding(t_pformat *pformat, char *str)
{
ft_strcpy(tmp, str);
i = len;
- while (i < pformat->min_width.hardcoded)
+ while (i < pformat->min_width.value)
tmp[i++] = ' ';
ft_strcpy(str, tmp);
}
@@ -108,25 +103,25 @@ void handle_precision(t_pformat *pformat, char *str)
char *tmp;
t_conversion conv;
- /* printf("\n%d\n", pformat->precision.hardcoded); */
- if (pformat->precision.hardcoded == -1)
+ /* printf("\n%d\n", pformat->precision.value); */
+ if (pformat->precision.value == -1)
return ;
len = ft_strlen(str);
conv = pformat->conversion;
if (conv == CONVERSION_STR)
- str[pformat->precision.hardcoded] = '\0';
+ str[pformat->precision.value] = '\0';
else if (conv == CONVERSION_DECIMAL || conv == CONVERSION_INT
|| conv == CONVERSION_UINT || conv == CONVERSION_HEX_LOWER
|| conv == CONVERSION_HEX_UPPER)
{
- if (len >= pformat->precision.hardcoded)
+ if (len >= pformat->precision.value)
return ;
- tmp = (char*)malloc(sizeof(char) * (pformat->precision.hardcoded + 1));
- ft_strcpy(tmp + pformat->precision.hardcoded - len, str);
+ 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.hardcoded -= len;
- while (pformat->precision.hardcoded-- > 0)
- tmp[pformat->precision.hardcoded] = '0';
+ pformat->precision.value -= len;
+ while (pformat->precision.value-- > 0)
+ tmp[pformat->precision.value] = '0';
ft_strcpy(str, tmp);
free(tmp);
}