Repository navigation
Expand file tree
/
Copy pathft_printf.c
More file actions
153 lines (140 loc) · 4.4 KB
/
Copy pathft_printf.c
File metadata and controls
153 lines (140 loc) · 4.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
152
153
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yazhu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/12/20 15:07:42 by yazhu #+# #+# */
/* Updated: 2018/01/09 21:42:27 by yazhu ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void occupy_conversion(const char *s, int *i, t_format *format)
{
char c;
c = s[*i];
if (c == 's' || c == 'S' || c == 'p' || c == 'd' || c == 'D' || c == 'i'
|| c == 'o' || c == 'O' || c == 'u' || c == 'U' || c == 'x'
|| c == 'X' || c == 'c' || c == 'C' || c == '%' || c == 'n'
|| c == 'f' || c == 'F' || c == 'e' || c == 'E' || c == 'g'
|| c == 'G')
format->conv = s[(*i)++];
else
{
format->conv = '\0';
if ((s[*i] == 'l' && format->len[0] == 'l')
|| (s[*i] == 'h' && format->len[0] == 'h'))
{
(*i)++;
while (s[*i] != '\0')
(*i)++;
}
}
if (format->conv == 'D' || format->conv == 'O'
|| format->conv == 'U' || format->conv == 'S'
|| format->conv == 'c')
format->len[0] = 'l';
}
/*
** order of precedence if >1 length modifier is provided:
** [l, ll, j, t, z] then [h] then [hh]
*/
static void occupy_len(const char *s, int *i, t_format *format, int replace)
{
if (replace == 0)
{
format->len[0] = (s[*i] == 'h' || s[*i] == 'l' || s[*i] == 'j'
|| s[*i] == 'z' || s[*i] == 'L') ? s[(*i)++] : '\0';
format->len[1] = ((s[*i - 1] == 'h' && s[*i] == 'h')
|| (s[*i - 1] == 'l' && s[*i] == 'l')) ? s[(*i)++] : '\0';
format->len[2] = '\0';
}
else
{
if ((s[*i] == 'h' || s[*i] == 'l' || s[*i] == 'j' || s[*i] == 'z'
|| s[*i] == 'L') && (*i)++)
format->len[0] = (!(format->len[0]) || format->len[1] == 'h'
|| (format->len[0] == 'h' && s[(*i) - 1] != 'h'))
? s[(*i) - 1] : format->len[0];
format->len[1] = ((s[(*i) - 1] == 'h' && s[*i] == 'h')
|| (s[(*i) - 1] == 'l' && s[*i] == 'l'))
? s[(*i)++] : format->len[1];
}
}
/*
** set_w will use absolute value of user specified min_w
** if the number provided by user is negative, a '-' flag is added
*/
static int set_w(t_format *format, int *i, int *j, va_list ap)
{
unsigned long long tmp;
tmp = va_arg(ap, int);
if ((int)tmp < 0)
{
tmp = (int)tmp * -1;
format->flag[(*j)++] = '-';
}
(*i)++;
return (tmp);
}
/*
** format->precision:
** set to -1 if there is no '.'
** set to 0 if there is a '.' but no number
*/
static void occupy_format(const char *s, int *i, t_format *format,
va_list ap)
{
int j;
j = 0;
occupy_len(s, i, format, 0);
while (s[*i] == '#' || s[*i] == '0' || s[*i] == '-' || s[*i] == '+'
|| s[*i] == ' ' || s[*i] == '\'')
format->flag[j++] = s[(*i)++];
format->min_w = (s[*i] >= '0' && s[*i] <= '9') ? ft_atoi(&s[*i]) : -1;
while (s[*i] >= '0' && s[*i] <= '9')
(*i)++;
format->min_w = (s[*i] == '*') ? set_w(format, i, &j, ap) : format->min_w;
while (s[*i] == '#' || s[*i] == '0' || s[*i] == '-' || s[*i] == '+'
|| s[*i] == ' ' || s[*i] == '\'')
format->flag[j++] = s[(*i)++];
while (j < 6)
format->flag[j++] = '\0';
format->prec = (s[*i] == '.') ? 0 : -1;
format->prec = (s[*i] == '.' && s[++(*i)] >= '0' && s[*i] <= '9')
? ft_atoi(&s[*i]) : format->prec;
format->prec = (s[*i] == '*' && ++(*i))
? va_arg(ap, int) : format->prec;
while (s[*i] >= '0' && s[*i] <= '9')
(*i)++;
occupy_len(s, i, format, 1);
occupy_conversion(s, i, format);
}
int ft_printf(const char *s, ...)
{
int i;
int count;
va_list ap;
t_format format;
int reset_text_color;
count = 0;
reset_text_color = 0;
i = 0;
va_start(ap, s);
while (s[i] != '\0')
{
if (s[i] == '%' && ++i)
{
occupy_format(s, &i, &format, ap);
conversion(&format, ap, &count);
}
else if (s[i] == '{')
set_color(s, &i, &reset_text_color, &count);
else
count += write(1, &s[i++], 1);
}
va_end(ap);
ft_putstr((reset_text_color) ? "\x1B[39m" : "");
return (count);
}