-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_printer_1.c
196 lines (188 loc) · 5.12 KB
/
spec_printer_1.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
/**
* convert_fmt_di - Prints a signed integer
* @args_list: The arguments list
* @fmt_info: The format info
*
* Return: The number of characters written
*/
void convert_fmt_di(va_list *args_list, fmt_info_t *fmt_info)
{
int i, len = 0, zeros_count = 0, num_len;
long num;
char *str, inv_plus;
if (fmt_info->is_long)
num = va_arg(*args_list, long);
else if (fmt_info->is_short)
num = (short)va_arg(*args_list, long);
else
num = va_arg(*args_list, int);
str = long_to_str(num);
if (str)
{
inv_plus = num >= 0 && (fmt_info->show_sign || fmt_info->space) ? 1 : 0;
if (fmt_info->is_precision_set && !fmt_info->prec && !num)
{
print_repeat(' ', fmt_info->width);
}
else
{
num_len = str_len(str) + (inv_plus ? 1 : 0);
if (fmt_info->is_precision_set)
zeros_count = MAX(fmt_info->prec + (inv_plus || num < 0 ? 1 : 0),
num_len) - num_len;
else
zeros_count = fmt_info->pad == '0' ? MAX(fmt_info->width,
num_len) - num_len : 0;
if (fmt_info->is_width_set)
len = (MAX(fmt_info->width, num_len) - num_len) - zeros_count;
for (i = 0; !fmt_info->left && i < len; i++)
_putchar(' ');
if (num < 0 || inv_plus)
_putchar(num < 0 ? '-'
: (fmt_info->space && !fmt_info->show_sign ? ' ' : '+'));
put_num(zeros_count, num, str);
for (i = 0; fmt_info->left && i < len; i++)
_putchar(' ');
}
free(str);
}
}
/**
* convert_fmt_xX - Prints the hexadecimal format of an unsigned int argument
* @args_list: The arguments list
* @fmt_info: The format info
*
* Return: The number of characters written
*/
void convert_fmt_xX(va_list *args_list, fmt_info_t *fmt_info)
{
int i, len = 0, zeros_count = 0, max_w, max_p, num_len;
unsigned long num;
char *str;
if (fmt_info->is_short)
num = (va_arg(*args_list, unsigned long) << 2 * 8) >> 2 * 8;
else if (fmt_info->is_long)
num = va_arg(*args_list, unsigned long);
else
num = va_arg(*args_list, unsigned int);
str = u_long_to_hex(num, fmt_info->spec == 'X');
if (str)
{
if (fmt_info->is_precision_set && !fmt_info->prec && !num)
{
print_repeat(' ', fmt_info->width);
}
else
{
num_len = str_len(str) + (num ? fmt_info->alt * 2 : 0);
max_w = MAX(fmt_info->width, num_len), max_p = MAX(fmt_info->prec, num_len);
zeros_count = (max_p - num_len) * !fmt_info->left;
len = max_w - (NO_NEG(zeros_count) + num_len);
for (i = 0; !fmt_info->left && i < len; i++)
_putchar(fmt_info->pad);
if (fmt_info->alt && num)
_putstr(fmt_info->spec == 'X' ? "0X" : "0x");
for (i = 0; i < zeros_count; i++)
_putchar('0');
for (i = 0; *(str + i) != '\0'; i++)
{
_putchar(*(str + i));
}
for (i = 0; fmt_info->left && i < len; i++)
_putchar(' ');
}
free(str);
}
}
/**
* convert_fmt_o - Prints the octadecimal format of a signed int argument
* @args_list: The arguments list
* @fmt_info: The format info
*
* Return: The number of characters written
*/
void convert_fmt_o(va_list *args_list, fmt_info_t *fmt_info)
{
int i = 0, zeros_count = 0, num_len = 0, len = 0, max_w, max_p;
unsigned long num;
char *str;
if (fmt_info->is_long)
num = va_arg(*args_list, unsigned long);
else if (fmt_info->is_short)
num = (va_arg(*args_list, unsigned long) << 2 * 8) >> 2 * 8;
else
num = va_arg(*args_list, unsigned int);
str = long_to_oct(num);
if (str)
{
if (fmt_info->is_precision_set && !fmt_info->prec && !num)
{
print_repeat(' ', fmt_info->width);
}
else
{
num_len = NO_LESS(str_len(str), 1);
max_w = MAX(fmt_info->width, num_len);
max_p = MAX(fmt_info->prec, num_len);
zeros_count = (max_p - num_len) * !fmt_info->left;
len = max_w - (NO_NEG(zeros_count) + num_len);
for (i = 0; !fmt_info->left && i < len; i++)
_putchar(fmt_info->pad);
if (fmt_info->alt && zeros_count == 0 && num)
_putchar('0');
for (i = 0; i < zeros_count; i++)
_putchar('0');
for (i = 0; *(str + i) != '\0'; i++)
_putchar(*(str + i));
for (i = 0; fmt_info->left && i < len; i++)
_putchar(' ');
}
free(str);
}
}
/**
* convert_fmt_u - Prints an unsigned integer
* @args_list: The arguments list
* @fmt_info: The format info
*/
void convert_fmt_u(va_list *args_list, fmt_info_t *fmt_info)
{
int i, len = 0, zeros_count = 0, num_len;
unsigned long num, max_w, max_p;
char *str;
if (fmt_info->is_long)
num = va_arg(*args_list, unsigned long);
else if (fmt_info->is_short)
num = (va_arg(*args_list, unsigned int) << 2 * 8) >> 2 * 8;
else
num = va_arg(*args_list, unsigned int);
str = u_long_to_str(num);
if (str)
{
if (FMT_PREC_EMPTY(fmt_info) && !num)
{
print_repeat(' ', fmt_info->width);
}
else
{
num_len = str_len(str);
max_w = MAX(fmt_info->width, num_len);
max_p = MAX(fmt_info->prec, num_len);
zeros_count = (max_p - num_len) * !fmt_info->left;
len = max_w - (NO_NEG(zeros_count) + num_len);
for (i = 0; !fmt_info->left && i < len; i++)
_putchar(fmt_info->pad);
for (i = 0; i < zeros_count; i++)
_putchar('0');
for (i = 0; *(str + i) != '\0'; i++)
_putchar(*(str + i));
for (i = 0; fmt_info->left && i < len; i++)
_putchar(' ');
}
free(str);
}
}