-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_handling.c
49 lines (48 loc) · 1.38 KB
/
print_handling.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
#include "main.h"
/**
* print_handling - Function prints a type based argument.
* @fmt: Formatted string.
* @list: Argument list to print.
* @ind: integer argument.
* @buff: print Buffer array.
* @flag: active flags calculations
* @width: get width.
* @prec: specifies precision
* @size: specifies size
* Return: 1 or 2
*/
int print_handling(const char *fmt, int *ind, va_list list, char buff[],
int flag, int width, int prec, int size)
{
int i, length = 0, printed_characters = -1;
fmt_t fmt_types[] = {
{'c', char_print}, {'s', string_print}, {'%', percent_print},
{'i', int_print}, {'d', int_print}, {'b', binary_print},
{'u', unsigned_print}, {'o', octal_print}, {'x', hexadecimal_print},
{'X', hexa_upper_print}, {'p', pointer_print}, {'S', non_printable_print},
{'r', reverse_print}, {'R', rot13string_print}, {'\0', NULL}
};
for (i = 0; fmt_types[i].fmt != '\0'; i++)
if (fmt[*ind] == fmt_types[i].fmt)
return (fmt_types[i].fn(list, buff, flag, width, prec, size));
if (fmt_types[i].fmt == '\0')
{
if (fmt[*ind] == '\0')
return (-1);
length += write(1, "%%", 1);
if (fmt[*ind - 1] == ' ')
length += write(1, " ", 1);
else if (width)
{
--(*ind);
while (fmt[*ind] != ' ' && fmt[*ind] != '%')
--(*ind);
if (fmt[*ind] == ' ')
--(*ind);
return (1);
}
length += write(1, &fmt[*ind], 1);
return (length);
}
return (printed_characters);
}