-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
55 lines (51 loc) · 1.76 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alex <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/13 16:50:24 by alex #+# #+# */
/* Updated: 2021/01/13 16:50:26 by alex ### ########.fr */
/* */
/* ************************************************************************** */
#include "printfhead.h"
void ft_struct_init(t_struct *blocks)
{
blocks->format = 0;
blocks->f_minus = 0;
blocks->f_zero = 0;
blocks->neg_number = 0;
blocks->width = 0;
blocks->precision = 0;
blocks->chars_to_print = 0;
blocks->f_prec = 0;
blocks->f_width = 0;
}
int ft_printf(const char *format, ...)
{
t_struct blocks;
if (!format)
return (0);
ft_struct_init(&blocks);
va_start(blocks.args, format);
blocks.total_printed = 0;
blocks.skipped_chars = 0;
while (format[blocks.skipped_chars] != '\0')
{
if (format[blocks.skipped_chars] == '%')
{
ft_parse_flags(&format[++blocks.skipped_chars], &blocks);
ft_struct_init(&blocks);
continue ;
}
else if (format[blocks.skipped_chars] != '\0')
blocks.total_printed +=
write(1, &format[blocks.skipped_chars++], 1);
else
break ;
}
blocks.skipped_chars = 0;
va_end(blocks.args);
return (blocks.total_printed);
}