-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_format.c
86 lines (84 loc) · 1.73 KB
/
handle_format.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
#include "main.h"
/**
* formatCases - have a bunch of cases to print
* @format: character after %
* @args: list of parameters to print
* Return: amount of printed bytes
**/
int formatCases(const char *format, va_list args)
{
char let;
int res = 0;
switch (*format)
{
case 'c':
let = va_arg(args, int);
write(1, &let, 1);
break;
case 's': case 'r': case 'R':
res += print_str(args, *format) - 1;
break;
case '%':
write(1, format, 1);
break;
case 'd': case 'i':
res += print_integer(args, *format) - 1;
break;
case 'b':
res += print_binary(args) - 1;
break;
case 'u': case 'o': case 'x': case 'X':
res += print_unsigned_integer(args, *format) - 1;
break;
case 'p':
res += print_address(args) - 1;
break;
case 'S':
res += print_custom_str(args) - 1;
break;
default:
write(1, format - 1, 2);
format = skp_space_percent(format), res++;
}
return (res);
}
/**
* handle_format - compares the format given with a bunch of cases to print
* @args: list of parameters
* @format: input given
* Return: amount of printed bytes
**/
int handle_format(va_list args, const char *format)
{
int res = 0;
while (*format)
{
if (*format == '%')
{
if (!*(++format))
return (-1);
else if (*format == '+' || *format == ' ')
{
if (*(format + 1) == 'd' || *(format + 1) == 'i')
{
res += print_integer(args, *format) - 1;
format += 1;
}
}
else if (*format == '#')
{
if (*(format + 1) == 'o' || *(format + 1) == 'x' || *(format + 1) == 'X')
{
res += handle_unsigned_integer(args, *(format + 1)) - 1;
format += 1;
}
}
else
res += formatCases(format, args);
}
else
write(1, format, 1);
++format, ++res;
}
return (res);
}