-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
108 lines (97 loc) · 2.62 KB
/
ft_printf.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_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kbelov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/30 14:23:16 by kbelov #+# #+# */
/* Updated: 2019/10/12 15:32:03 by kbelov ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void str_tolower(char **s)
{
char *tmp;
tmp = *s;
while (*tmp != '\0')
{
if (*tmp >= 'A' && *tmp <= 'Z')
*tmp += 32;
tmp++;
}
}
int print_formated(t_format *f, va_list *ap)
{
int charcount;
charcount = 0;
if (f->specifier == 'c')
print_c(f, ap, &charcount);
else if (f->specifier == '%')
print_percent(f, &charcount);
else if (f->specifier == 's')
print_s(f, ap, &charcount);
else if (f->specifier == 'd' || f->specifier == 'i')
print_d(f, ap, &charcount);
else if (f->specifier == 'u')
print_u(f, ap, &charcount);
else if (f->specifier == 'o' || f->specifier == 'x' ||
f->specifier == 'X' || f->specifier == 'b')
print_oxx(f, ap, &charcount);
else if (f->specifier == 'p')
print_p(f, ap, &charcount);
else if (f->specifier == 'f')
print_f(f, ap, &charcount);
else
charcount = 0;
return (charcount);
}
t_format *parse_format(const char *format, va_list *ap, size_t *i)
{
t_format *f;
f = malloc(sizeof(t_format));
(*i)++;
reset_format(f);
parse_flags(f, format, i);
parse_wid(f, format, i, ap);
parse_prec(f, format, i, ap);
parse_length(f, format, i);
parse_flags(f, format, i);
parse_type(f, format, i);
return (f);
}
int print_ap(const char *format, va_list *ap)
{
t_format *f;
int charcount;
size_t i;
charcount = 0;
i = 0;
while (i < ft_strlen(format))
{
if (format[i] == '%')
{
f = parse_format(format, ap, &i);
if (f->specifier != 'a')
charcount += print_formated(f, ap);
}
else
{
ft_putchar(format[i]);
charcount++;
i++;
}
}
free(f);
return (charcount);
}
int ft_printf(const char *format, ...)
{
va_list ap;
int charcount;
charcount = 0;
va_start(ap, format);
charcount = print_ap(format, &ap);
va_end(ap);
return (charcount);
}