-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_more_flags.c
107 lines (100 loc) · 2.34 KB
/
ft_more_flags.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_more_flags.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iidzim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/17 11:41:25 by iidzim #+# #+# */
/* Updated: 2019/12/27 12:12:30 by iidzim ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdio.h>
int ft_atoi(const char *str)
{
int signe;
long x;
int i;
signe = 1;
x = 0;
i = 0;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
signe = -1;
i++;
}
while (str[i] != '\0' && str[i] >= 48 && str[i] <= 57)
{
x = (x * 10) + ((char)str[i] - '0');
if (x > 4294967296)
return ((signe > 0) ? -1 : 0);
i++;
}
return (x * signe);
}
int ft_isdigit(int c)
{
if (c >= 48 && c <= 57)
return (1);
else
return (0);
}
void ft_width_arg(t_flags *f, va_list arg, int i)
{
i += 1;
f->width = va_arg(arg, int);
if (f->width < 0)
{
f->align = 1;
f->width *= -1;
}
}
void ft_precision(char *str, t_flags *f, va_list args, int *i)
{
*i += 1;
if (!(ft_isdigit(str[*i])) && str[*i] != '*')
f->prec = 0;
if (ft_isdigit(str[*i]) || str[*i] == '-')
{
f->prec = ft_atoi(str + (*i));
while (ft_isdigit(str[*i]))
*i += 1;
}
else if (str[*i] == '*')
{
f->prec = va_arg(args, int);
if (f->prec < 0)
f->prec = -1;
*i += 1;
}
}
void ft_more_flags(va_list args, char *str, int *i, t_flags *f)
{
(*i)++;
while (!(ft_strchr("cspduixX%%", str[*i])))
{
while (str[*i] == '-')
{
f->align = 1;
*i += 1;
}
if (str[*i] == '0')
{
f->zero = 1;
*i += 1;
}
if (ft_isdigit(str[*i]))
{
f->width = ft_atoi(str + (*i));
*i += ft_size_nbr(f->width);
}
else if (str[*i] == '*')
{
ft_width_arg(f, args, *i);
*i += 1;
}
(str[*i] == '.') ? ft_precision(str, f, args, i) : 0;
}
}