-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
83 lines (81 loc) · 1.45 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
77
78
79
80
81
82
83
#include "holberton.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
/**
* printer - Uses the coresponding function to print.
* @formati: Type of element to print.
* Return: Function address.
**/
int (*printer(char formati))(va_list)
{
type_printer frm[] = {
{'c', print_c},
{'s', print_s},
{'i', print_i},
{'d', print_i},
{'R', print_rot13},
{'b', print_b},
{'r', print_r},
{'S', print_S},
{'p', print_p},
{'u', print_u},
{'x', print_x},
{'X', print_X},
{'o', print_o},
{'\0', NULL}
};
int i = 0;
for (i = 0; frm[i].c; i++)
{
if (formati == frm[i].c)
break;
}
return (frm[i].f);
}
/**
* _printf - a function that produces output according to a format
* @format: character string
* Return: 0 success
*/
int _printf(const char *format, ...)
{
va_list arg;
int i = 0, p_counter = 0;
int (*f)(va_list);
if (!format || (format[0] == '%' && format[1] == '\0'))
return (-1);
va_start(arg, format);
for (; format && format[i]; i++)
{
if (format[i] == '%')
{
if (format[i + 1] == '%')
{
_putchar(format[i]);
i++;
p_counter++;
}
else if (format[i + 1] == '\0')
{
_putchar(format[i]);
p_counter++;
return (p_counter);
}
else
{
f = printer(format[i + 1]);
if (f != NULL)
{
p_counter += f(arg);
i++;
}
else
{
_putchar(format[i]);
p_counter++; } } }
else
{ _putchar(format[i]);
p_counter++; } }
va_end(arg);
return (p_counter); }