-
Notifications
You must be signed in to change notification settings - Fork 0
/
_printf.c
75 lines (62 loc) · 1.28 KB
/
_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
#include "main.h"
/**
* _printf - prints a format string
*
* @format: string to print
* Return: number of characters printed
*/
int _printf(const char *format, ...)
{
va_list args;
buf *my_buffer;
int tmp_count, printf_ret;
if (!format)
return (-1);
my_buffer = init_buff();
if (my_buffer == NULL)
return (-1);
va_start(args, format);
printf_ret = init_printf(format, my_buffer, args);
if (printf_ret < 0)
{
cleanup_buff(my_buffer);
va_end(args);
return (-1);
}
write(STDOUT_FILENO, my_buffer->head, my_buffer->tmp);
tmp_count = my_buffer->count;
cleanup_buff(my_buffer);
va_end(args);
return (tmp_count);
}
/**
* init_printf - printf engine
*
* @format: format string
* @my_buffer: buffer that holds final string and count
* @args: variadic argument list
* Return: the value of the handler function
*/
int init_printf(const char *format, buf *my_buffer, va_list args)
{
char *tmp = (char *)format;
int parsed_chars, handler_value;
while (*tmp)
{
parsed_chars = 0;
if (*tmp == '%')
{
if (*(tmp + 1) == '\0')
return (-1);
handler_value = specifier_handler(my_buffer, tmp + 1, args, &parsed_chars);
if (handler_value >= 0)
{
tmp += (parsed_chars + 1);
continue;
}
}
update_buff(my_buffer, *tmp);
tmp++;
}
return (0);
}