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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_treat_flags.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 17:06:45 by rbousset #+# #+# */
/* Updated: 2020/02/14 17:06:45 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <stdlib.h>
#include <stdarg.h>
static int
ft_printf_get_width(va_list arg, t_printflist *pflist)
{
char *nstr;
char *nstr_ptr;
int ret;
if (ft_strlchr(pflist->fullflag, '.') >= 0)
nstr = ft_substr(pflist->fullflag, 0,
ft_strlchr(pflist->fullflag, '.'));
else
nstr = ft_strdup(pflist->fullflag);
nstr_ptr = nstr;
nstr = ft_printf_get_width_nstr(nstr, pflist);
ret = ft_printf_fetch_width(arg, nstr, pflist);
ft_memdel((void**)&nstr_ptr);
(ret < 0) ? (pflist->isreverse = 1) : 0;
(ret < 0) ? (ret = -ret) : 0;
return (ret);
}
static int
ft_printf_get_precision(va_list arg, int pos, t_printflist *pflist)
{
int ret;
char *ptr;
ptr = pflist->fullflag;
if (pflist->isaz && ft_strlchr(pflist->fullflag, '+') >= 0)
{
pflist->isaplus = 1;
if (ft_strlen(ptr) < pflist->flaglen)
ptr += 1;
}
if (*(ptr + pos + 1) == '*')
{
ret = va_arg(arg, int);
(ret < 0) ? (ret = 0) : 0;
return (ret);
}
else
ret = ft_atoi(ptr + pos + 1);
if (ret == 0)
return (-1);
return (ret);
}
/*
** Corresponding l ll hh h
** in pflist->lh
** hh = -2
** h = -1
** l = 1
** ll = 2
*/
static void
ft_printf_get_lh(t_printflist *pflist)
{
int pos;
if ((pos = ft_strlchr(pflist->fullflag, 'z')) >= 0)
pflist->zflag = 1;
else if ((pos = ft_strlchr(pflist->fullflag, 'l')) >= 0)
{
if (pos + 2 <= (int)pflist->flaglen &&
*(pflist->fullflag + pos + 1) == 'l')
pflist->lh = 2;
else
pflist->lh = 1;
}
else if ((pos = ft_strlchr(pflist->fullflag, 'h')) >= 0)
{
if (pos + 2 <= (int)pflist->flaglen &&
*(pflist->fullflag + pos + 1) == 'h')
pflist->lh = -2;
else
pflist->lh = -1;
}
}
static uint8_t
ft_printf_check_z(t_printflist *pflist)
{
char *ptr;
ptr = pflist->fullflag;
while (!ft_isdigit(*ptr) && *ptr)
ptr++;
if ((ptr - pflist->fullflag) <= (long)ft_strlen(pflist->fullflag)
&& (*ptr == '0') && ft_strlchr(pflist->fullflag, '.') < 0)
return (1);
return (0);
}
/*
** Uses pflist->fullflag to put
** width and precision in the list
** also l ll hh h
*/
void
ft_printf_treat_flags(va_list arg, t_printflist *pflist)
{
int pos;
char c;
pflist->flaglen = ft_strlen(pflist->fullflag);
if (((pflist->isaz = ft_printf_check_z(pflist)) == 1
&& ft_strlchr(pflist->fullflag, '.') < 0
&& ft_strlchr(pflist->fullflag, '-') < 0))
pflist->precision = ft_printf_get_precision(arg, 0, pflist);
else
{
pflist->width = ft_printf_get_width(arg, pflist);
(pflist->isaspace >= 0) ? (pflist->isaspace = 1) : 0;
if ((pos = ft_strlchr(pflist->fullflag, '.')) >= 0)
pflist->precision = ft_printf_get_precision(arg, pos, pflist);
}
(pflist->isaz && pflist->width) ? (pflist->isaz = 0) : 0;
(ft_strlchr(pflist->fullflag, '#') >= 0) ? (pflist->issharp = 1) : 0;
c = pflist->actconv;
if (ft_strlchr(pflist->fullflag, '.') < 0 && pflist->isaz
&& pflist->precision < 0 && c != 'c' && c != 's' && c != '%')
{
pflist->precision = 0;
pflist->isaz = 0;
}
ft_printf_get_lh(pflist);
}
|