-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
90 lines (83 loc) · 2.28 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
84
85
86
87
88
89
90
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_printf.c :+: :+: */
/* +:+ */
/* By: ksmorozo <ksmorozo@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/01/18 13:54:43 by ksmorozo #+# #+# */
/* Updated: 2021/02/11 12:29:11 by ksmorozo ######## odam.nl */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "ft_printf.h"
#include "libft/libft.h"
void initialise_struct(t_recipe *recipe)
{
recipe->minus_flag = 0;
recipe->zero_flag = 0;
recipe->space_flag = 0;
recipe->plus_flag = 0;
recipe->hash_flag = 0;
recipe->width = 0;
recipe->precision = 0;
recipe->null_precision = 0;
recipe->length = '\0';
recipe->type = '\0';
}
int check_percent(const char *print_me)
{
while (*print_me)
{
if (*print_me == '%')
return (1);
print_me++;
}
return (0);
}
int print_till_percent(const char **print_me)
{
int length;
char *buff;
length = 0;
buff = (char*)*print_me;
while (*buff)
{
if (*buff == '%')
{
write(1, *print_me, length);
(*print_me) += length;
return (length);
}
length++;
buff++;
}
return (0);
}
int ft_printf(const char *print_me, ...)
{
va_list arguments;
int amount_to_be_printed;
t_recipe recipe;
int print_return;
va_start(arguments, print_me);
amount_to_be_printed = 0;
while (check_percent(print_me))
{
initialise_struct(&recipe);
amount_to_be_printed += print_till_percent(&print_me);
if (!parse(&arguments, &print_me, &recipe))
return (-1);
print_return = print(&arguments, recipe);
if (print_return < 0)
return (-1);
amount_to_be_printed += print_return;
}
if (*print_me)
{
write(1, print_me, ft_strlen(print_me));
amount_to_be_printed += ft_strlen(print_me);
}
va_end(arguments);
return (amount_to_be_printed);
}