-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_putflags.c
132 lines (122 loc) · 3.11 KB
/
ft_printf_putflags.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
124
125
126
127
128
129
130
131
132
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_putflags.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mfrisby <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/07/11 13:13:48 by mfrisby #+# #+# */
/* Updated: 2017/07/11 13:28:19 by mfrisby ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static char *getprecision(t_env *e, char *s, size_t len)
{
char tmp[e->precision - len];
int i;
char *ret;
i = 0;
ret = NULL;
if (e->type != 'c' && e->type != 'C' && e->type != 'S' && e->type != 's'
&& e->precision > (int)len)
{
while (i < e->precision - (int)len)
{
tmp[i] = '0';
i++;
}
tmp[i] = '\0';
if (!s)
ret = ft_strjoin(tmp, "");
else
ret = ft_strjoin(tmp, s);
ft_strdel(&s);
return (ret);
}
return (s);
}
static char *getfield(t_env *e, char *field, int len)
{
char c;
int i;
i = 0;
c = e->flag_zero == 1 ? '0' : ' ';
if (e->field_width > len)
{
field = ft_strnew(e->field_width - len);
while (i < e->field_width - len)
{
field[i] = c;
i++;
}
}
if (field == NULL)
return (ft_strdup(""));
return (field);
}
static char *getdp(t_env *e, char *dp, char *s)
{
dp = ft_strnew(2);
if ((e->flag_diese == 1 && (e->type == 'x' || e->type == 'X'))
|| e->type == 'p')
{
dp[0] = '0';
if (e->type == 'x' || e->type == 'p')
dp[1] = 'x';
else
dp[1] = 'X';
}
else if (e->flag_diese == 1 && (e->type == 'o' || e->type == 'O')
&& s[0] != '0')
dp[0] = '0';
else if (e->type == 'd' || e->type == 'i' || e->type == 'o')
{
if (e->neg)
dp[0] = '-';
else if (e->flag_plus)
dp[0] = '+';
}
return (dp);
}
static void addtobuffer(t_env *e, char *s, char *dp, char *space)
{
char *field;
char *tmp;
char *tmp1;
tmp = NULL;
tmp1 = NULL;
field = NULL;
field = getfield(e, field, ft_strlen(s) + ft_strlen(dp));
if (e->flag_moins)
tmp = ft_strjoin(s, field);
else
tmp = ft_strjoin((!ft_strlen(field)
|| field[0] == '0' ? field : dp), s);
tmp1 = ft_strjoin((e->flag_moins || (!ft_strlen(field) || field[0] == '0')
? dp : field), tmp);
ft_printf_add_to_buffer(e, space, 0);
ft_printf_add_to_buffer(e, tmp1, 0);
ft_strdel(&tmp);
ft_strdel(&tmp1);
ft_strdel(&field);
ft_strdel(&dp);
ft_strdel(&space);
ft_strdel(&s);
}
void ft_printf_putflags(t_env *e, char *s)
{
char *dp;
char *space;
char *ret;
size_t len;
ret = NULL;
len = ft_strlen(s);
space = NULL;
dp = NULL;
space = getspace(e, space, s);
ret = getprecision(e, s, len);
if (ret == NULL)
ret = ft_strdup("");
dp = getdp(e, dp, ret);
addtobuffer(e, ret, dp, space);
}