Repository navigation
Expand file tree
/
Copy pathconvert_misc.c
More file actions
131 lines (109 loc) · 3.09 KB
/
Copy pathconvert_misc.c
File metadata and controls
131 lines (109 loc) · 3.09 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* File: convert_misc.c
* Auth: Brennan D Baraban
*/
#include "holberton.h"
unsigned int convert_c(va_list args, buffer_t *output,
unsigned char flags, char wid, char prec, unsigned char len);
unsigned int convert_percent(va_list args, buffer_t *output,
unsigned char flags, char wid, char prec, unsigned char len);
unsigned int convert_p(va_list args, buffer_t *output,
unsigned char flags, char wid, char 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, char wid, char prec, unsigned char len)
{
char c, width = ' ';
unsigned int ret = 0;
c = va_arg(args, int);
(void)prec;
(void)len;
if (NEG_FLAG == 0)
{
for (; wid > 1; wid--)
ret += _memcpy(output, &width, 1);
}
ret += _memcpy(output, &c, 1);
if (NEG_FLAG == 1)
{
for (wid -= ret; wid > 0; wid--)
ret += _memcpy(output, &width, 1);
}
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, char wid, char prec, unsigned char len)
{
char percent = '%', width = ' ';
unsigned int ret = 0;
(void)args;
(void)prec;
(void)len;
if (NEG_FLAG == 0)
{
for (; wid > 1; wid--)
ret += _memcpy(output, &width, 1);
}
ret += _memcpy(output, &percent, 1);
if (NEG_FLAG == 1)
{
for (wid -= ret; wid > 1; wid--)
ret += _memcpy(output, &width, 1);
}
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, char wid, char prec, unsigned char len)
{
char *null = "(nil)", width = ' ';
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);
if (NEG_FLAG == 1)
{
for (wid -= ret; wid > 0; wid--)
ret += _memcpy(output, &width, 1);
}
return (ret);
}