This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_parse_token.c
153 lines (143 loc) · 4.19 KB
/
ft_parse_token.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_token.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minjungk <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/27 04:50:52 by minjungk #+# #+# */
/* Updated: 2024/06/13 04:53:12 by minjungk ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf_util.h"
extern char *ft_utoa(unsigned long n, const char *base);
static void make_out(t_token *t, char *copy, int copy_len)
{
if (t->type != 's' && copy && t->precision < copy_len)
t->precision = copy_len;
t->precision += 2 * (1 && (t->opt & FOUND));
t->precision += 1 * (1 && (t->opt & (PLUS | BLANK)));
if (t->width < t->precision)
t->width = t->precision;
t->out = ft_calloc(t->width + 1, sizeof(char));
if (t->out == NULL)
return ;
ft_memset(t->out, ' ', t->width);
if ((t->opt & ZERO) && !(t->opt & PREC))
t->precision = t->width;
if (t->opt & MINUS)
ft_memset(t->out, '0', t->precision);
else
ft_memset(t->out + t->width - t->precision, '0', t->precision);
if (copy == NULL)
return ;
if (t->type == 's')
copy_len = t->precision;
if (t->opt & MINUS)
ft_memcpy(t->out + t->precision - copy_len, copy, copy_len);
else
ft_memcpy(t->out + t->width - copy_len, copy, copy_len);
}
static int parse_text(t_token *t, va_list ap)
{
char *s;
if (t->type == '%')
make_out(t, "%", 1);
else if (t->type == 'c')
{
make_out(t, " ", 1);
if (t->out == NULL)
return (-1);
if (t->opt & MINUS)
t->out[0] = va_arg(ap, int);
else
t->out[t->width - 1] = va_arg(ap, int);
}
else if (t->type == 's')
{
s = va_arg(ap, char *);
if (s == NULL)
s = "(null)";
if ((t->opt & PREC) == 0 || t->precision >= (int)ft_strlen(s))
t->precision = ft_strlen(s);
if (ft_strlen(s) <= 2147483647)
make_out(t, s, ft_strlen(s));
}
return (0);
}
static int parse_number(t_token *t, char *s, char flag)
{
if (s == NULL)
return (-1);
if (s[0] == '-')
t->opt |= BLANK;
if (s[0] == '0' && (t->opt & PREC) && (t->precision < 1))
make_out(t, "", 0);
else if (s[0] == '-')
make_out(t, s + 1, ft_strlen(s + 1));
else
make_out(t, s, ft_strlen(s));
if (t->out)
{
if (s[0] == '-')
flag = '-';
else if (t->opt & PLUS)
flag = '+';
else if (t->opt & BLANK)
flag = ' ';
if (flag && t->opt & MINUS)
t->out[0] = flag;
else if (flag)
t->out[t->width - t->precision] = flag;
}
free(s);
return (0);
}
static int parse_hex(t_token *t, char *s)
{
int len;
if (s == NULL)
return (-1);
len = ft_strlen(s);
if (s[0] == '0' && (t->type != 'p'))
{
t->opt &= ~FOUND;
if ((t->opt & PREC) && (t->precision < 1))
len = 0;
}
make_out(t, s, len);
if (t->out)
{
if ((t->opt & FOUND) && (t->opt & MINUS))
ft_memcpy(t->out, "0x", 2);
if ((t->opt & FOUND) && !(t->opt & MINUS))
ft_memcpy(t->out + t->width - t->precision, "0x", 2);
len = -1;
while (s[0] != '0' && t->type == 'X' && t->out[++len])
t->out[len] = ft_toupper(t->out[len]);
}
free(s);
return (0);
}
int ft_parse_token(t_token *t, va_list ap)
{
const char *dec = "0123456789";
const char *hex = "0123456789abcdef";
if (t == NULL)
return (-1);
if (t->type == '%' || t->type == 'c' || t->type == 's')
return (parse_text(t, ap));
else if (t->type == 'd' || t->type == 'i')
return (parse_number(t, ft_itoa(va_arg(ap, int)), 0));
else if (t->type == 'u')
return (parse_number(t, ft_utoa(va_arg(ap, unsigned int), dec), 0));
else if (t->type == 'x' || t->type == 'X')
return (parse_hex(t, ft_utoa(va_arg(ap, unsigned int), hex)));
else if (t->type == 'p')
return (parse_hex(t, ft_utoa(va_arg(ap, unsigned long), hex)));
else
t->out = ft_strdup(t->in);
if (t->out == NULL)
return (-1);
return (0);
}