forked from mnlazs/holbertonschool-print
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.c
executable file
·81 lines (66 loc) · 1.41 KB
/
handler.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
#include "main.h"
/**
* handler - Format controller
* @str: String format
* @list: List of arguments
*
* Return: Total size of arguments with the total size of the base string
**/
int handler(const char *str, va_list list)
{
int size, i, aux;
size = 0;
for (i = 0; str[i] != 0; i++)
{
if (str[i] == '%')
{
aux = percent_handler(str, list, &i);
if (aux == -1)
return (-1);
size += aux;
continue;
}
_putchar(str[i]);
size = size + 1;
}
return (size);
}
/**
* percent_handler - Controller for percent format
* @str: String format
* @list: List of arguments
* @i: Iterator
*
* Return: Size of the numbers of elements printed
**/
int percent_handler(const char *str, va_list list, int *i)
{
int size, j, number_formats;
format formats[] = {
{'s', print_string}, {'c', print_char},
{'d', print_integer}, {'i', print_integer},
{'b', print_binary}, {'u', print_unsigned},
{'o', print_octal}, {'x', print_hexadecimal_low},
{'X', print_hexadecimal_upp}, {'p', print_pointer},
{'r', print_rev_string}, {'R', print_rot}
};
*i = *i + 1;
if (str[*i] == '\0')
return (-1);
if (str[*i] == '%')
{
_putchar('%');
return (1);
}
number_formats = sizeof(formats) / sizeof(formats[0]);
for (size = j = 0; j < number_formats; j++)
{
if (str[*i] == formats[j].type)
{
size = formats[j].f(list);
return (size);
}
}
_putchar('%'), _putchar(str[*i]);
return (2);
}