-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdec_utils_bonus.c
119 lines (108 loc) · 3.3 KB
/
dec_utils_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dec_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gmachado <gmachado@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/15 23:29:40 by gmachado #+# #+# */
/* Updated: 2022/05/29 22:14:30 by gmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf_bonus.h"
int write_dec_left_justified(char *s, int len, t_format *format, int num_zeros)
{
int num_spaces;
int copied;
copied = 0;
num_spaces = format->width - num_zeros - len;
if (format->flags & (MINUS_PREFIX | PLUS_PREFIX | SPACE_PREFIX))
num_spaces--;
if (format->flags & (MINUS_PREFIX))
copied = write(1, "-", 1);
else if (format->flags & (PLUS_PREFIX))
copied = write(1, "+", 1);
else if (format->flags & (SPACE_PREFIX))
copied = write(1, " ", 1);
copied += write_repeated('0', num_zeros);
copied += write(1, s, len);
return (copied + write_repeated(' ', num_spaces));
}
int write_dec_right_justified(char *s, int len, t_format *format, int num_zeros)
{
int num_spaces;
int copied;
num_spaces = format->width - num_zeros - len;
if (format->flags & (MINUS_PREFIX | PLUS_PREFIX | SPACE_PREFIX))
num_spaces--;
copied = write_repeated(' ', num_spaces);
if (format->flags & (MINUS_PREFIX))
copied += write(1, "-", 1);
else if (format->flags & (PLUS_PREFIX))
copied += write(1, "+", 1);
else if (format->flags & (SPACE_PREFIX))
copied += write(1, " ", 1);
copied += write_repeated('0', num_zeros);
return (copied + write(1, s, len));
}
int write_dec_padded(char *s, int len, t_format *format)
{
int num_zeros;
if ((format->flags & PRECISION_SET))
num_zeros = format->precision - len;
else if ((format->flags & PAD_WITH_ZEROS)
&& !(format->flags & JUSTIFY_LEFT))
{
num_zeros = format->width - len;
if (format->flags & (MINUS_PREFIX | PLUS_PREFIX))
num_zeros--;
}
else
num_zeros = 0;
if (format->flags & JUSTIFY_LEFT)
return (write_dec_left_justified(s, len, format, max(num_zeros, 0)));
else
return (write_dec_right_justified(s, len, format, max(num_zeros, 0)));
}
int putnbr_dec_int(int nbr, t_format *format)
{
char buf[10];
int pos;
pos = 10;
if (nbr == 0)
{
buf[--pos] = '0';
if (format->flags & PRECISION_SET && format->precision == 0)
pos = 10;
}
else
{
while (nbr != 0)
{
buf[--pos] = '0' - (unsigned char)(nbr % 10);
nbr /= 10;
}
}
return (write_dec_padded(buf + pos, 10 - pos, format));
}
int putnbr_dec_uint(unsigned int nbr, t_format *format)
{
char buf[10];
int pos;
pos = 10;
if (nbr == 0)
{
buf[--pos] = '0';
if (format->flags & PRECISION_SET && format->precision == 0)
pos = 10;
}
else
{
while (nbr != 0)
{
buf[--pos] = '0' + (unsigned char)(nbr % 10);
nbr /= 10;
}
}
return (write_dec_padded(buf + pos, 10 - pos, format));
}