-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint_handler.c
57 lines (54 loc) · 1.55 KB
/
print_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
#include "main.h"
/**
* print_handler - a function to print any type of argument;
* @format: is multibytes character string to be used;
* @x: is number of arguments to be printed;
* @arg: is list of arguments;
* @buffer: is an array buffer as Random Access Memmory for print;
* @flags: is number of active flags;
* @width: is resulting width of format specification;
* @precision: is resulting precision of format specification;
* @size: is size of a given argument;
*
* Return: resulted format type otherwise value -1 or 1.
*/
int print_handler(const char *format, int *x, va_list arg, char buffer[],
int flags, int width, int precision, int size)
{
fmt_func fmt_type[] = {
{'%', print_percent}, {'c', print_char}, {'s', print_string},
{'u', print_uns}, {'i', print_int}, {'d', print_int},
{'b', print_binary}, {'o', print_octal}, {'x', print_hexadec},
{'X', print_upper_hexa}, {'p', print_pointer}, {'r', print_rev},
{'S', print_non_printable}, {'R', print_rot13}, {'\0', NULL}
};
int z, ulen = 0;
z = 0;
while (fmt_type[z].format != '\0')
{
if (format[*x] == fmt_type[z].format)
return (fmt_type[z].func(arg, buffer, flags, width,
precision, size));
z++;
}
if (fmt_type[z].format == '\0')
{
if (format[*x] == '\0')
return (-1);
ulen += write(1, "%%", 1);
if (format[*x] == ' ')
ulen += write(1, " ", 1);
else if (width)
{
--(*x);
while (format[*x] != ' ' && format[*x] != '%')
--(*x);
if (format[*x] == ' ')
--(*x);
return (1);
}
ulen += write(1, &format[*x], 1);
return (ulen);
}
return (-1);
}