aboutsummaryrefslogtreecommitdiff
path: root/parse.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-12 10:43:01 +0200
committerCharles <sircharlesaze@gmail.com>2019-10-12 11:48:54 +0200
commit6ea4606cd3f74377691d200d69df8398f90cc2ff (patch)
treef354459b9054f5bd23a7508c12ef1589af38e75a /parse.c
parentbddc2d153a4c47257740a0bf0651513058a612d5 (diff)
downloadft_printf-6ea4606cd3f74377691d200d69df8398f90cc2ff.tar.gz
ft_printf-6ea4606cd3f74377691d200d69df8398f90cc2ff.tar.bz2
ft_printf-6ea4606cd3f74377691d200d69df8398f90cc2ff.zip
Basic conversion parsing
Using a list to store each format conversion informations.
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/parse.c b/parse.c
index e69de29..11316ba 100644
--- a/parse.c
+++ b/parse.c
@@ -0,0 +1,58 @@
+#include <stdlib.h>
+#include "header.h"
+
+#define STRRCHR_CONVERSIONS(c) (ft_strrchr(CONVERSIONS_STR, c))
+
+t_list *parse(const char *format)
+{
+ t_list *format_list;
+ t_list *tmp;
+ t_pformat *parsed;
+
+ format_list = NULL;
+ while (*format)
+ {
+ if (*format != '%')
+ {
+ format++;
+ continue;
+ }
+ format++;
+ if ((parsed = parse_conversion(isolate_conversion(format))) == NULL)
+ return (list_destroy(format_list));
+ if ((tmp = list_new(parsed)) == NULL)
+ return (list_destroy(format_list));
+ list_push_back(&format_list, tmp);
+ }
+ return (format_list);
+}
+
+char *isolate_conversion(const char *conversion_start)
+{
+ int i;
+
+ i = 0;
+ while (strrchr_index(CONVERSIONS_STR, conversion_start[i]) == -1)
+ i++;
+ return (ft_strndup(conversion_start, i + 1));
+}
+
+t_pformat *parse_conversion(char *conversion)
+{
+ int i;
+ 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->conversion = strrchr_index(CONVERSIONS_STR, conversion[i]);
+ i--;
+ pformat->arg_position = 0;
+ pformat->left_adjusted = FALSE;
+ pformat->zero_padding = FALSE;
+ pformat->min_field_width = -1;
+ pformat->len = 1;
+ return (pformat);
+}