-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
83 lines (76 loc) · 2.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fschuber <fschuber@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/16 07:40:32 by fschuber #+# #+# */
/* Updated: 2023/10/19 08:49:06 by fschuber ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <stdio.h>
#include <stdio.h>
#include "ft_printf.h"
static int identify_specials(const char *str, va_list args, int counter)
{
if (str[counter + 1] == 'd' || str[counter + 1] == 'i')
return (print_int(va_arg(args, int)));
if (str[counter + 1] == 'c')
return (print_char((char) va_arg(args, int)));
if (str[counter + 1] == 'u')
return (print_u_int(va_arg(args, int)));
if (str[counter + 1] == 's')
return (print_str(va_arg(args, char *)));
if (str[counter + 1] == '%')
return (print_char('%'));
if (str[counter + 1] == 'p')
return (print_ptr(va_arg(args, void *)));
if (str[counter + 1] == 'x')
return (print_hexa(va_arg(args, int), 'x'));
if (str[counter + 1] == 'X')
return (print_hexa(va_arg(args, int), 'X'));
return (-1);
}
int process_string(const char *str, va_list args)
{
int counter;
int printed_chars_counter;
int specials_output;
counter = 0;
printed_chars_counter = 0;
specials_output = 0;
while (str[counter] != '\0')
{
if (str[counter] == '%')
{
specials_output = identify_specials(str, args, counter++);
if (specials_output == -1)
return (-1);
printed_chars_counter += specials_output;
}
else
{
if (write(1, &str[counter], 1) == -1)
return (-1);
printed_chars_counter++;
}
counter++;
}
return (printed_chars_counter);
}
int ft_printf(const char *str, ...)
{
va_list args;
int printed_chars_counter;
va_start(args, str);
printed_chars_counter = 0;
if (!str)
return (va_end(args), 0);
printed_chars_counter = process_string(str, args);
va_end(args);
if (printed_chars_counter == -1)
return (-1);
return (printed_chars_counter);
}