-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_format.c
More file actions
109 lines (102 loc) · 2.9 KB
/
read_format.c
File metadata and controls
109 lines (102 loc) · 2.9 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvarodi <vvarodi@student.42madrid.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/15 20:20:54 by vvarodi #+# #+# */
/* Updated: 2020/08/19 09:39:27 by vvarodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_is_type(char c)
{
if (c == '%' || c == 'c' || c == 's' || c == 'p' || c == 'd'
|| c == 'i' || c == 'x' || c == 'X' || c == 'u')
return (1);
return (0);
}
void read_flags(t_buffer *b, t_flags *f, char *str, int *i)
{
while (str[*i] == '0' || str[*i] == '*' || str[*i] == '-')
{
if (str[*i] == '*')
{
f->width = va_arg(b->args, int);
(*i)++;
if (f->width < 0)
{
f->width *= -1;
f->b_left_aligned = 1;
}
}
if (str[*i] == '0')
{
f->b_zero_padding = 1;
(*i)++;
}
else if (str[*i] == '-')
{
f->b_left_aligned = 1;
(*i)++;
}
}
}
void width_precision(t_buffer *b, t_flags *f, char *str, int *i)
{
while (str[*i] >= '0' && str[*i] <= '9')
{
f->width = f->width * 10 + str[*i] - '0';
(*i)++;
}
if (str[*i] == '.')
{
(*i)++;
if (str[*i] == '*')
{
f->b_preci = 1;
f->precision = va_arg(b->args, int);
(*i)++;
}
if (((ft_is_type(str[*i]) || str[*i] == '0') && f->b_preci == 0)
|| (ft_is_type(str[*i]) || str[*i] == '0'))
f->b_preci = 2;
else
f->b_preci = 1;
while (str[*i] >= '0' && str[*i] <= '9')
{
f->precision = f->precision * 10 + str[*i] - '0';
(*i)++;
}
}
}
char *placeholders(t_buffer *b, t_flags *f, char *str)
{
int i;
i = 0;
read_flags(b, f, str, &i);
width_precision(b, f, str, &i);
if (ft_is_type(*(str + i)))
return (str + i);
else
return (str - 1);
}
char *read_format(t_buffer *b, t_flags *f, char *str)
{
str++;
str = placeholders(b, f, str);
if (*str == '%' || *str == 'c')
str = type_c(b, f, *str == 'c' ? va_arg(b->args, int) : '%', str) + 1;
else if (*str == 's')
str = type_s(b, f, va_arg(b->args, char *), str) + 1;
else if (*str == 'd' || *str == 'i')
str = type_di1(b, f, va_arg(b->args, int), str) + 1;
else if (*str == 'u')
str = type_u(b, f, va_arg(b->args, unsigned int), str) + 1;
else if (*str == 'x' || *str == 'X')
str = type_x(b, f, va_arg(b->args, unsigned int), str) + 1;
else if (*str == 'p')
str = type_p(b, f, va_arg(b->args, unsigned long int), str) + 1;
return (str);
}