-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_print_buffer.c
More file actions
52 lines (47 loc) · 1021 Bytes
/
_print_buffer.c
File metadata and controls
52 lines (47 loc) · 1021 Bytes
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
#include "main.h"
/**
* print_buffered - Prints a formatted string to a buffer
* @buffer: Pointer to the buffer to store the formatted string
* @format: Character(s) string to be printed
* may contain directives
* @...: Variadic args (unknown)
*
* Return: Numbers of characters printed.
*/
int print_buffered(char *buffer, const char *format, ...)
{
int chars_printed = 0;
asoc_fun fun_list[] = {
{"c", _print_char},
{"s", _print_string},
{"%", _print_percent},
{"d", _print_integer},
{"i", _print_integer},
{NULL, NULL}
};
va_list arg_list;
size_t buffer_index = 0;
va_start(arg_list, format);
while (*format != '\0')
{
if (*format != '%')
{
buffer[buffer_index++] = *format;
chars_printed++;
}
else
{
format++;
chars_printed += parser(format, fun_list, arg_list);
format++;
}
if (buffer_index >= 1023)
{
write(1, buffer, buffer_index);
buffer_index = 0;
}
}
write(1, buffer, buffer_index);
va_end(arg_list);
return (chars_printed);
}