-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_width.c
108 lines (97 loc) · 2.81 KB
/
ft_printf_width.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_width.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomlulu <tomlulu@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/14 14:39:10 by tomlulu #+# #+# */
/* Updated: 2018/01/24 08:19:34 by tmaraval ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_printf_width_dosharp_zero(t_parsed_opt *opt)
{
char *temp;
if (opt->bin_flag & FLG_SHARP && (opt->ch_convert == 'x'
|| opt->ch_convert == 'X'))
{
opt->in_width -= 2;
temp = opt->str_arg;
opt->str_arg = ft_strsub(opt->str_arg, 2, ft_strlen(opt->str_arg));
free(temp);
while ((int)ft_strlen(opt->str_arg) < opt->in_width)
{
temp = opt->str_arg;
opt->str_arg = ft_strjoin("0", opt->str_arg);
free(temp);
}
temp = opt->str_arg;
opt->str_arg = ft_strjoin("0x", opt->str_arg);
free(temp);
}
}
void ft_printf_width_sign(t_parsed_opt *opt)
{
char sign[2];
char *temp;
sign[0] = opt->str_arg[0];
temp = opt->str_arg;
opt->str_arg = ft_strsub(opt->str_arg, 1, ft_strlen(opt->str_arg));
free(temp);
while ((int)ft_strlen(opt->str_arg) + 1 < opt->in_width)
{
temp = opt->str_arg;
opt->str_arg = ft_strjoin("0", opt->str_arg);
free(temp);
}
temp = opt->str_arg;
sign[1] = '\0';
opt->str_arg = ft_strjoin(sign, opt->str_arg);
free(temp);
}
void ft_printf_width_doit(t_parsed_opt *opt)
{
char sign[2];
char *temp;
if (opt->str_arg[0] == '+' || opt->str_arg[0] == '-'
|| opt->str_arg[0] == ' ')
ft_printf_width_sign(opt);
else
{
while ((int)ft_strlen(opt->str_arg) < opt->in_width)
{
temp = opt->str_arg;
opt->str_arg = ft_strjoin("0", opt->str_arg);
free(temp);
}
}
}
void ft_printf_width_dolen0(t_parsed_opt *opt)
{
char *temp;
if (opt->str_arg[0] == '\0' || ft_strlen(opt->str_arg) == 0)
{
free(opt->str_arg);
opt->str_arg = ft_strdup(" ");
while ((int)ft_strlen(opt->str_arg) < opt->in_width)
{
temp = opt->str_arg;
opt->str_arg = ft_strjoin(" ", opt->str_arg);
free(temp);
}
}
else
ft_printf_width_do(opt);
}
void ft_printf_width(t_parsed_opt *opt)
{
char *temp;
if (opt->bin_flag & FLG_ZERO && opt->in_precision == -1)
{
ft_printf_width_dosharp_zero(opt);
ft_printf_width_doit(opt);
}
if (opt->in_width > 0)
ft_printf_width_dolen0(opt);
}