forked from Tahani-Saber/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
52 lines (47 loc) · 1.07 KB
/
_printf.c
File metadata and controls
52 lines (47 loc) · 1.07 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
#include "main.h"
/**
* _printf - print formated string.
*
* @format: formated string.
*
* Return: num of chars printed.
*/
int _printf(const char *format, ...)
{
int n_printed_chars = 0, index = 0, func_printed_chars = 0;
va_list args;
int (*op_func)(va_list, flags_t *);
flags_t flags = {0, 0, 0};
int flags_found;
va_start(args, format);
if (format == NULL || (format[index] == '%' && format[index + 1] == '\0'))
return (-1);
while (format && format[index] != '\0')
{
if (format[index] != '%' && !flags_found)
{
n_printed_chars += _putchar(format[index]), index++;
continue;
}
if (format[index + 1] == '\0')
{
va_end(args);
return (-1);
}
index++;
flags_found = get_flags(format[index], &flags);
if (flags_found)
continue;
op_func = get_op_func(format[index]);
if (op_func)
{
func_printed_chars = op_func(args, &flags);
n_printed_chars += func_printed_chars, index++;
}
else if (op_func == NULL)
n_printed_chars += _putchar('%');
flags.hash = flags.plus = flags.space = 0;
}
va_end(args);
return (n_printed_chars);
}