-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
151 lines (138 loc) · 3.4 KB
/
ft_printf.c
File metadata and controls
151 lines (138 loc) · 3.4 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anttran <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/13 10:33:30 by anttran #+# #+# */
/* Updated: 2019/02/28 11:55:19 by anttran ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int g_c;
static int g_r;
static int g_p;
static t_color g_colors[] =
{
{"{LR}", "\e[38;2;225;100;125m"},
{"{LY}", "\e[38;2;255;220;65m"},
{"{LG}", "\e[38;2;185;225;70m"},
{"{LB}", "\e[38;2;80;220;200m"},
{"{LP}", "\e[38;2;160;160;255m"},
{"{RE}", "\e[1;38;2;225;20;20m"},
{"{OR}", "\e[1;38;2;255;120;10m"},
{"{YE}", "\e[1;38;2;255;200;0m"},
{"{GR}", "\e[1;38;2;80;200;60m"},
{"{BL}", "\e[1;38;2;50;150;250m"},
{"{PR}", "\e[1;38;2;150;75;255m"},
{"{BR}", "\e[1;48;2;150;0;0m"},
{"{BY}", "\e[1;48;2;255;155;0m"},
{"{BG}", "\e[1;48;2;0;100;25m"},
{"{BB}", "\e[1;48;2;0;65;140m"},
{"{BP}", "\e[1;48;2;60;0;125m"}
};
static int colors(const char *f, int i)
{
int c;
c = -1;
while (++c < 16)
if (ft_strnequ(f + i, g_colors[c].key, 4))
{
if (g_r)
ft_putstr_fd("\033[0m", 1);
g_r = 1;
ft_putstr_fd(g_colors[c].value, 1);
return (i += 4);
}
g_c++;
ft_putchar_fd('{', 1);
g_p = i + 1;
if (f[++i] == '{')
return (colors(f, i));
return (0);
}
static int perc(const char *f, int i)
{
char *str;
char *fill;
t_attr bah;
int len;
bah = set_attr(f, i);
if (bah.width > 1)
{
fill = fill_str(bah.width - 1, ' ');
if (find_c(bah.flags, '-'))
str = ft_strjoin("%", fill);
else
str = ft_strjoin(fill, "%");
free(fill);
}
else
str = ft_strdup("%");
ft_putstr_fd(str, g_fd);
len = ft_strlen(str);
free(str);
return (len);
}
static int print_arg(va_list ap, const char *f, int i)
{
int x;
x = i;
i = hidden_c(f, "cspfFdDioOuUxX%", i, ft_strlen(f));
if (f[i] == 'c' || f[i] == 's' || f[i] == 'p')
g_c += csp(ap, f, x);
else if (f[i] == 'd' || f[i] == 'D' || f[i] == 'i')
g_c += di(ap, f, x);
else if (f[i] == 'f' || f[i] == 'F')
g_c += dubz(ap, f, x);
else if (f[i] == '%')
g_c += perc(f, x);
else
g_c += oux(ap, f, x);
return (1);
}
int ft_fprintf(int fd, const char *f, ...)
{
int i;
va_list ap;
i = 0;
g_fd = fd;
va_start(ap, f);
while (f[i])
{
if (f[i] == '%' && print_arg(ap, f, ++i))
i = hidden_c4(f, "cspfFdDioOuUxX%", i, ft_strlen(f));
else
{
ft_putchar_fd(f[i++], fd);
g_c++;
}
}
va_end(ap);
return (f[i] ? -1 : g_c);
}
int ft_printf(const char *f, ...)
{
int i;
va_list ap;
i = 0;
g_fd = 1;
va_start(ap, f);
while (f[i])
{
if ((f[i] == '{') && !(i = colors(f, i)))
i = g_p;
if ((f[i] == '%') && print_arg(ap, f, ++i))
i = hidden_c4(f, "cspfFdDioOuUxX%", i, ft_strlen(f));
else
{
ft_putchar_fd(f[i++], 1);
g_c++;
}
}
if (g_r)
ft_putstr_fd("\033[0m", 1);
va_end(ap);
return (f[i] ? -1 : g_c);
}