-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
73 lines (67 loc) · 1.81 KB
/
ft_printf.c
File metadata and controls
73 lines (67 loc) · 1.81 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_printf.c :+: :+: */
/* +:+ */
/* By: cwesseli <cwesseli@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2022/10/24 10:24:39 by cwesseli #+# #+# */
/* Updated: 2022/11/01 16:40:29 by cwesseli ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static t_print *initialize_list(t_print *list)
{
list->len = 0;
list->valid_char = "cspdiuxX%";
list->base_d = "0123456789";
list->base_h = "0123456789abcdef";
list->base_h_up = "0123456789ABCDEF";
return (list);
}
static int loop(t_print *list, char *str)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == '%')
{
if (str [i + 1] == '\0')
return (1);
else
{
check_letter(list, str[i + 1]);
i++;
}
}
else
list->len += write(1, &str[i], 1);
if (str[i + 1])
i++;
else
return (1);
}
return (1);
}
int ft_printf(const char *str, ...)
{
t_print *list;
int ret;
list = (t_print *)malloc(sizeof(t_print));
if (!list)
return (-1);
initialize_list(list);
va_start(list->args, str);
if (!(check_input(list, (char *)str)))
{
va_end(list->args);
free(list);
return (-1);
}
loop(list, (char *)str);
ret = list->len;
va_end(list->args);
free(list);
return (ret);
}