-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_arg.c
More file actions
95 lines (87 loc) · 2.26 KB
/
Copy pathft_printf_arg.c
File metadata and controls
95 lines (87 loc) · 2.26 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_arg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sujo <sujo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/18 12:42:44 by sujo #+# #+# */
/* Updated: 2021/05/24 12:00:18 by sujo ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int get_type_per(t_format info)
{
int length;
length = 1;
if (info.width > 1)
length = info.width;
if (info.left == 0)
{
if (info.zero == 1)
ft_put_n_char(length - 1, '0');
else
ft_put_n_char(length - 1, ' ');
write(1, "%", 1);
}
else
{
write(1, "%", 1);
ft_put_n_char(length - 1, ' ');
}
return (length);
}
int get_type_c(va_list ap, t_format info)
{
int length;
char ch;
ch = va_arg(ap, int);
if (info.width > 1)
length = info.width;
else
length = 1;
if (info.left == 0)
{
ft_put_n_char(length - 1, ' ');
write(1, &ch, 1);
}
else
{
write(1, &ch, 1);
ft_put_n_char(length - 1, ' ');
}
return (length);
}
static void print_zero(int width, int zero)
{
if (zero == 1)
ft_put_n_char(width, '0');
else
ft_put_n_char(width, ' ');
}
int get_type_s(va_list ap, t_format info)
{
int tmp;
char *str;
int str_len;
str = va_arg(ap, char *);
if (str == NULL)
str = "(null)";
str_len = (int)ft_strlen(str);
if (info.precision > str_len || info.precision <= -1)
info.precision = str_len;
tmp = info.width - info.precision;
if (tmp <= 0)
write(1, str, info.precision);
else if (info.left == 0)
{
print_zero(tmp, info.zero);
write(1, str, info.precision);
}
else if (info.left == 1)
{
write(1, str, info.precision);
ft_put_n_char(tmp, ' ');
}
return (info.width > info.precision ? info.width : info.precision);
}