-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
76 lines (66 loc) · 2.08 KB
/
ft_printf.c
File metadata and controls
76 lines (66 loc) · 2.08 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erabbath <erabbath@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/11 08:51:02 by erabbath #+# #+# */
/* Updated: 2023/07/12 07:35:44 by erabbath ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void init_specifier(t_specifier *specifier)
{
specifier->alignment = ALIGN_RIGHT;
specifier->sign = SIGN_DEFAULT;
specifier->is_alternative = 0;
specifier->padding = PADDING_SPACE;
specifier->min_width = 0;
specifier->precision = -1;
specifier->format_len = 0;
}
static size_t handle_normal(char *format, int *len)
{
size_t i;
i = 1;
while (format[i] && format[i] != '%')
i++;
ft_write_count(1, format, i, len);
return (i);
}
size_t handle_special(char *format, va_list args, int *len)
{
t_specifier specifier;
init_specifier(&specifier);
parse_specifier(format, args, &specifier);
if (specifier.conversion == CONVERT_INVALID)
ft_write_count(1, format, 1, len);
else
print_special(&specifier, args, len);
return (specifier.format_len);
}
int ft_printf_core(char *format, va_list args)
{
int len;
len = 0;
while (*format)
{
if (*format == '%')
format += handle_special(format, args, &len);
else
format += handle_normal(format, &len);
if (len < 0)
return (len);
}
return (len);
}
int ft_printf(const char *format, ...)
{
int result;
va_list args;
va_start(args, format);
result = ft_printf_core((char *)format, args);
va_end(args);
return (result);
}