-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
98 lines (89 loc) · 2.39 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
91
92
93
94
95
96
97
98
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ysonmez <ysonmez@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/13 11:28:05 by ysonmez #+# #+# */
/* Updated: 2021/07/17 17:41:49 by ysonmez ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static t_struct *new_struct(void)
{
t_struct *p_data;
p_data = (t_struct *)malloc(sizeof(t_struct));
(*p_data).hash = 0;
(*p_data).space = 0;
(*p_data).plus = 0;
(*p_data).zero = 0;
(*p_data).minus = 0;
(*p_data).prec = -1;
(*p_data).width = 0;
(*p_data).alen = 0;
(*p_data).print = 0;
(*p_data).type = 0;
(*p_data).chartype = 0;
(*p_data).argint = 0;
(*p_data).argstr = 0;
(*p_data).argptr = 0;
(*p_data).argunsint = 0;
return (p_data);
}
static void ft_setvarzero(int *count, int *i)
{
*count = 0;
*i = 0;
}
static void ft_put_normal(char format, int *count, int *i)
{
ft_putcharr_fd(format, 1);
(*count)++;
(*i)++;
}
int ft_parse_format(va_list args, t_struct *data, const char *str)
{
int state;
int i;
state = 0;
i = 0;
(*data).type = ft_get_type(str);
(*data).chartype = ft_get_chartype(data);
if ((*data).type == -1 || (*data).chartype == -1)
return (-1);
i = ft_set_format(data, str);
if (i == -1)
return (-1);
state = ft_printer(args, data);
if (state == -1)
return (-1);
return (2 + i);
}
int ft_printf(const char *format, ...)
{
va_list args;
t_struct *data;
int i;
int count;
int parser;
va_start(args, format);
ft_setvarzero(&count, &i);
while (format[i])
{
if (format[i] == '%')
{
data = new_struct();
parser = ft_parse_format(args, data, format + i + 1);
if (parser == -1)
return (-1);
i += parser;
count += (*data).print;
free(data);
}
else
ft_put_normal(format[i], &count, &i);
}
va_end(args);
return (count);
}