-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_c_percent_p.c
100 lines (85 loc) · 2.75 KB
/
convert_c_percent_p.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
100
#include "main.h"
unsigned int convert_c(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
unsigned int convert_percent(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
unsigned int convert_p(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
/**
* convert_c - Converts an argument to an unsigned char and
* stores it to a buffer contained in a struct.
* @args: A va_list pointing to the argument to be converted.
* @flags: Flag modifiers.
* @wid: A width modifier.
* @prec: A precision modifier.
* @len: A length modifier.
* @output: A buffer_t struct containing a character array.
*
* Return: The number of bytes stored to the buffer.
*/
unsigned int convert_c(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
char c;
unsigned int ret = 0;
(void)prec;
(void)len;
c = va_arg(args, int);
ret += print_width(output, ret, flags, wid);
ret += _memcpy(output, &c, 1);
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}
/**
* convert_percent - Stores a percent sign to a
* buffer contained in a struct.
* @args: A va_list pointing to the argument to be converted.
* @flags: Flag modifiers.
* @wid: A width modifier.
* @prec: A precision modifier.
* @len: A length modifier.
* @output: A buffer_t struct containing a character array.
*
* Return: The number of bytes stored to the buffer (always 1).
*/
unsigned int convert_percent(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
char percent = '%';
unsigned int ret = 0;
(void)args;
(void)prec;
(void)len;
ret += print_width(output, ret, flags, wid);
ret += _memcpy(output, &percent, 1);
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}
/**
* convert_p - Converts the address of an argument to hex and
* stores it to a buffer contained in a struct.
* @args: A va_list pointing to the argument to be converted.
* @flags: Flag modifiers.
* @wid: A width modifier.
* @prec: A precision modifier.
* @len: A length modifier.
* @output: A buffer_t struct containing a character array.
*
* Return: The number of bytes stored to the buffer.
*/
unsigned int convert_p(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
char *null = "(nil)";
unsigned long int address;
unsigned int ret = 0;
(void)len;
address = va_arg(args, unsigned long int);
if (address == '\0')
return (_memcpy(output, null, 5));
flags |= 32;
ret += convert_ubase(output, address, "0123456789abcdef",
flags, wid, prec);
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}