-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_width_num.c
123 lines (112 loc) · 2.79 KB
/
ft_width_num.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_width_num.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mlegeay <mlegeay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/05/18 05:44:32 by mlegeay #+# #+# */
/* Updated: 2017/05/30 06:49:16 by mlegeay ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_precision_wo_width(t_list *f)
{
int len;
int prec;
prec = f->flags[0];
len = ft_strlen(f->result);
ft_space_plus_sharp(f);
while (prec > len-- && len > 0)
prec--;
if (f->flags[0] > f->flags[1] + ft_strlen(f->result))
{
while (++len < prec)
ft_buf('0', f);
}
}
void ft_precision_numb(t_list *f, int len_zero)
{
int len;
len = ft_strlen(f->result);
if (f->flags[5] == 1 || f->flags[6] == 1 || f->sign == '-'
|| (f->flags[2] == 1 && f->flags[0] <= len && (f->spe == 'o'
|| f->spe == 'O')))
len_zero--;
if (f->flags[2] == 1 && (f->spe == 'x' || f->spe == 'X'
|| f->spe == 'p'))
len_zero = len_zero - 2;
while (f->flags[0] > len++)
len_zero--;
if (f->flags[0] < f->flags[1])
{
while (len_zero-- > 0)
{
if (f->flags[3] == 1 && f->flags[0] == 0)
ft_buf('0', f);
else
ft_buf(' ', f);
}
}
}
void ft_zero_numb(t_list *f)
{
int i;
int len;
int len_zero;
i = -1;
len = ft_strlen(f->result);
len_zero = f->flags[1] - len;
if (f->flags[3] == 1)
{
ft_space_plus_sharp(f);
ft_precision_numb(f, len_zero);
}
else
{
ft_precision_numb(f, len_zero);
ft_space_plus_sharp(f);
}
while (f->flags[0] > len++)
ft_buf('0', f);
while (f->result[++i] != '\0')
ft_buf(f->result[i], f);
}
void ft_minus_numb(t_list *f)
{
int i;
int len;
int len_zero;
i = -1;
len_zero = f->flags[1] - ft_strlen(f->result);
len = ft_strlen(f->result);
f->flags[3] = 0;
ft_space_plus_sharp(f);
while (f->flags[0] > len++)
ft_buf('0', f);
while (f->result[++i] != '\0')
ft_buf(f->result[i], f);
ft_precision_numb(f, len_zero);
}
void ft_width_numb(t_list *f)
{
int i;
int len;
i = -1;
len = ft_strlen(f->result);
if (len < f->flags[1])
{
if (f->flags[4] == 1)
ft_minus_numb(f);
else
ft_zero_numb(f);
}
else
{
ft_space_plus_sharp(f);
while (f->flags[0] > len++)
ft_buf('0', f);
while (f->result[++i] != '\0')
ft_buf(f->result[i], f);
}
}