-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_numbers.c
178 lines (152 loc) · 4.93 KB
/
convert_numbers.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "main.h"
unsigned int convert_di(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
unsigned int convert_b(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
unsigned int convert_u(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
unsigned int convert_o(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
/**
* convert_di - Converts an argument to a signed int 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_di(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
long int d, copy;
unsigned int ret = 0, count = 0;
char pad, space = ' ', neg = '-', plus = '+';
if (len == LONG)
d = va_arg(args, long int);
else
d = va_arg(args, int);
if (len == SHORT)
d = (short)d;
/* Handle space flag */
if (SPACE_FLAG == 1 && d >= 0)
ret += _memcpy(output, &space, 1);
if (prec <= 0 && NEG_FLAG == 0) /* Handle width */
{
if (d == LONG_MIN)
count += 19;
else
{
for (copy = (d < 0) ? -d : d; copy > 0; copy /= 10)
count++;
}
count += (d == 0) ? 1 : 0;
count += (d < 0) ? 1 : 0;
count += (PLUS_FLAG == 1 && d >= 0) ? 1 : 0;
count += (SPACE_FLAG == 1 && d >= 0) ? 1 : 0;
/* Handle plus flag when zero flag is active */
if (ZERO_FLAG == 1 && PLUS_FLAG == 1 && d >= 0)
ret += _memcpy(output, &plus, 1);
/*Print negative sign when zero flag is active */
if (ZERO_FLAG == 1 && d < 0)
ret += _memcpy(output, &neg, 1);
pad = (ZERO_FLAG == 1) ? '0' : ' ';
for (wid -= count; wid > 0; wid--)
ret += _memcpy(output, &pad, 1);
}
/* Print negative sign when zero flag is not active */
if (ZERO_FLAG == 0 && d < 0)
ret += _memcpy(output, &neg, 1);
/* Handle plus flag when zero flag is not active */
if (ZERO_FLAG == 0 && (PLUS_FLAG == 1 && d >= 0))
ret += _memcpy(output, &plus, 1);
if (!(d == 0 && prec == 0))
ret += convert_sbase(output, d, "0123456789",
flags, 0, prec);
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}
/**
* convert_b - Converts an unsigned int argument to binary
* 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_b(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
unsigned int num;
num = va_arg(args, unsigned int);
(void)len;
return (convert_ubase(output, num, "01", flags, wid, prec));
}
/**
* convert_o - Converts an unsigned int to octal and
* stores it to a buffer contained in a struct.
* @args: A va_list poinitng 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_o(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
unsigned long int num;
unsigned int ret = 0;
char zero = '0';
if (len == LONG)
num = va_arg(args, unsigned long int);
else
num = va_arg(args, unsigned int);
if (len == SHORT)
num = (unsigned short)num;
if (HASH_FLAG == 1 && num != 0)
ret += _memcpy(output, &zero, 1);
if (!(num == 0 && prec == 0))
ret += convert_ubase(output, num, "01234567",
flags, wid, prec);
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}
/**
* convert_u - Converts an unsigned int argument to decimal 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_u(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
unsigned long int num;
unsigned int ret = 0;
if (len == LONG)
num = va_arg(args, unsigned long int);
else
num = va_arg(args, unsigned int);
if (len == SHORT)
num = (unsigned short)num;
if (!(num == 0 && prec == 0))
ret += convert_ubase(output, num, "0123456789",
flags, wid, prec);
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}