-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
99 lines (90 loc) · 2.85 KB
/
ft_printf.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: monoue <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/28 16:57:30 by monoue #+# #+# */
/* Updated: 2020/08/24 09:26:03 by monoue ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static bool isvalid_format(char *format_cut)
{
if (!format_cut || ft_strlen(format_cut) < 2)
return (false);
return (isconversion_c(format_cut[ft_strlen(format_cut) - 1]));
}
static char *deal_with_null_case(char *str)
{
if (!str)
str = ft_strdup("(null)");
return (str);
}
static void *get_value(t_format_info *info, va_list *ap)
{
unsigned int c;
if (ft_strchr("di", info->conv_c))
return (ft_itoa_malloc((long)va_arg(*ap, int)));
else if (info->conv_c == 'u')
return (ft_utoa_malloc(va_arg(*ap, unsigned int)));
else if (info->conv_c == 'x' || info->conv_c == 'X')
return (ft_xtoa_malloc((unsigned long long)va_arg(*ap, unsigned int),
info));
else if (info->conv_c == 'p')
return (ft_xtoa_malloc((unsigned long long)va_arg(*ap, void *), info));
else if (info->conv_c == 'c')
{
if ((c = va_arg(*ap, unsigned int)) != 0)
return (ft_ctoa_malloc(c));
info->c_null = true;
return (ft_ctoa_malloc('1'));
}
else if (info->conv_c == '%')
return (ft_ctoa_malloc('%'));
else if (info->conv_c == 's')
return (deal_with_null_case(ft_strdup(va_arg(*ap, char *))));
return (NULL);
}
static t_format_info *get_format_info(char *target, va_list *ap)
{
int index;
t_format_info *info;
info = malloc(sizeof(t_format_info) * 1);
if (!info)
return (NULL);
init_format_info(info);
info->conv_c = target[ft_strlen(target) - 1];
index = 1;
while (!isconversion_c(target[index]))
set_zero_minwidth_minus_prec(target, &index, info);
info->value = get_value(info, ap);
return (info);
}
int ft_printf(const char *format, ...)
{
size_t count;
char *format_cut;
va_list ap;
va_start(ap, format);
count = 0;
while (format && *format)
{
if (*format != '%')
{
ft_putchar(*format);
format++;
count++;
}
else
{
format_cut = cut_out_format(&format, &ap);
if (isvalid_format(format_cut))
count += put_result(get_format_info(format_cut, &ap));
SAFE_FREE(format_cut);
}
}
va_end(ap);
return (count);
}