-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_parse.c
126 lines (115 loc) · 3.25 KB
/
ft_parse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cado-car <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/04 16:38:09 by cado-car #+# #+# */
/* Updated: 2021/08/20 19:46:37 by cado-car ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
/*
* DESCRIPTION
* The parse() function will take the format string from the index in which the
* placeholder begins, and will attempt to analyze and organize its flags and
* conversors into a struct of type t_holder, using other parsing subfunctions.
* PARAMETERS
* #1. The t_format struct that holds information about the string to be
* formatted.
* RETURN VALUES
* it returns the t_holder variable that holds all the information about that
* particular placeholder.
*/
#include <stdio.h>
void *ft_parse(t_format *fmt, t_holder *h)
{
ft_parse_flags(fmt, h);
ft_parse_width(fmt, h);
ft_parse_precision(fmt, h);
ft_parse_conversion(fmt, h);
if (!h->conversion && (ft_strchr(HOLDER_ALL, fmt->format[fmt->i])))
ft_parse(fmt, h);
return (h);
}
void ft_parse_flags(t_format *fmt, t_holder *h)
{
char *temp;
if (!h->prefix)
h->prefix = ft_strdup("");
while (ft_strchr(HOLDER_ALL_FLAGS, fmt->format[fmt->i]))
{
if (fmt->format[fmt->i] == HOLDER_JUSTIFY)
h->left_justify = 1;
if (ft_strchr(HOLDER_PREFIX, fmt->format[fmt->i]))
{
temp = h->prefix;
h->prefix = ft_appendchr(temp, fmt->format[fmt->i]);
free(temp);
}
if (fmt->format[fmt->i] == HOLDER_PAD)
h->padding = HOLDER_PAD;
fmt->i++;
}
}
void ft_parse_width(t_format *fmt, t_holder *h)
{
int width;
width = h->width;
if (fmt->format[fmt->i] == HOLDER_STAR)
{
width = (int)va_arg(fmt->ap, int);
if (width < 0)
{
h->left_justify = 1;
width *= -1;
}
fmt->i++;
}
else if (ft_isdigit(fmt->format[fmt->i]))
{
width = 0;
while (ft_isdigit(fmt->format[fmt->i]))
{
width = (width * 10) + (fmt->format[fmt->i] - '0');
fmt->i++;
}
}
h->width = width;
}
void ft_parse_precision(t_format *fmt, t_holder *h)
{
int precision;
precision = h->precision;
if (fmt->format[fmt->i] == HOLDER_PRECISION)
{
fmt->i++;
if (fmt->format[fmt->i] == HOLDER_STAR)
{
precision = (int)va_arg(fmt->ap, int);
fmt->i++;
}
else if (!ft_isdigit(fmt->format[fmt->i]))
precision = 0;
else
{
precision = 0;
while (ft_isdigit(fmt->format[fmt->i]))
{
precision = (precision * 10) + (fmt->format[fmt->i] - '0');
fmt->i++;
}
}
}
h->precision = precision;
}
void ft_parse_conversion(t_format *fmt, t_holder *h)
{
if (!ft_strchr(HOLDER_ALL, fmt->format[fmt->i]) \
&& ft_isprint(fmt->format[fmt->i]))
{
h->conversion = fmt->format[fmt->i];
fmt->i++;
}
}