-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_handler.c
More file actions
184 lines (163 loc) · 3.87 KB
/
format_handler.c
File metadata and controls
184 lines (163 loc) · 3.87 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "_printf_internals.h"
/**
* get_flags - interpret the flag modifiers in the string.
* @format: the format string.
* @mods: variable to store results.
*/
static void get_flags(string *format, modifiers *mods)
{
char c = string_readc(format);
if (c < 0)
return;
while (c == '#' || c == '0' || c == '-' || c == ' ' || c == '+')
{
switch (c)
{
case '#':
mods->flags.alternate_form = true;
break;
case '0':
mods->flags.zero_padding = true;
break;
case '-':
mods->flags.left_adjust = true;
break;
case ' ':
mods->flags.space = true;
break;
case '+':
mods->flags.sign = true;
break;
default:
break;
}
c = string_readc(format);
}
string_readp(format);
}
/**
* get_width - interpret the width modifiers in the string.
* @args: list of arguments to `_printf`.
* @format: the format string.
* @mods: variable to store results.
*/
static void get_width(va_list args, string *format, modifiers *mods)
{
int i = 0;
(void)args;
if (string_peekc(format) == '*')
{
mods->width = va_arg(args, int);
string_readc(format);
return;
}
if (!isdigit(string_peekc(format)))
return;
mods->width = _atoimax(&format->s[format->i]);
for (i = count_digits(mods->width, BASE10); i > 0; --i)
string_readc(format);
}
/**
* get_precision - interpret the precision modifiers in the string.
* @args: list of arguments to `_printf`.
* @format: the format string.
* @mods: variable to store results.
*/
static void get_precision(va_list args, string *format, modifiers *mods)
{
char c = string_peekc(format);
mods->precision = -1;
if (c != '.')
return;
string_readc(format);
c = string_peekc(format);
if (c == '*')
{
mods->precision = va_arg(args, int);
string_readc(format);
return;
}
mods->precision = _atoimax(&format->s[format->i]);
if (c == '-')
string_readc(format);
while (isdigit(string_peekc(format)))
string_readc(format);
}
/**
* get_type - interpret the length modifiers in the string.
* @format: the format string.
* @mods: variable to store results.
*/
static void get_type(string *format, modifiers *mods)
{
char c = string_readc(format);
if (c < 0)
return;
if (c == 'h')
{
mods->length = PRINTF_SHORT;
if (string_peekc(format) == 'h')
{
string_readc(format);
mods->length = PRINTF_CHAR;
}
}
else if (c == 'l')
{
mods->length = PRINTF_LONG;
if (string_peekc(format) == 'l')
{
string_readc(format);
mods->length = PRINTF_LLONG;
}
}
else if (c == 'L')
mods->length = PRINTF_LDOUBLE;
else if (c == 'j')
mods->length = PRINTF_INTMAX_T;
else if (c == 'z')
mods->length = PRINTF_SIZE_T;
else if (c == 't')
mods->length = PRINTF_PTRDIFF_T;
else
string_readp(format);
}
/**
* format_handler - handles formatting of an argument.
* @args: the argument to be formatted.
* @format: pointer to the format string.
* @buffer: pointer to the working buffer for `_printf`.
*
* Return: the number of characters printed, -1 if format doesn't match
*/
int format_handler(va_list args, string *format, char_arr *buffer)
{
char c = 0;
int bytes_printed = 0, i = 0;
modifiers mods = {0};
format_funcs fmt_funcs[] = {
{print_character, 'c'}, {print_str, 's'}, {print_percent, '%'},
{print_int_di, 'd'}, {print_int_di, 'i'}, {print_binary, 'b'},
{print_oct, 'o'}, {print_int_u, 'u'}, {print_hexa_lower, 'x'},
{print_hexa_upper, 'X'}, {print_ptr, 'p'}, {print_STR, 'S'},
{print_reversed, 'r'}, {print_rot13, 'R'}, {0},
};
assert(format && buffer);
get_flags(format, &mods);
get_width(args, format, &mods);
get_precision(args, format, &mods);
get_type(format, &mods);
c = string_peekc(format);
for (i = 0; fmt_funcs[i].ch; i++)
{
if (c == fmt_funcs[i].ch)
{
bytes_printed = fmt_funcs[i].func(args, buffer, mods);
string_readc(format);
break;
}
}
if (!fmt_funcs[i].ch)
bytes_printed = print_unknown(format, buffer);
return (bytes_printed);
}