-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
57 lines (52 loc) · 1.8 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: msubtil- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/25 21:08:37 by msubtil- #+# #+# */
/* Updated: 2022/07/31 16:32:53 by msubtil- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_offstate(t_state *fmt_st, char chr, int *chr_c)
{
if (chr == '%')
*fmt_st = ON;
else
{
write(1, &chr, 1);
(*chr_c)++;
}
}
static void ft_onstate(va_list *args_ptr, t_state *fmt_st, char chr, int *chr_c)
{
ft_parse_chr(args_ptr, fmt_st, chr, chr_c);
ft_parse_str(args_ptr, fmt_st, chr, chr_c);
ft_parse_int(args_ptr, fmt_st, chr, chr_c);
ft_parse_uint(args_ptr, fmt_st, chr, chr_c);
ft_parse_ptr(args_ptr, fmt_st, chr, chr_c);
ft_parse_hex(args_ptr, fmt_st, chr, chr_c);
if (*fmt_st == DONE)
*fmt_st = OFF;
}
int ft_printf(const char *format_str, ...)
{
va_list args;
t_state fmt_st;
int chr_counter;
fmt_st = OFF;
va_start(args, format_str);
chr_counter = 0;
while (*format_str != '\0')
{
if (fmt_st == OFF)
ft_offstate(&fmt_st, *format_str, &chr_counter);
else
ft_onstate(&args, &fmt_st, *format_str, &chr_counter);
format_str++;
}
va_end(args);
return (chr_counter);
}