-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_wandp_bonus.c
120 lines (113 loc) · 3.84 KB
/
format_wandp_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* format_wandp_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: melkholy <melkholy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/25 02:01:46 by melkholy #+# #+# */
/* Updated: 2022/08/27 07:57:39 by melkholy ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf_bonus.h"
void ft_width_andminus(t_print *buff, char *arg, int len)
{
buff->extra = 0;
buff->count = buff->pres;
if (len > buff->pres)
buff->count = len;
if (arg && *arg == '-')
buff->extra ++;
if ((buff->flag2 == '+' && *arg != '-') || buff->flag2 == ' ')
buff->count ++;
else if (buff->flag2 == '#')
buff->count += 2;
if (buff->pres >= len)
buff->count += buff->extra;
while (--len >= buff->extra && \
!(buff->s_pres && !buff->pres && arg[0] == '0'))
buff->str[--buff->count] = arg[len];
if (buff->flag2 == ' ' && *arg != '-')
buff->str[0] = ' ';
else if (buff->flag2 == '+' && *arg != '-')
buff->str[0] = '+';
else if (buff->flag2 == '+' || *arg == '-')
buff->str[0] = '-';
else if (buff->flag2 == '#' && buff->speci == 'x')
buff->str[1] = 'x';
else if (buff->flag2 == '#' && buff->speci == 'X')
buff->str[1] = 'X';
}
void ft_width_and_pres(t_print *buff, char *arg, int len)
{
buff->extra = 0;
buff->count = 0;
if (arg && *arg == '-')
buff->extra ++;
while (--len >= buff->extra && \
!(buff->s_pres && !buff->pres && arg[0] == '0'))
{
buff->str[--buff->width] = arg[len];
buff->pres --;
}
if (len < buff->pres)
buff->count = buff->pres;
if (buff->flag2 == '+' && *arg != '-')
buff->str[buff->width - buff->count - 1] = '+';
else if (buff->flag2 == '+' || *arg == '-')
buff->str[buff->width - buff->count - 1] = '-';
else if (buff->flag2 == '#' && buff->speci == 'x')
buff->str[buff->width - buff->count - 1] = 'x';
else if (buff->flag2 == '#' && buff->speci == 'X')
buff->str[buff->width - buff->count - 1] = 'X';
if (buff->flag2 == '#')
buff->str[buff->width - buff->count - 2] = '0';
}
void ft_width_and_zero(t_print *buff, char *arg, int len)
{
buff->extra = 0;
buff->count = buff->width;
if (*arg == '-')
buff->extra ++;
while (--len >= buff->extra && \
!(buff->s_pres && !buff->pres && arg[0] == '0'))
buff->str[--buff->count] = arg[len];
if (buff->flag2 == ' ' && *arg != '-')
buff->str[0] = ' ';
else if (buff->flag2 == '+' && *arg != '-')
buff->str[0] = '+';
else if (buff->flag2 == '+' || *arg == '-')
buff->str[0] = '-';
else if (buff->flag2 == '#' && buff->speci == 'x')
buff->str[1] = 'x';
else if (buff->flag2 == '#' && buff->speci == 'X')
buff->str[1] = 'X';
}
void ft_fmt_width(t_print *buff, char *arg, int len)
{
if (buff->flag == '0' && !buff->s_pres)
ft_width_and_zero(buff, arg, len);
else if (buff->flag == '-')
ft_width_andminus(buff, arg, len);
else
ft_width_and_pres(buff, arg, len);
}
void ft_fmt_pres(t_print *buff, char *arg, int len)
{
int count;
int str_len;
count = 0;
str_len = ft_strlen(buff->str);
if (*arg == '-')
count ++;
if (buff->flag2 == '#' && buff->speci == 'x')
buff->str[1] = 'x';
else if (buff->flag2 == '#' && buff->speci == 'X')
buff->str[1] = 'X';
else if (buff->flag2 == '+' && *arg != '-')
buff->str[0] = '+';
else if (buff->flag2 == '+' || *arg == '-')
buff->str[0] = '-';
while (--len >= count)
buff->str[--str_len] = arg[len];
}