-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_print_conv_utils_sid.c
135 lines (125 loc) · 3.61 KB
/
ft_printf_print_conv_utils_sid.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_print_conv_utils_sid.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emdi-mar <emdi-mar@student.42roma.it> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 19:36:06 by emdi-mar #+# #+# */
/* Updated: 2025/02/19 23:46:35 by emdi-mar ### ########.fr */
/* */
/* ************************************************************************** */
/*
** FLAG '0' is ignored when flag '-' is present
** FLAG ' ' is ignored when flag '+' is present
**
** Conversion specifier 's':
** flag '0' results in undefined behavior with 'c'
** flag '+' results in undefined behavior with 'c'
** flag '#' results in undefined behavior with 'c'
** flag ' ' results in undefined behavior with 'c'
**
** Conversion specifier 'i'/'d':
** flag '#' results in undefined behavior with 'i'/'d'
** if a precision is specified, the 0 flag is ignored
*/
#include "ft_printf.h"
static void print_conv_s_2(t_fields *flds, size_t max_s, char *s, char fill)
{
if (flds->minus > 0)
{
while (max_s-- > 0)
putchar_ftprintf(*s++, flds);
while (flds->width-- > 0)
putchar_ftprintf(SPACE, flds);
}
else
{
while (flds->width-- > 0)
putchar_ftprintf(fill, flds);
while (max_s-- > 0)
putchar_ftprintf(*s++, flds);
}
}
void print_conv_s(t_fields *flds, va_list ap)
{
char *s;
char fill;
size_t max_s;
fill = SPACE;
if (flds->zero > 0)
fill = ZERO;
s = va_arg(ap, char *);
if (!s)
s = STR_N;
max_s = ft_strlen(s);
if ((size_t)flds->dot < max_s && flds->dot > -1)
max_s = flds->dot;
flds->width = 0;
if (max_s < (size_t)flds->width)
flds->width = flds->width - max_s;
print_conv_s_2(flds, max_s, s, fill);
}
static void print_conv_i_utils(t_fields *flds, char *n, char fill)
{
if (flds->minus > 0)
{
if (*n == MINUS)
putchar_ftprintf(*n++, flds);
while (flds->dot-- > 0)
putchar_ftprintf(ZERO, flds);
while (*n)
putchar_ftprintf(*n++, flds);
while (flds->width-- > 0)
putchar_ftprintf(SPACE, flds);
}
else
{
if (*n == MINUS && fill == ZERO)
putchar_ftprintf(*n++, flds);
while (flds->width-- > 0)
putchar_ftprintf(fill, flds);
if (*n == MINUS)
putchar_ftprintf(*n++, flds);
while (flds->dot-- > 0)
putchar_ftprintf(ZERO, flds);
while (*n)
putchar_ftprintf(*n++, flds);
}
}
static char print_conv_i_2(char *n, t_fields *flds, char fill)
{
size_t max_s;
size_t n_dgt;
if (*n == '0' && !(*(n + 1)) && flds->dot == 0)
*n = '\0';
fill = SPACE;
if (flds->zero > 0 && !(flds->dot > -1))
fill = ZERO;
max_s = ft_strlen(n);
n_dgt = max_s;
if (*n == MINUS && ft_isdigit(*(n + 1)))
n_dgt = max_s - 1;
flds->dot = 0;
if (flds->dot > -1 && (size_t)(flds->dot) > n_dgt)
flds->dot = flds->dot - n_dgt;
flds->width = 0;
if ((size_t)flds->width > (max_s + flds->dot))
flds->width = (flds->width - (max_s + flds->dot));
return (fill);
}
void print_conv_i(t_fields *flds, va_list ap)
{
char *n;
char fill;
fill = SPACE;
n = ft_itoa(va_arg(ap, int));
if (!n)
flds->printed = ERR;
else
{
fill = print_conv_i_2(n, flds, fill);
print_conv_i_utils(flds, n, fill);
free(n);
}
}