-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
46 lines (42 loc) · 1.78 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nsierra- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/24 00:30:31 by nsierra- #+# #+# */
/* Updated: 2021/11/29 21:03:54 by nsierra- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "ft_printf.h"
#include <stdarg.h>
#include <stddef.h>
static void init_state(t_printf *state)
{
state->current = STATE_DEFAULT;
state->bytes_printed = 0;
state->callback[STATE_DEFAULT] = state_default;
state->callback[STATE_CONVERSION_FLAGS] = state_conversion_flags;
state->callback[STATE_CONVERSION_LENGTH] = state_conversion_length;
state->callback[STATE_CONVERSION_PRECISION] = state_conversion_precision;
state->callback[STATE_CONVERSION_PRINT] = state_conversion_print;
state->callback[STATE_WRONG_FLAG] = state_wrong_flag;
}
int ft_printf(const char *format, ...)
{
t_printf state;
unsigned int current_state;
if (format == NULL)
return (-1);
init_state(&state);
va_start(state.args, format);
while (state.current != STATE_END)
{
current_state = state.current;
format = state.callback[current_state](format, &state);
}
va_end(state.args);
return (state.bytes_printed);
}