-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_speciftype.c
executable file
·110 lines (100 loc) · 3.03 KB
/
ft_speciftype.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_specifType.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmakwakw <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/30 18:08:27 by mmakwakw #+# #+# */
/* Updated: 2017/11/30 18:10:57 by mmakwakw ### ########.fr */
/* */
/* ************************************************************************** */
# include "ft_printf.h"
void ft_speciftypeini(char *format, char specif, char flag, va_list ap)
{
if (specif == 'c' || specif == 'C')
ft_c(ap, format, flag);
else if (specif == 's' || specif == 'S')
ft_s(ap, format, flag);
else if (specif == 'p')
ft_p(ap);
else if (specif == 'x' || specif == 'X')
ft_x(ap, format, flag);
else if (specif == 'd' || specif == 'D')
ft_d(ap, format, flag);
else if (specif == 'i')
ft_i(ap, format, flag);
else if (specif == 'o' || specif == 'O')
ft_o(ap, format, flag);
else if (specif == 'u' || specif == 'U')
ft_u(ap, format, flag);
}
void ft_c(va_list ap, char *format, char flag)
{
char c;
char f_addon;
char *tmp;
int param;
int p_addon;
c = (char)va_arg(ap, int);
f_addon = format[ft_chrpos(format, flag) + 1];
tmp = ft_strsub(format, ft_chrpos(format, f_addon), ft_strlen(format) - 2);
param = ft_getparam(format, flag) - 1;
p_addon = ft_getparam(tmp, flag) - 1;
if (flag == '+' && f_addon != '-')
ft_fill(param - 1, ' ');
if (flag == '0' && f_addon != '+')
ft_fill(param - 1, ' ');
if (f_addon == '+' && flag == '0')
ft_fill(p_addon - 1, ' ');
if (c)
ft_c_support(c, param, flag, f_addon);
}
void ft_s(va_list ap, char *format, char flag)
{
char *s;
int param;
int s_length;
s = va_arg(ap, char*);
if (s == NULL && flag == '\0')
ft_putstr("(null)");
else
{
param = ft_getparam(format, flag) - 1;
s_length = ft_strlen(s);
if (ft_strcmp(s, "") == 0 && !flag)
ft_putstr("");
if (param > 0 && (flag == ' ' || flag == '+'))
ft_fill(param - s_length, ' ');
if (s)
{
if (param > 0 && !flag)
ft_fill(param - s_length, ' ');
ft_putstr(s);
if (flag == '-')
ft_fill(param - s_length, ' ');
}
}
}
void ft_p(va_list ap)
{
int nbr;
nbr = va_arg(ap, int);
ft_putstr("0x");
ft_puthex(nbr);
}
void ft_x(va_list ap, char *format, char flag)
{
int x;
int param;
x = va_arg(ap, int);
param = ft_getparam(format, flag);
if (flag == '#' && !ft_strchr(format, 'X'))
ft_putstr("0x");
if (flag == '#' && ft_strchr(format, 'X'))
ft_putstr("0X");
if (x && ft_strchr(format, 'X'))
ft_putstr(ft_itoa_base(x, 16));
if (x && !param && !ft_strchr(format, 'X'))
ft_puthex(x);
}