aboutsummaryrefslogtreecommitdiff
path: root/parse.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-15 13:02:53 +0200
committerCharles <sircharlesaze@gmail.com>2019-10-15 13:02:53 +0200
commitfa4bf89263e897695dbf48061369a23d695fef8b (patch)
tree0c1bd50114f49adac07bd2fabda5d7a5ea74e88e /parse.c
parent60733a2298c7a93fe681f78af9b69e1639a791b5 (diff)
downloadft_printf-fa4bf89263e897695dbf48061369a23d695fef8b.tar.gz
ft_printf-fa4bf89263e897695dbf48061369a23d695fef8b.tar.bz2
ft_printf-fa4bf89263e897695dbf48061369a23d695fef8b.zip
Parsing rewrite
- 4 extraction functions which parse some format attributes and remove them. - Not handling wildcard yet
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c97
1 files changed, 11 insertions, 86 deletions
diff --git a/parse.c b/parse.c
index 17cbb22..b07b436 100644
--- a/parse.c
+++ b/parse.c
@@ -2,12 +2,12 @@
#include "header.h"
#define STRRCHR_CONVERSIONS(c) (ft_strrchr(CONVERSIONS_STR, c))
-#define IS_STANDALONE_FLAG(c) (c == '0' || c == '-')
/*
** %(?:\d+\$)?[-]?(?:[0]|'.{1})?-?\d*(?:\.\d+)?[cdusxX]
*/
+#include <stdio.h>
t_list *parse(const char *format)
{
t_list *format_list;
@@ -23,7 +23,7 @@ t_list *parse(const char *format)
continue;
}
format++;
- if ((parsed = parse_conversion(isolate_conversion(format))) == NULL)
+ if ((parsed = parse_reduced(isolate_conversion(format))) == NULL)
return (list_destroy(format_list));
if ((tmp = list_new(parsed)) == NULL)
return (list_destroy(format_list));
@@ -42,100 +42,25 @@ char *isolate_conversion(const char *conversion_start)
return (ft_strndup(conversion_start, i + 1));
}
-
-t_pformat *parse_reduced_fmt(char *fmt)
+t_pformat *parse_reduced(char *fmt)
{
t_pformat *pformat;
- if (conversion == NULL)
+ if (fmt == NULL)
return (NULL);
if ((pformat = (t_pformat*)malloc(sizeof(t_pformat))) == NULL)
return (NULL);
pformat->ap_index = -1;
pformat->left_adjusted = FALSE;
pformat->zero_padding = FALSE;
- pformat->min_width->wildcard->exist = FALSE;
- pformat->precision->wildcard->exist = FALSE;
- fmt = extrac_ap_index(pformat, fmt);
- fmt = extrac_standalone_flags(pformat, fmt);
+ pformat->min_width.wildcard.exist = FALSE;
+ pformat->precision.wildcard.exist = FALSE;
+ pformat->precision.hardcoded = -1;
+ pformat->len = ft_strlen(fmt);
+ pformat->conversion = strrchr_index(CONVERSIONS_STR, fmt[pformat->len - 1]);
+ fmt[pformat->len - 1] = 0;
+ fmt = extract_standalone_flags(pformat, fmt);
fmt = extract_min_width(pformat, fmt);
fmt = extract_precision(pformat, fmt);
return (pformat);
}
-
-// %[position][dollar][flags][width][.precision][length]type
-t_pformat *parse_conversion(char *conversion)
-{
- int i;
- char *start;
- t_pformat *pformat;
-
- if (conversion == NULL)
- return (NULL);
- if ((pformat = (t_pformat*)malloc(sizeof(t_pformat))) == NULL)
- return (NULL);
- i = ft_strlen(conversion) - 1;
- pformat->len = i + 1;
- pformat->conversion = strrchr_index(CONVERSIONS_STR, conversion[i]);
- i--;
- if ((conversion = parse_arg_position(conversion, pformat)) == NULL)
- return (NULL);
- start = conversion;
- pformat->zero_padding = FALSE;
- pformat->left_adjusted = FALSE;
- while (IS_STANDALONE_FLAG(*start))
- {
- if (!pformat->zero_padding)
- pformat->zero_padding = *start == '0';
- if (!pformat->left_adjusted)
- pformat->left_adjusted = *start == '-';
- start++;
- }
- pformat->min_field_width_wildcard = FALSE;
- pformat->min_field_width = -1;
- if (*start == '*')
- {
- pformat->min_field_width_wildcard = TRUE;
- start++;
- }
- if (ft_isdigit(*start))
- {
- /* pformat->min_field_width_wildcard = FALSE; */
- pformat->min_field_width = ft_atoi(start);
- while (ft_isdigit(*start))
- start++;
- }
- pformat->precision = -1;
- pformat->precision_wildcard = FALSE;
- if (*start == '.')
- {
- start++;
- if (*start == '*')
- pformat->precision_wildcard = TRUE;
- else
- {
- pformat->precision = ft_atoi(start);
- while (ft_isdigit(*start))
- start++;
- }
- }
- return (pformat);
-}
-
-char *parse_arg_position(char *conversion, t_pformat *pformat)
-{
- if (strrchr_index(conversion, '$') != -1)
- {
- if ((pformat->ap_index = ft_atoi(conversion)) == 0)
- {
- free(pformat);
- return (NULL);
- }
- while (ft_isdigit(*conversion))
- conversion++;
- conversion++;
- }
- else
- pformat->ap_index = -1;
- return (conversion);
-}