Repository navigation
Expand file tree
/
Copy pathft_printf.c
More file actions
149 lines (137 loc) · 3.45 KB
/
Copy pathft_printf.c
File metadata and controls
149 lines (137 loc) · 3.45 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dchernyk <dchernyk@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/03 20:49:21 by dchernyk #+# #+# */
/* Updated: 2026/05/04 14:47:27 by dchernyk ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_charorstr(int charorstr, ...)
{
int i;
char *str;
va_list args;
i = 0;
va_start(args, charorstr);
if (charorstr == 0)
{
i = (unsigned char)va_arg(args, int);
write(1, (char *)&i, 1);
return (1);
}
else
{
str = va_arg(args, char *);
if (str == NULL)
return (ft_charorstr(1, "(null)"));
while (str[i])
ft_charorstr(0, str[i++]);
va_end(args);
return (i);
}
}
static int put_num(int n)
{
int bytes_counter;
long int nb;
nb = n;
bytes_counter = 0;
if (nb < 0)
bytes_counter += ft_charorstr(0, '-');
if (nb < 0)
nb = -nb;
if (nb >= 10)
{
bytes_counter += put_num((nb / 10));
bytes_counter += put_num((nb % 10));
return (bytes_counter);
}
else
{
if (nb < 10)
bytes_counter += ft_charorstr(0, nb + '0');
}
return (bytes_counter);
}
static int put_hex(unsigned long int n, int hexcase)
{
int bytes_counter;
unsigned int base;
base = 16;
if (hexcase == 0)
base = 10;
bytes_counter = 0;
if (n >= base)
{
bytes_counter += put_hex((n / base), hexcase);
bytes_counter += put_hex((n % base), hexcase);
return (bytes_counter);
}
else
{
if (n < 10)
bytes_counter += ft_charorstr(0, (n + '0'));
else if (hexcase == 1)
bytes_counter += ft_charorstr(0, ((n - 10) + 'a'));
else if (hexcase == 2)
bytes_counter += ft_charorstr(0, ((n - 10) + 'A'));
}
return (bytes_counter);
}
static int switch_type(char c, va_list *args)
{
unsigned long int arg;
if (c == 'c')
return (ft_charorstr(0, va_arg(*args, int)));
else if (c == 's')
return (ft_charorstr(1, va_arg(*args, char *)));
else if (c == 'p')
{
arg = va_arg(*args, unsigned long int);
if (!arg)
return (ft_charorstr(1, "(nil)"));
return (ft_charorstr(1, "0x") + put_hex(arg, 1));
}
else if (c == 'd' || c == 'i')
return (put_num(va_arg(*args, int)));
else if (c == 'u')
return (put_hex(va_arg(*args, unsigned int), 0));
else if (c == 'x')
return (put_hex(va_arg(*args, unsigned int), 1));
else if (c == 'X')
return (put_hex(va_arg(*args, unsigned int), 2));
else if (c == '%')
return (ft_charorstr(0, '%'));
return (0);
}
int ft_printf(const char *format, ...)
{
va_list args;
int bytes_counter;
bytes_counter = 0;
va_start(args, format);
while (*format)
{
if (*format == '%' && *(format + 1) != '\0')
{
format++;
bytes_counter += switch_type(*format, &args);
}
else
bytes_counter += ft_charorstr(0, *format);
format++;
}
va_end(args);
return (bytes_counter);
}
//#include <stdio.h>
//int main(void)
//{
// printf(" %p %p \n", NULL, NULL);
// ft_printf(" %p %p ", NULL, NULL);
// return (0);
//}