-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
85 lines (75 loc) · 1.99 KB
/
Copy pathft_printf.c
File metadata and controls
85 lines (75 loc) · 1.99 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
78
79
80
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: obouizi <obouizi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/05 16:46:13 by obouizi #+# #+# */
/* Updated: 2024/12/05 20:26:25 by obouizi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static int handle_character(const char *str, int *i)
{
int len;
len = ft_putchar(str[*i]);
(*i)++;
return (len);
}
static int handle_percent(const char *str, int *i, va_list args)
{
if (is_valid_specifier(str[*i + 1]))
{
(*i)++;
return (check_specifier(str[*i], args));
}
return (handle_character(str, i));
}
static int process_format(const char *str, int *i, va_list args)
{
int temp;
if (str[*i] == '%')
{
temp = handle_percent(str, i, args);
if (temp == -1)
return (-1);
(*i)++;
return (temp);
}
else
return (handle_character(str, i));
}
static int process_string(const char *str, int *i, va_list args, int *printlen)
{
int temp;
temp = process_format(str, i, args);
if (temp == -1)
return (-1);
*printlen += temp;
return (0);
}
int ft_printf(const char *str, ...)
{
int printlen;
int i;
va_list args;
int temp;
if (!str)
return (-1);
va_start(args, str);
printlen = 0;
i = 0;
temp = 0;
while (str[i])
{
temp = process_string(str, &i, args, &printlen);
if (temp == -1)
{
va_end(args);
return (-1);
}
}
va_end(args);
return (printlen);
}