-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.c
177 lines (162 loc) · 3.21 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
#include "main.h"
/**
* handle_custom_specifiers - Handles custom conversion specifiers.
* @format: The format string.
* @args: The variable arguments list.
*
* Return: The number of characters processed from the format string.
*/
int handle_custom_specifiers(const char *format, va_list args)
{
int i = 0;
if (format[i] == 'b')
{
unsigned int num = va_arg(args, unsigned int);
i += print_binary(num);
}
else if (format[i] == 'S')
{
const char *str = va_arg(args, const char *);
i += print_S(str);
}
else if (format[i] == 'R')
{
const char *str = va_arg(args, const char *);
i += _rot13(str);
}
else if (format[i] == 'r')
{
const char *str = va_arg(args, const char *);
i += reversed(str);
}
else
{
_putchar('%');
_putchar(format[i]);
i++;
}
return (i);
}
/**
* handle_specifiers_2 - Handles conversion specifiers 2.
* @format: The format string
* @args: The variable arguments list
*
* Return: The number of characters processed from the format string
*/
int handle_specifiers_2(const char *format, va_list args)
{
int i = 0;
if (format[i] == 'o')
{
unsigned int num = va_arg(args, unsigned int);
i += print_octal(num);
}
else if (format[i] == 'p')
{
void *ptr = va_arg(args, void *);
i += print_addr(ptr);
}
else if (format[i] == 'x' || format[i] == 'X')
{
unsigned int num = va_arg(args, unsigned int);
int upper = (format[i] == 'X') ? 1 : 0;
i += print_hexa(num, upper);
}
else
{
_putchar('%');
_putchar(format[i]);
i++;
}
return (i);
}
/**
* handle_specifiers - Handles conversion specifiers in the format string
* @format: The format string
* @args: The variable arguments list
* Return: The number of characters processed from the format string
*/
int handle_specifiers(const char *format, va_list args)
{
int i = 0;
if (format[i] == 'c')
{
char ch = va_arg(args, int);
_putchar(ch);
i++;
}
else if (format[i] == 's')
{
const char *str = va_arg(args, const char *);
i += print_string(str);
}
else if (format[i] == 'd' || format[i] == 'i')
{
int num = va_arg(args, int);
i += print_int(num);
}
else if (format[i] == 'u')
{
unsigned int num = va_arg(args, unsigned int);
i += print_unsigned_int(num);
}
else if (format[i] == '%')
{
_putchar('%');
i++;
}
else
{
_putchar('%');
_putchar(format[i]);
i++;
}
return (i);
}
/**
* handle_formats - Handles the format specifiers
* @format: The format string
* @args: The va_list of arguments
* Return: The number of characters handled
*/
int handle_formats(const char *format, va_list args)
{
int count = 0, i = 0;
for (; format[i]; i++)
{
if (format[i] == '%')
{
i++;
if (format[i] == '\0')
return (-1);
if (format[i] == ' ')
{
_putchar('%');
_putchar(' ');
count += 2;
continue;
}
if (format[i] == 'b' || format[i] == 'S' ||
format[i] == 'r' || format[i] == 'R')
{
count += handle_custom_specifiers(&format[i], args);
}
else if (format[i] == 'o' || format[i] == 'p' ||
format[i] == 'x' || format[i] == 'X')
{
count += handle_specifiers_2(&format[i], args);
}
else
{
count += handle_specifiers(&format[i], args);
}
}
else
{
_putchar(format[i]);
count++;
}
}
return (count);
}