-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
111 lines (101 loc) · 3.44 KB
/
ft_printf.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: imunaev- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/16 22:28:06 by imunaev- #+# #+# */
/* Updated: 2024/11/18 22:12:38 by imunaev- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*
** Checks if a given character is a valid format specifier for ft_printf.
**
** c: The character to check.
** Return: 1 if the character is a valid specifier, 0 otherwise.
*/
int in_set(char c)
{
return (c == 'c' || c == 's' || c == 'p' || c == 'd'
|| c == 'i' || c == 'u' || c == 'x' || c == 'X' || c == '%');
}
/*
** Processes the format specifier and writes the corresponding argument
** to the standard output.
**
** args: The list of arguments passed to ft_printf.
** specifier: The format specifier to handle.
** Return: The number of bytes written on success, -1 on failure.
*/
ssize_t handle_format(va_list args, char specifier)
{
ssize_t result;
if (specifier == 'c')
result = ft_putchar(va_arg(args, int));
else if (specifier == 's')
result = ft_putstr(va_arg(args, char *));
else if (specifier == 'p')
result = ft_putptr(va_arg(args, void *));
else if (specifier == 'd' || specifier == 'i')
result = ft_putnbr(va_arg(args, int));
else if (specifier == 'u')
result = ft_putnbr_unsigned(va_arg(args, unsigned int));
else if (specifier == 'x' || specifier == 'X')
result = ft_puthex(va_arg(args, unsigned int), specifier);
else if (specifier == '%')
result = ft_putchar('%');
else
result = ft_putchar(specifier);
if (result == -1)
return (-1);
return (result);
}
/*
** A simplified implementation of printf that formats and writes data
** to the standard output based on a format string.
**
** Supported conversion specifications:
** - %c: Prints a single character.
** - %s: Prints a string.
** - %p: Prints a pointer in hexadecimal format.
** - %d: Prints a signed decimal integer.
** - %i: Prints a signed decimal integer.
** - %u: Prints an unsigned decimal integer.
** - %x: Prints an unsigned hexadecimal integer (lowercase).
** - %X: Prints an unsigned hexadecimal integer (uppercase).
** - %%: Prints a literal percent sign.
**
** format: The format string containing text and format specifiers.
** param ...: The variable arguments corresponding to the format specifiers.
** Return: The total number of bytes written on success, or -1 on failure.
*/
ssize_t ft_printf(const char *format, ...)
{
va_list args;
ssize_t bytes_total;
ssize_t result;
bytes_total = 0;
va_start(args, format);
while (*format)
{
if (*format == '%' && in_set(*(format + 1)))
{
format++;
result = handle_format(args, *format);
}
else
result = ft_putchar(*format);
if (result == -1)
return (va_end(args), -1);
bytes_total += result;
format++;
}
va_end(args);
return (bytes_total);
}
ssize_t ft_putchar(char c)
{
return (write(1, &c, 1));
}