-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_output.c
125 lines (114 loc) · 2.42 KB
/
ft_output.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_output.c :+: :+: */
/* +:+ */
/* By: splattje <splattje@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/14 10:00:06 by splattje #+# #+# */
/* Updated: 2023/11/15 15:09:49 by splattje ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putchar(va_list ap, char c, int inputplace)
{
char ch;
if (inputplace == 1)
{
if (c == 'c')
{
ch = va_arg(ap, int);
ft_putchar_fd(ch, 1);
return (1);
}
else
{
ft_putchar_fd('%', 1);
return (1);
}
}
else
{
ft_putchar_fd(c, 1);
return (1);
}
}
int ft_putstring(va_list ap)
{
char *str;
str = NULL;
str = va_arg(ap, char *);
if (str == NULL)
str = "(null)";
ft_putstr_fd(str, 1);
return (ft_strlen(str));
}
int ft_put_ptr_to_str(va_list ap)
{
char *str;
void *ptr;
int length;
ptr = va_arg(ap, void *);
str = ptr_to_hex(ptr);
if (str != NULL)
{
if (*str == '0' && ft_strlen(str) == 1)
{
free(str);
str = "(nil)";
ft_putstr_fd(str, 1);
return (5);
}
else
{
length = ft_strlen(str);
write(1, "0x", 2);
ft_putstr_fd(str, 1);
free(str);
return (length + 2);
}
}
return (0);
}
int ft_putnumber(va_list ap)
{
int num;
char *str;
int length;
str = NULL;
num = va_arg(ap, int);
str = ft_itoa(num);
if (str != NULL)
{
length = ft_strlen(str);
ft_putstr_fd(str, 1);
free(str);
return (length);
}
return (0);
}
int ft_uint_to_str(va_list ap, char c)
{
unsigned int un_num;
char *str;
int length;
str = NULL;
if (c == 'u')
{
un_num = va_arg(ap, unsigned int);
str = uitostr(un_num);
}
else if (c == 'x' || c == 'X')
{
un_num = va_arg(ap, unsigned int);
str = uint_to_hex_uplow(un_num, c == 'x');
}
if (str != NULL)
{
length = ft_strlen(str);
ft_putstr_fd(str, 1);
free(str);
return (length);
}
return (0);
}