-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
executable file
·128 lines (119 loc) · 2.99 KB
/
ft_printf.c
File metadata and controls
executable file
·128 lines (119 loc) · 2.99 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vnakonec <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/07 16:37:23 by vnakonec #+# #+# */
/* Updated: 2017/03/17 18:47:33 by vnakonec ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdio.h>
int save_str(const char *str, t_prf *phar, va_list ap)
{
phar->spec = str[0];
if (phar->spec == 'd' || phar->spec == 'i' || phar->spec == 'D')
save_d(phar, ap);
else if (phar->spec == 'x' || phar->spec == 'X')
save_x(phar, ap);
else if (phar->spec == 'O' || phar->spec == 'o')
save_o(phar, ap);
else if (phar->spec == 'p')
save_p(phar, ap);
else if (phar->spec == 'u' || phar->spec == 'U')
save_u(phar, ap);
else if (phar->spec == 's' || phar->spec == 'S')
save_s(phar, ap);
else if (phar->spec == 'b')
save_b(phar, ap);
else
save_c(phar, ap);
if (!phar->str)
return (0);
if (phar->str[0] == '\0' && phar->str[1] == '\0')
phar->str[0] = '\301';
return (1);
}
void write_str(t_prf *phar)
{
int i;
i = 0;
if (!phar->str)
return ;
while (phar->str[i] != '\0')
{
if (phar->str[i] == '\301')
write(1, "\0", 1);
else
ft_putchar_fd(phar->str[i], 1);
i++;
}
}
int make_all(t_prf *phar)
{
if (phar->str[0] != '\301')
{
if (phar->pr > 0)
make_prec(phar);
make_plus(phar);
if (phar->spec != 'u' && phar->spec != 'o')
make_space(phar);
make_hesh(phar);
}
make_width(phar);
kostuli(phar);
return (1);
}
int phars(const char *str, va_list ap, int *count)
{
int i;
t_prf phar;
int a;
i = 0;
__builtin_bzero(&phar, sizeof(phar));
while ((a = test_symb(str[i])) > 0)
{
if (a == 1)
i = i + flags(&str[i], &phar);
if (a == 4)
i = i + width(&str[i], &phar, ap);
if (a == 3)
i = i + 1 + prec(&str[i + 1], &phar, ap);
if (a == 2)
i = i + length(&str[i], &phar);
if (a == 5)
return (i);
}
i = i + save_str(&str[i], &phar, ap);
make_all(&phar);
if (phar.str)
write_str(&phar);
*count += ft_strlen(phar.str);
return (i);
}
int ft_printf(const char *format, ...)
{
int i;
int count;
va_list ap;
count = 0;
i = 0;
va_start(ap, format);
while (format[i] != '\0')
{
while (format[i] == '%')
i = i + phars(&format[i + 1], ap, &count) + 1;
if (format[i] == '{')
i = i + color(&format[i]);
if (format[i] != '\0')
{
ft_putchar_fd(format[i], 1);
count++;
i++;
}
}
va_end(ap);
return (count);
}