-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_save_struct.c
108 lines (98 loc) · 2.4 KB
/
ft_save_struct.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_save_struct.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: afukuhar <afukuhar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/27 11:36:38 by afukuhar #+# #+# */
/* Updated: 2020/09/11 11:41:55 by afukuhar ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*
** All functions read string, update struct accordinly and return a pointer
** to new position after reading it
*/
const char *pf_saveflag(const char *str, t_format *arg)
{
while (*str && is_flag(*str))
{
if (*str == '0')
arg->zero = 1;
else if (*str == '-')
arg->left = 1;
str++;
}
return (str);
}
/*
** Save width based on string or va_list (when it is *)
**
** if w is sent as * and value is negative, it is converted to
** positive and updated 'left' flag
*/
const char *pf_savew(const char *str, t_format *arg, va_list *ap)
{
int w;
if (ft_isdigit(*str))
{
w = 0;
while (ft_isdigit(*str))
{
w = 10 * w + (*str - '0');
str++;
}
arg->w = w;
}
else if (*str == '*')
{
arg->w = va_arg(*ap, int);
if (arg->w < 0)
{
arg->w *= -1;
arg->left = 1;
}
str++;
}
return (str);
}
/*
** Same logic of width, with the difference that you should look for a
** '.' at beggining
** a negative precision is saved, but since condition for formatting is that
** it should be >= 0, it will be ignored on later functions
*/
const char *pf_savep(const char *str, t_format *arg, va_list *ap)
{
int p;
if (*str == '.')
{
str++;
p = 0;
if (ft_isdigit(*str))
{
while (ft_isdigit(*str))
{
p = 10 * p + (*str - '0');
str++;
}
}
else if (*str == '*')
{
p = va_arg(*ap, int);
str++;
}
arg->p = p;
}
return (str);
}
const char *pf_savespec(const char *str, t_format *arg)
{
if (*str && is_conv_spec(*str))
{
arg->spec = *str;
str++;
}
return (str);
}