-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_2.c
128 lines (119 loc) · 3.52 KB
/
print_2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vportell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/19 14:15:19 by vportell #+# #+# */
/* Updated: 2016/12/24 03:27:58 by vportell ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void print_bin(char **s, t_format *format, unsigned long arg)
{
char *t;
char *p;
char *k;
*s = ft_ltoa_base(arg, 2, "01");
format_setup_1(s, format, arg, &k);
format_setup_2(*s, format, &p, &k);
t = ft_strlen(p) ? ft_strjoin(p, *s) : ft_strjoin(*s, p);
ft_strdel(&p);
ft_strdel(s);
if (format->flag->plus && ft_strlen(k) > 0)
{
p = ft_strsub(k, 1, ft_strlen(k) - 1);
ft_strdel(&k);
k = ft_strdup(p);
}
*s = format->flag->minus ? ft_strjoin(t, k) : ft_strjoin(k, t);
ft_strdel(&p);
ft_strdel(&t);
ft_strdel(&k);
}
void dec_assist(char **s, t_format *format, char **p, char **k)
{
ft_strdel(p);
ft_strdel(k);
if (format->flag->space && !format->flag->plus &&
(int)ft_strlen(*s) == format->min_width && format->precision == -1)
{
*p = ft_strsub(*s, 1, ft_strlen(*s) - 1);
ft_strdel(s);
*s = ft_strjoin(" ", *p);
ft_strdel(p);
}
}
void print_dec(char **s, t_format *format, long arg)
{
int i;
char *t;
char *p;
char *k;
*s = utoa(arg);
format_setup_1(s, format, arg, &k);
format_setup_2(*s, format, &p, &k);
t = ft_strlen(p) ? ft_strjoin(p, *s) : ft_strjoin(*s, p);
i = k[0] == '0' ? 0 : 1;
i == 1 ? format_sign(&t, format, arg) : 0;
format_space(&t, format, k);
ft_strdel(&p);
ft_strdel(s);
if ((format->flag->plus || arg < 0) && ft_strlen(k) > 0)
{
p = ft_strsub(k, 1, ft_strlen(k) - 1);
ft_strdel(&k);
k = ft_strdup(p);
}
*s = format->flag->minus ? ft_strjoin(t, k) : ft_strjoin(k, t);
i == 0 ? format_sign(s, format, arg) : 0;
ft_strdel(&t);
dec_assist(s, format, &p, &k);
}
int hex_assist(char **t, char **p, char **k, t_format *format)
{
*t = ft_strlen(*k) > 1 ?
ft_strsub(*k, 2, ft_strlen(*k) - 2) : ft_strdup("");
ft_strdel(k);
if (format->flag->zero && !format->flag->minus &&
format->precision == -1)
{
*k = ft_strdup(*p);
ft_strdel(p);
*p = ft_strdup(*t);
ft_strdel(t);
*t = ft_strdup(*k);
ft_strdel(k);
}
*k = format->type == 11 ? ft_strjoin("0x", *p) : ft_strjoin("0X", *p);
ft_strdel(p);
return (1);
}
void print_hex(char **s, t_format *format, unsigned long arg)
{
char *t;
char *p;
char *k;
if (format->type == 11)
*s = ft_ltoa_base(arg, 16, "0123456789abcdef");
else
*s = ft_ltoa_base(arg, 16, "0123456789ABCDEF");
format_setup_1(s, format, arg, &k);
format_setup_2(*s, format, &p, &k);
if (format->flag->hash && arg && hex_assist(&t, &p, &k, format))
{
p = ft_strjoin(k, *s);
ft_strdel(s);
*s = format->flag->minus ? ft_strjoin(p, t) : ft_strjoin(t, p);
}
else
{
t = ft_strlen(p) ? ft_strjoin(p, *s) : ft_strjoin(*s, p);
ft_strdel(s);
*s = format->flag->minus ? ft_strjoin(t, k) : ft_strjoin(k, t);
}
ft_strdel(&p);
ft_strdel(&t);
ft_strdel(&k);
}