-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_struct_print.c
92 lines (84 loc) · 2.8 KB
/
init_struct_print.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_struct_print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rnishimo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/09 09:50:20 by rnishimo #+# #+# */
/* Updated: 2022/01/29 21:47:48 by rnishimo ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static size_t _get_size_of_space(
t_print *st_print,
t_str *st_str,
t_flag *st_flag
)
{
size_t space_size;
size_t other_size;
(void)st_str;
space_size = 0;
other_size = st_print->size;
other_size += st_print->sign;
other_size += st_print->hex;
other_size += st_print->zero;
if (st_flag->width > other_size)
space_size += st_flag->width - other_size;
return (space_size);
}
static size_t _get_size_of_zero(
t_print *st_print,
t_str *st_str,
t_flag *st_flag
)
{
size_t zero_size;
size_t other_size;
if (st_flag->minus)
return (0);
zero_size = 0;
other_size = st_print->size;
other_size += st_print->sign;
other_size += st_print->hex;
if (st_flag->precision && ft_strchr("dipuxX", st_str->specifier)
&& st_flag->precision > st_str->size)
zero_size = st_flag->precision - st_str->size;
if (st_flag->zero
&& st_flag->width > other_size
&& zero_size < st_flag->width - other_size)
zero_size = st_flag->width - other_size;
return (zero_size);
}
static size_t _get_base_size(t_str *st_str, t_flag *st_flag)
{
if (st_flag->dot
&& st_str->specifier == 's'
&& st_str->size > st_flag->precision)
return (st_flag->precision);
if (st_str->zero && st_flag->dot && st_flag->precision == 0)
return (0);
return (st_str->size);
}
t_print init_struct_print(t_str *st_str, t_flag *st_flag)
{
t_print st_print;
size_t print_size;
(void)print_size;
ft_memset(&st_print, 0, sizeof(t_print));
st_print.size = _get_base_size(st_str, st_flag);
if (st_str->minus
|| (ft_strchr("di", st_str->specifier) && st_flag->space)
|| (ft_strchr("di", st_str->specifier) && st_flag->plus))
st_print.sign = 1;
if (st_str->specifier == 'p')
st_print.hex = 2;
else if (st_flag->sharp
&& ft_strchr("xX", st_str->specifier)
&& st_str->str[0] != '0')
st_print.hex = 2;
st_print.zero = _get_size_of_zero(&st_print, st_str, st_flag);
st_print.space = _get_size_of_space(&st_print, st_str, st_flag);
return (st_print);
}