-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintHandler.c
50 lines (49 loc) · 1.51 KB
/
printHandler.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
#include "main.h"
/**
* handle_print - Prints an argument depending on its type
* @fmt: Formatted strings that print the various arguments.
* @list: Listed all arguments fixed for print.
* @ind: ind.
* @buffer: Buffer array that handles print.
* @flags: evaluates all the active flags
* @width: get width specification.
* @precision: Specification of precision functions
* @size: size specifier function
*
* Return: 1 or 2;
*/
int handlePrint(const char *fmt, int *ind, va_list list, char buffer[],
int flags, int width, int precision, int size)
{
int i, unknown_len = 0, print_chars = -1;
fmt_t fmt_types[] = {
{'c', print_char}, {'s', print_string}, {'%', print_percent},
{'i', print_int}, {'d', print_int}, {'b', print_binary},
{'u', print_unsigned}, {'o', print_octal}, {'x', print_hexadecimal},
{'X', print_hexa_upper}, {'p', print_pointer}, {'S', print_non_printable},
{'r', print_reverse}, {'R', print_rot13string}, {'\0', NULL}
};
for (i = 0; fmt_types[i].fmt != '\0'; i++)
if (fmt[*ind] == fmt_types[i].fmt)
return (fmt_types[i].fn(list, buffer, flags, width, precision, size));
if (fmt_types[i].fmt == '\0')
{
if (fmt[*ind] == '\0')
return (-1);
unknown_len += write(1, "%%", 1);
if (fmt[*ind - 1] == ' ')
unknown_len += write(1, " ", 1);
else if (width)
{
--(*ind);
while (fmt[*ind] != ' ' && fmt[*ind] != '%')
--(*ind);
if (fmt[*ind] == ' ')
--(*ind);
return (1);
}
unknown_len += write(1, &fmt[*ind], 1);
return (unknown_len);
}
return (print_chars);
}