-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_handler.c
99 lines (87 loc) · 2.11 KB
/
format_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "main.h"
/**
* format_handler - checks format specifier and executes the right functions
* @args: the argument to be formatted
* @format: the format string
* @fmt_i: index of the format string
* @buf: pointer to character buffer
* @buf_i: pointer to current index of the buffer
*
* Return: the number of characters printed, -1 if format doesn't match
*/
int format_handler(va_list args, const char *const format,
int fmt_i, char *const buf, int *const buf_i)
{
long int b, i_cpy = *buf_i;
int nob = 0;
f_w_p *edits = malloc(sizeof(*edits) * 4);
c_sp fmts[] = {{'c', print_c},
{'s', print_s},
{'%', print_pc},
{'d', print_Rnum},
{'i', print_Rnum},
{'b', print_b},
{'o', print_oct},
{'u', print_dec},
{'x', print_lower_hexa},
{'X', print_upper_hexa},
{'p', print_p},
{'\0', NULL}};
if (edits == NULL)
return (-1);
for (b = 0; fmts[b].ch; b++)
{
if (format[fmt_i] == fmts[b].ch)
{
nob += fmts[b].f(args, buf, buf_i);
break;
}
else
fwp(format[fmt_i], -1, edits, buf, buf_i);
}
nob += fwp('G', (*buf_i - i_cpy), edits, buf, buf_i);
free(edits);
if (fmts[b].ch == '\0')
return (-1);
return (nob);
}
/**
* fwp - handles the flags, width, precision and length modifiers
* @mod: modifier specifier
* @num_len: number of digits in the number
* @edits: pointer to an array of structures
* @buf: the buffer
* @buf_i: current buffer index
*
* Return: number of bytes printed if any
*/
int fwp(int mod, int num_len, f_w_p *edits, char *const buf,
int *const buf_i)
{
int nob = 0, b;
edits[0].gate = false;
edits[0].ch = ' ';
edits[0].f = print_plus;
edits[1].gate = false;
edits[1].ch = '+';
edits[1].f = print_plus;
edits[2].gate = false;
edits[2].ch = '#';
edits[2].f = print_prefix;
edits[3].gate = false;
edits[3].ch = '\0';
edits[3].f = NULL;
if (num_len < 0 && mod != 'G')
{
for (b = 0; edits[b].ch; b++)
if (mod == edits[b].ch)
edits[b].gate = true;
}
else
{
for (b = 0; edits[b].ch; b++)
if (edits[b].gate)
return (edits[b].f(num_len, buf, buf_i, mod));
}
return (nob);
}