-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandle_numbers.c
More file actions
222 lines (195 loc) · 5.85 KB
/
handle_numbers.c
File metadata and controls
222 lines (195 loc) · 5.85 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "main.h"
/**
* handle_decimal - convert and append an integer in decimal format to the
* string buffer
* @spec: format specifier information
* @args: arguments list
* @buffer: string buffer to store the result
*
* Return: the number of characters appended to the string buffer
*/
int handle_decimal(const format_specifier *spec, va_list args,
string_buffer *buffer)
{
long int n =
(spec->length == 'l') ? va_arg(args, long int) : va_arg(args, int);
char result[21];
size_t initial_length = buffer->length;
int characters_added, len;
format_specifier *tmp_spec = (format_specifier *)spec;
if (spec->precision)
{
tmp_spec->zero_flag = 1; /* handle precision for signed integers */
tmp_spec->width = (n < 0) ? spec->precision + 1 : spec->precision;
}
_itob(n, result, DEC);
len = _strlen(result);
if (tmp_spec->plus_flag && n >= 0)
{
append_char(buffer, '+'); /* handle the '+' flag */
tmp_spec->width -= spec->plus_flag;
}
else if (tmp_spec->space_flag && n >= 0)
{
append_char(buffer, ' '); /* handle the space flag */
tmp_spec->width -= tmp_spec->space_flag;
}
if (tmp_spec->zero_flag && !tmp_spec->minus_flag)
{
if (n < 0)
return (neg_zero_handler(tmp_spec, result, buffer, len));
handle_width(tmp_spec, buffer, len);
}
if (tmp_spec->width && !tmp_spec->minus_flag)
handle_width(tmp_spec, buffer, len);
if (!(n < 0 && spec->zero_flag) || !(n == 0 && !spec->precision))
append_string(buffer, result);
if (tmp_spec->minus_flag)
handle_width(tmp_spec, buffer, len); /* handle the '-' flag */
characters_added = buffer->length - initial_length;
return (characters_added);
}
/**
* handle_unsigned_int - convert and append an integer in decimal format to the
* string buffer
* @spec: format specifier information
* @args: arguments list
* @buffer: string buffer to store the result
*
* Return: the number of characters appended to the string buffer
*/
int handle_unsigned_int(const format_specifier *spec, va_list args,
string_buffer *buffer)
{
size_t n;
char result[21];
size_t initial_length = buffer->length;
int characters_added, len;
format_specifier *tmp_spec = (format_specifier *)spec;
if (spec->length == 'l')
n = va_arg(args, unsigned long int);
else
n = va_arg(args, unsigned int);
if (spec->precision)
{
tmp_spec->zero_flag = 1; /* handle precision for unsigned integers */
tmp_spec->width = spec->precision;
}
utob(n, result, DEC);
len = _strlen(result);
if (spec->zero_flag && !spec->minus_flag)
handle_width(tmp_spec, buffer, len);
if (spec->width && spec->minus_flag == 0)
handle_width(tmp_spec, buffer, len);
if (!(n == 0 && !spec->precision))
append_string(buffer, result);
else
append_string(buffer, result);
if (spec->minus_flag)
handle_width(tmp_spec, buffer, len);
characters_added = buffer->length - initial_length;
return (characters_added);
}
/**
* handle_binary - convert and append an integer in binary format to the string
* buffer
* @spec: format specifier information (unused)
* @args: arguments list
* @buffer: string buffer to store the result
*
* Return: the number of characters appended to the string buffer
*/
int handle_binary(__attribute__((unused)) const format_specifier *spec,
va_list args, string_buffer *buffer)
{
char result[65];
int characters_added;
unsigned int n = va_arg(args, unsigned int);
size_t initial_length = buffer->length;
utob(n, result, BIN);
append_string(buffer, result);
characters_added = buffer->length - initial_length;
return (characters_added);
}
/**
* handle_octal - convert and append an integer in octal format to the string
* buffer
* @spec: format specifier information
* @args: arguments list
* @buffer: string buffer to store the result
*
* Return: the number of characters appended to the string buffer
*/
int handle_octal(const format_specifier *spec, va_list args,
string_buffer *buffer)
{
size_t n;
char result[23];
size_t initial_length = buffer->length;
int characters_added, len = 0;
format_specifier *tmp_spec = (format_specifier *)spec;
if (spec->length == 'l')
n = va_arg(args, unsigned long int);
else
n = va_arg(args, unsigned int);
if (spec->precision)
{
tmp_spec->zero_flag = 1; /* handle precision for unsigned integers */
tmp_spec->width = spec->precision;
}
utob(n, result, OCT);
len = _strlen(result);
if (spec->width && spec->minus_flag == 0)
{
tmp_spec->width -= spec->sharp_flag;
handle_width(tmp_spec, buffer, len);
}
if (spec->sharp_flag && n > 0)
{
tmp_spec->width -= spec->sharp_flag;
append_char(buffer, '0');
}
if (spec->zero_flag && !spec->minus_flag)
handle_width(tmp_spec, buffer, len);
if (!(n == 0 && !spec->precision))
append_string(buffer, result);
else
append_string(buffer, result);
if (spec->minus_flag)
handle_width(tmp_spec, buffer, len);
characters_added = buffer->length - initial_length;
return (characters_added);
}
/**
* neg_zero_handler - handles the appending of negative numbers
* when the zero flag is used.
* @spec: format specifier information
* @result: the string version of the integer number
* @buffer: string buffer to store the result
* @len: the length of the string @result
*
* Return: the current length of the string buffer
*/
int neg_zero_handler(const format_specifier *spec, char *result,
string_buffer *buffer, int len)
{
char *dup_str;
format_specifier *tmp_spec = (format_specifier *)spec;
dup_str = _strdup(result);
if (dup_str == NULL)
{
return (0); /* memory allocation failed */
}
_reverse_str(dup_str, len);
append_char(buffer, '-'); /* append the negative sign */
/* get the absolute of the number */
dup_str[len - 1] = '\0';
/* pad with the necessary amount of zeros */
handle_width(tmp_spec, buffer, len);
_reverse_str(dup_str, len - 1);
/* append the updated the number */
append_string(buffer, dup_str);
/* deallocate memory*/
safe_free(dup_str);
return (buffer->length);
}