-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply_margin.c
48 lines (45 loc) · 1.84 KB
/
apply_margin.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* width.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: monoue <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/06 16:06:58 by monoue #+# #+# */
/* Updated: 2020/08/20 12:34:21 by monoue ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void fill_empty_width(char filler, char ***new_target,
int empty_width, t_format_info *info)
{
if (info->minus)
{
while (**new_target && empty_width--)
**new_target = ft_strjoin_free_both(**new_target,
ft_ctoa_malloc(filler));
}
else
{
while (**new_target && empty_width--)
**new_target = ft_strjoin_free_both(ft_ctoa_malloc(filler),
**new_target);
}
}
void apply_margin(char **new_target, int empty_width,
t_format_info *info)
{
if (info->minus)
fill_empty_width(' ', &new_target, empty_width, info);
else if (info->zero && info->precision == NOT_SET && *new_target[0]
&& *new_target[0] == '-')
{
*new_target[0] = '0';
fill_empty_width('0', &new_target, empty_width, info);
*new_target[0] = '-';
}
else if (info->precision != NOT_SET || info->zero == false)
fill_empty_width(' ', &new_target, empty_width, info);
else
fill_empty_width('0', &new_target, empty_width, info);
}