-
Notifications
You must be signed in to change notification settings - Fork 0
/
dioux.c
135 lines (124 loc) · 3.55 KB
/
dioux.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dioux.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aimelda <aimelda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 13:39:29 by aimelda #+# #+# */
/* Updated: 2020/03/09 19:42:04 by aimelda ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int get_length(t_printf *cur, char base, unsigned long long *nb)
{
int len;
unsigned long long n;
n = *(unsigned long long*)cur->content;
if (!cur->precision && !n && cur->precision_ast > -1)
return (0);
*nb = 1;
len = 1;
while (n /= base)
{
len++;
*nb *= base;
}
return (len);
}
static void put_string(t_printf *cur, unsigned long long nb, char base, int len)
{
char tmp;
char c;
unsigned long long n;
while (cur->precision-- > len)
ft_putchar('0');
n = *(unsigned long long*)cur->content;
c = base - 81;
if (base > 10)
base = 16;
if (len && !n)
ft_putchar('0');
else if (len)
while (nb)
{
tmp = n / nb + '0';
if (tmp > '9')
tmp += c;
ft_putchar(tmp);
n %= nb;
nb /= base;
}
free(cur->content);
}
int to_signed_dec(t_printf *cur, char base)
{
int len;
int tmp;
unsigned long long nb;
if (*(long long*)cur->content < 0)
cur->sign = '-';
*(long long*)cur->content = ft_abs(*(long long*)cur->content);
len = get_length(cur, base, &nb);
tmp = ft_max(len, cur->precision) + (cur->sign > 0);
if (cur->zero == '0' && cur->sign)
ft_putchar(cur->sign);
if (!(cur->left_adjusted))
while (cur->width > tmp++)
ft_putchar(cur->zero);
if (cur->zero == ' ' && cur->sign)
ft_putchar(cur->sign);
put_string(cur, nb, base, len);
if (cur->left_adjusted)
while (cur->width > tmp++)
ft_putchar(cur->zero);
return (tmp - 1);
}
int to_unsigned_num(t_printf *cur, char base)
{
int len;
int tmp;
unsigned long long nb;
len = get_length(cur, base, &nb);
tmp = ft_max(len, cur->precision);
if (base == 8)
tmp += cur->sharp * ((cur->precision <= len
&& *(long long*)cur->content) || !len);
if (!(cur->left_adjusted))
while (cur->width > tmp++)
ft_putchar(cur->zero);
if (base == 8 && cur->sharp && ((cur->precision <= len
&& *(long long*)cur->content) || !len))
ft_putchar('0');
put_string(cur, nb, base, len);
if (cur->left_adjusted)
while (cur->width > tmp++)
ft_putchar(cur->zero);
return (tmp - 1);
}
int to_unsigned_hex(t_printf *cur, unsigned long long n, char base)
{
int len;
unsigned long long nb;
int t;
len = get_length(cur, base, &nb);
t = ft_max(len, cur->precision) + 2 * (cur->sharp < 0 || (cur->sharp && n));
if (cur->zero == '0' && ((cur->sharp && n) || cur->sharp < 0))
{
ft_putchar('0');
ft_putchar(cur->argt);
}
if (!(cur->left_adjusted))
while (cur->width > t++)
ft_putchar(cur->zero);
if (cur->zero == ' ' && ((cur->sharp && n) || cur->sharp < 0))
{
ft_putchar('0');
ft_putchar(cur->argt);
}
put_string(cur, nb, cur->argt, len);
if (cur->left_adjusted)
while (cur->width > t++)
ft_putchar(cur->zero);
return (t - 1);
}