-
Notifications
You must be signed in to change notification settings - Fork 0
/
_printf.c
75 lines (68 loc) · 1.57 KB
/
_printf.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
#include "main.h"
/**
* handel_specifires - handel all specifires in tha format
* @list: the vadriadic list
* @format: the format to insert the spacifires values in
* @str: the buffer
* @size: pointer to size of str
* Return: size of str buffer
*/
int handel_specifires(va_list *list, const char *format, char *str, int *size)
{
int i = 0, j = 0, result = 1;
specifier_handler handlers[] = {{'c', c_handler}, {'s', s_handler},
{'d', d_handler}, {'i', i_handler}, {'b', b_handler},
{'r', U_handler}, {'x', U_handler}, {'X', U_handler},
{'o', U_handler}, {'u', U_handler}, {'S', c_handler}};
if (!format || !list || !str)
return (-1);
while (format[i])
{
if (format[i] != '%')
{
str[(*size)++] = format[i];
i++;
continue;
}
result = percentage_handler(&str[*size], &format[i]);
if (result == -1)
return (-1);
else if (result == 1)
{
i += 2;
*size += 1;
continue;
}
else
i++;
for (j = 0; ; j++)
{
if (handlers[j].specifier == 'S')
break;
if (handlers[j].specifier == format[i])
{
*size += handlers[j].handler(&str[*size], list), i++;
}
}
}
return (*size);
}
/**
* _printf - print the format wicht specifires passed in
* @format: the format
* Return: the size of buffer
*/
int _printf(const char *format, ...)
{
va_list list;
char str[1024];
int str_size = 0, i, error_code;
if (!format)
return (-1);
va_start(list, format);
error_code = handel_specifires(&list, format, str, &str_size);
va_end(list);
for (i = 0; i < str_size; i++)
putchar(str[i]);
return ((error_code == -1) ? (-1) : (str_size));
}