-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
83 lines (78 loc) · 2.54 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dalbano <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 09:59:21 by dalbano #+# #+# */
/* Updated: 2024/11/08 21:30:00 by dalbano ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/**
* TODO: fix strict and add to libft
*/
/*
implement stair-functions to check every possible *format type
%c - done
%s - done
%p - done
%d - done
%i - done
%u - done
%x - done
%X - done
%% - done
%mix - done
*/
static void manual_switch(const char *format,
int *printed_chars, va_list args, t_flags *flags)
{
if (*format == 'c')
*printed_chars += write(1,
&(char){(unsigned char)va_arg(args, int)}, 1);
else if (*format == 'p')
{
*printed_chars += write(1, "0x", 2);
ft_print_pointer((uintptr_t)va_arg(args, void *), printed_chars);
}
else if (*format == 's')
ft_print_string(va_arg(args, char *), printed_chars);
else if (*format == 'd' || *format == 'i')
*printed_chars += ft_print_number(va_arg(args, int), flags);
else if (*format == 'u')
*printed_chars += ft_print_number_unsigned(va_arg(args,
unsigned int), flags);
else if (*format == 'x')
*printed_chars += ft_print_hex(va_arg(args, unsigned int), 0, flags);
else if (*format == 'X')
*printed_chars += ft_print_hex(va_arg(args, unsigned int), 1, flags);
else if (*format == '%')
*printed_chars += write(1, "%", 1);
else
*printed_chars += write(1, format, 1);
}
int ft_printf(const char *format, ...)
{
va_list args;
t_flags flags;
int printed_chars;
printed_chars = 0;
va_start(args, format);
while (*format)
{
if (*format == '%' && *(format + 1))
{
format = parse_flags(format + 1, &flags);
format = parse_width(format, &flags, args);
format = parse_precision(format, &flags, args);
manual_switch(format, &printed_chars, args, &flags);
}
else
printed_chars += write(1, format, 1);
format++;
}
va_end(args);
return (printed_chars);
}