-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
77 lines (68 loc) · 1.65 KB
/
Copy pathft_printf.c
File metadata and controls
77 lines (68 loc) · 1.65 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asuc <asuc@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 17:19:23 by asuc #+# #+# */
/* Updated: 2023/11/09 17:35:30 by asuc ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/ft_printf.h"
int ft_printf(const char *format, ...)
{
va_list arg;
int count;
if (!format)
return (-1);
va_start(arg, format);
count = process_format(format, arg);
va_end(arg);
return (count);
}
int process_format(const char *format, va_list arg)
{
int i;
int count;
int ret;
int err;
i = 0;
count = 0;
err = 0;
while (format[i])
{
ret = handle_character(format, &i, arg, &err);
if (ret == -1)
return (-1);
count += ret;
}
return (count);
}
int handle_character(const char *format, int *index, va_list arg, int *err)
{
int ret;
if (format[*index] == '%')
ret = handle_percent(format, index, arg, err);
else
ret = ft_putchar_fd(format[*index], 1);
(*index)++;
return (ret);
}
int handle_percent(const char *format, int *index, va_list arg, int *err)
{
int ret;
ret = 0;
if (is_format(format[*index + 1]) == 0)
(*err)++;
ret += print_format(format, *index + 1, arg);
if ((*err) == 1 && ret == 0 && format[*index + 1] == 0)
return (-1);
if (ret == 0)
ret = ft_putchar_fd(format[*index], 1);
else
(*index)++;
if (ret == -1)
return (0);
return (ret);
}