-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_printf.c
76 lines (67 loc) · 1.27 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
76
#include "main.h"
#include <string.h>
/**
* get_flags - Get the flags object
* @s: The string
* Return: The flag
*/
flag_t get_flags(const char *s)
{
flag_t flag = {0, 0, 0, 0};
while (*s == '+' || *s == ' ' || *s == '#')
{
if (*s == '+')
flag.plus = 1;
if (*s == ' ')
flag.space = 1;
if (*s == '#')
flag.diese = 1;
s++;
flag.count += 1;
}
return (flag);
}
/**
* _printf - Produce output according to a format
* @format: The string containing the format
* Return: Number of printed char
*/
int _printf(const char *format, ...)
{
int nb = 0;
int (*ptr)(va_list, flag_t);
va_list list;
flag_t flag = {0, 0, 0, 0};
if (!format || (format[0] == '%' && !format[1]))
return (-1);
if (format[0] == '%' && format[1] == ' ' && !format[2])
return (_puts("% "));
va_start(list, format);
for (; *format; format++)
{
memset(&flag, 0, sizeof(flag_t));
if (*format == '%')
{
format++;
if (!*format)
break;
if (*format == '+' || *format == ' ' || *format == '#')
{
flag = get_flags(format);
format += flag.count;
}
ptr = get_specifier(*format);
if (ptr)
nb += ptr(list, flag);
else
{
nb += _putchar('%');
nb += _putchar(*format);
}
}
else
nb += _putchar(*format);
}
va_end(list);
return (nb);
}