-
Notifications
You must be signed in to change notification settings - Fork 0
/
put_result.c
99 lines (89 loc) · 2.71 KB
/
put_result.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* put_result.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: monoue <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/28 17:07:49 by monoue #+# #+# */
/* Updated: 2020/08/20 12:37:39 by monoue ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void put_c_null_case(char *output)
{
size_t index;
const size_t output_len = ft_strlen(output);
index = 0;
while (index < output_len)
{
if (output[index] == '1')
ft_putchar('\0');
else
ft_putchar(' ');
index++;
}
}
static char *cut_by_precision(char *output, int prec)
{
char *cut_output;
if (!output)
return (NULL);
cut_output = ft_substr(output, 0, prec);
SAFE_FREE(output);
return (cut_output);
}
static char *free_and_strdup_null(char *new_target)
{
SAFE_FREE(new_target);
return (ft_strdup(""));
}
static char *apply_precision(char *output, t_format_info *info)
{
int prec;
int margin;
prec = info->precision;
if (prec == NOT_SET)
return (output);
if (ft_strchr("diuxXp", info->conv_c) && prec == 0 && output[0] == '0')
return (free_and_strdup_null(output));
margin = prec - ft_strlen(output);
if (ft_strchr("cs", info->conv_c) && margin <= 0)
return (cut_by_precision(output, prec));
if (ft_strchr("diup", info->conv_c) && margin >= 0)
{
if (output[0] == '-')
{
output[0] = '0';
fill_output_with_zero(&output, margin + 1);
output[0] = '-';
}
else
fill_output_with_zero(&output, margin);
}
else if (ft_strchr("xX", info->conv_c) && margin > 0)
fill_empty_with_zero(&output, margin, info);
return (output);
}
int put_result(t_format_info *info)
{
char *output;
size_t output_len;
int margin;
output = ft_strdup(info->value);
output = apply_precision(output, info);
if (info->conv_c == 'p')
output = ft_strjoin_free_both(ft_strdup("0x"), output);
margin = info->min_width - (int)ft_strlen(output);
if (margin > 0)
apply_margin(&output, margin, info);
output_len = ft_strlen(output);
if (info->c_null == true)
put_c_null_case(output);
else
ft_putstr(output);
SAFE_FREE(output);
SAFE_FREE(info->value);
SAFE_FREE(info);
return (output_len);
}