-
Notifications
You must be signed in to change notification settings - Fork 2
/
_printf.c
85 lines (82 loc) · 1.62 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
84
85
#include <stdio.h>
#include <stdlib.h>
#include "holberton.h"
#include <stdarg.h>
/**
* verify_type - search list of types for a matching format
* @format: string of formats from input
* Description: refers to struct for valist database of formats.
* Return: on success ptr to right function or NULL if failure
*/
static int (*verify_type(const char *format))(va_list)
{
unsigned int i;
print_t p[] = {
{"c", print_c},
{"s", print_s},
{"i", print_i},
{"d", print_d},
{"b", print_b},
{"u", print_u},
{"o", print_o},
{"x", print_x},
{"X", print_X},
{"p", print_p},
{"S", print_S},
{"r", print_r},
{"R", print_R},
{NULL, NULL}
};
for (i = 0; p[i].t != NULL; i++)
{
if (*(p[i].t) == *format)
{
break;
}
}
return (p[i].f);
}
/**
* _printf - prints a variety of outputs in specific formats
* @format: input argument types for output format
* Description: produces output according to a format using _putchar.
* Return: number of characters printed or error number -1
*/
int _printf(const char *format, ...)
{
/* i = iterator of format string */
/* j = counter of chars printed */
unsigned int i = 0, j = 0;
va_list valist;
int (*f)(va_list);
if (format == NULL)
return (-1);
va_start(valist, format);
while (format[i])
{
for (; format[i] != '%' && format[i]; i++)
{
_putchar(format[i]);
j++;
}
if (!format[i])
return (j);
f = verify_type(&format[i + 1]);
if (f != NULL)
{
j += f(valist);
i += 2;
continue;
}
if (!format[i + 1])
return (-1);
_putchar(format[i]);
j++;
if (format[i + 1] == '%')
i += 2;
else
i++;
}
va_end(valist);
return (j);
}