-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
86 lines (77 loc) · 2.5 KB
/
ft_printf.c
File metadata and controls
86 lines (77 loc) · 2.5 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
86
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gcoralie <gcoralie@student.21-school.ru +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/17 22:54:40 by gcoralie #+# #+# */
/* Updated: 2022/01/14 18:04:51 by gcoralie ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include "ft_printf.h"
#include "gc_libft_part.h"
#include "gc_conversion_output.h"
static int output_argument(va_list *aptr, char *conv);
int ft_printf(const char *fs, ...)
{
va_list aptr;
int print_len;
int total_len;
char *fs_ptr;
char conv;
va_start(aptr, fs);
fs_ptr = (char *)fs;
total_len = 0;
while (fs_ptr != NULL)
{
conv = 0;
fs_ptr = gc_parse_format_string(fs_ptr, &conv, &print_len);
total_len += print_len;
print_len = output_argument(&aptr, &conv);
if (print_len == -1)
return (total_len);
total_len += print_len;
}
va_end(aptr);
return (total_len);
}
static int output_argument(va_list *aptr, char *conv)
{
int res;
res = -1;
if (*conv == (char) 'c')
res = output_char((char)(va_arg(*aptr, int)));
if (*conv == (char) 's')
res = output_str(va_arg(*aptr, char *));
if (*conv == (char) 'p')
res = output_ptr(va_arg(*aptr, void *));
if ((*conv == (char) 'd') || (*conv == (char) 'i'))
res = output_dec(va_arg(*aptr, int));
if (*conv == (char) 'u')
res = output_uint(va_arg(*aptr, unsigned int));
if (*conv == (char) 'x')
res = output_hex_lowercase(va_arg(*aptr, unsigned int));
if (*conv == (char) 'X')
res = output_hex_uppercase(va_arg(*aptr, unsigned int));
if (*conv == (char) '%' || *conv == (char) '\0'
|| (gc_strchr("cspdiuxX", (int)(*conv)) == NULL))
res = output_other(*conv);
return (res);
}
/* Output 'len' symbols from string 's' to 'fd' 'count' times */
int gc_putnstr_fd(char *s, int fd, int len, int count)
{
int i;
i = 0;
if (count < 0)
count = 0;
while (i < count)
{
write(fd, (void *)s, len);
++i;
}
return (len * i);
}