-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_bonus.c
135 lines (125 loc) · 3.46 KB
/
ft_printf_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: achane-l <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/14 12:05:22 by achane-l #+# #+# */
/* Updated: 2021/07/14 12:05:24 by achane-l ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf_bonus.h"
int ft_printf(const char *str, ...)
{
va_list elem;
t_flags flags;
va_start(elem, str);
flags.char_count = 0;
while (*str)
{
initialize_flags(&flags);
if (*str == '%')
{
str = parsing(str, &flags, elem);
convert_and_print(elem, &flags);
str++;
if (flags.char_count == -1)
return (flags.char_count);
}
if (*str != 0 && *str != '%')
{
write(1, str, 1);
flags.char_count++;
str++;
}
}
va_end(elem);
return (flags.char_count);
}
void *parsing(const char *str, t_flags *flags, va_list elem)
{
str++;
while (ft_is_in("0-#+ ", *str) == 1)
append_flags(flags, *(str++));
while (*str >= '0' && *str <= '9')
flags->width = (flags->width * 10) + (*(str++) - '0');
if (*str == '*')
{
flags->width = va_arg(elem, int);
str++;
}
if (*str == '.')
{
str++;
flags->precision = 0;
while (*str >= '0' && *str <= '9')
flags->precision = (flags->precision * 10) + (*(str++) - '0');
if (*str == '*')
{
flags->precision = va_arg(elem, int);
str++;
}
}
flags->type_conv = *str;
change_state_flags(flags);
return ((void *)str);
}
void convert_type(t_flags *flags, va_list elem, char **str)
{
long long nbr;
char *base;
if (flags->type_conv == 'd' || flags->type_conv == 'i')
*str = ft_itoa_with_prec(va_arg(elem, long long), flags);
else if (flags->type_conv == 'u')
*str = ft_uitoa_with_prec(va_arg(elem, unsigned long), flags);
else if (flags->type_conv == 'c' || flags->type_conv == '%')
*str = conv_char_to_str(flags, elem);
else if (flags->type_conv == 's')
*str = str_with_prec(va_arg(elem, void *), flags);
else if (flags->type_conv == 'p')
*str = conv_to_pointer(va_arg(elem, void *), flags);
else if (flags->type_conv == 'x' || flags->type_conv == 'X')
{
nbr = va_arg(elem, unsigned int);
base = choose_base(flags->type_conv);
*str = conv_to_hex_with_prec(nbr, base, flags);
if (flags->sharp > 0 && nbr != 0)
{
append_char(str, flags->type_conv);
append_char(str, '0');
}
}
}
void convert_and_print(va_list elem, t_flags *flags)
{
char *str;
str = NULL;
convert_type(flags, elem, &str);
if (str == NULL && (flags->type_conv != 's'))
flags->char_count = -1;
print_width_and_str(str, flags);
}
void print_width_and_str(char *str, t_flags *flags)
{
int check_width;
int c;
check_width = flags->width - ft_strlen(str);
if (check_width > 0)
{
c = ' ';
if (flags->minus > 0)
putstr(str, flags);
if (flags->zero > 0)
c = '0';
while (check_width--)
{
write(1, &c, 1);
flags->char_count++;
}
if (flags->minus == 0)
putstr(str, flags);
}
else
putstr(str, flags);
}