-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.c
197 lines (174 loc) · 4.17 KB
/
handlers.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "main.h"
unsigned char handle_flags(const char *flag, char *index);
unsigned char handle_length(const char *modifier, char *index);
int handle_width(va_list args, const char *modifier, char *index);
int handle_precision(va_list args, const char *modifier, char *index);
unsigned int (*handle_specifiers(const char *specifier))(va_list, buffer_t *,
unsigned char, int, int, unsigned char);
/**
* handle_flags - Matches flags with corresponding values.
* @flag: A pointer to a potential string of flags.
* @index: An index counter for the original format string.
*
* Return: If flag characters are matched - a corresponding value.
* Otherwise - 0.
*/
unsigned char handle_flags(const char *flag, char *index)
{
int i, j;
unsigned char ret = 0;
flag_t flags[] = {
{'+', PLUS},
{' ', SPACE},
{'#', HASH},
{'0', ZERO},
{'-', NEG},
{0, 0}
};
for (i = 0; flag[i]; i++)
{
for (j = 0; flags[j].flag != 0; j++)
{
if (flag[i] == flags[j].flag)
{
(*index)++;
if (ret == 0)
ret = flags[j].value;
else
ret |= flags[j].value;
break;
}
}
if (flags[j].value == 0)
break;
}
return (ret);
}
/**
* handle_length - Matches length modifiers with their corresponding value.
* @modifier: A pointer to a potential length modifier.
* @index: An index counter for the original format string.
*
* Return: If a lenth modifier is matched - its corresponding value.
* Otherwise - 0.
*/
unsigned char handle_length(const char *modifier, char *index)
{
if (*modifier == 'h')
{
(*index)++;
return (SHORT);
}
else if (*modifier == 'l')
{
(*index)++;
return (LONG);
}
return (0);
}
/**
* handle_width - Matches a width modifier with its corresponding value.
* @args: A va_list of arguments.
* @modifier: A pointer to a potential width modifier.
* @index: An index counter for the original format string.
*
* Return: If a width modifier is matched - its value.
* Otherwise - 0.
*/
int handle_width(va_list args, const char *modifier, char *index)
{
int value = 0;
while ((*modifier >= '0' && *modifier <= '9') || (*modifier == '*'))
{
(*index)++;
if (*modifier == '*')
{
value = va_arg(args, int);
if (value <= 0)
return (0);
return (value);
}
value *= 10;
value += (*modifier - '0');
modifier++;
}
return (value);
}
/**
* handle_precision - Matches a precision modifier with
* its corresponding value.
* @args: A va_list of arguments.
* @modifier: A pointer to a potential precision modifier.
* @index: An index counter for the original format string.
*
* Return: If a precision modifier is matched - its value.
* If the precision modifier is empty, zero, or negative - 0.
* Otherwise - -1.
*/
int handle_precision(va_list args, const char *modifier, char *index)
{
int value = 0;
if (*modifier != '.')
return (-1);
modifier++;
(*index)++;
if ((*modifier <= '0' || *modifier > '9') &&
*modifier != '*')
{
if (*modifier == '0')
(*index)++;
return (0);
}
while ((*modifier >= '0' && *modifier <= '9') ||
(*modifier == '*'))
{
(*index)++;
if (*modifier == '*')
{
value = va_arg(args, int);
if (value <= 0)
return (0);
return (value);
}
value *= 10;
value += (*modifier - '0');
modifier++;
}
return (value);
}
/**
* handle_specifiers - Matches a conversion specifier with
* a corresponding conversion function.
* @specifier: A pointer to a potential conversion specifier.
*
* Return: If a conversion function is matched - a pointer to the function.
* Otherwise - NULL.
*/
unsigned int (*handle_specifiers(const char *specifier))(va_list, buffer_t *,
unsigned char, int, int, unsigned char)
{
int i;
converter_t converters[] = {
{'c', convert_c},
{'s', convert_s},
{'d', convert_di},
{'i', convert_di},
{'%', convert_percent},
{'b', convert_b},
{'u', convert_u},
{'o', convert_o},
{'x', convert_x},
{'X', convert_X},
{'S', convert_S},
{'p', convert_p},
{'r', convert_r},
{'R', convert_R},
{0, NULL}
};
for (i = 0; converters[i].func; i++)
{
if (converters[i].specifier == *specifier)
return (converters[i].func);
}
return (NULL);
}