forked from MatiasAcostaDiaz/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
73 lines (68 loc) · 1.43 KB
/
_printf.c
File metadata and controls
73 lines (68 loc) · 1.43 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "holberton.h"
/**
* auxiliar - the printf auxiliar function
* @format: the string
* @args: arguments passed
* @options: the options of functions
* Return: the characters printed
*/
int auxiliar(const char *format, va_list args, op_t *options)
{
int flag = 0, i = 0, j = 0, count = 0;
while (format[i] != '\0' && format)
{
flag = 0;
if (format[i] == '%')
{
if (format[i] == '%' && format[i + 1] == '\0')
return (-1);
while (options[j].c != '\0')
{
if (format[i + 1] == options[j].c)
{
flag = 1;
count += options[j].func(args);
break;
}
j++;
}
}
if (flag == 1)
{
if (format[i + 1] == '\0' || format[i + 2] == '\0')
return (count);
i += 2;
}
else
{
write(1, &format[i], 1);
i++;
count++;
}
j = 0;
}
return (count);
}
/**
* _printf - the printf function
* @format: the string
* Return: the characters printed
*/
int _printf(const char *format, ...)
{
int count = 0;
va_list args;
op_t options[] = {
{'%', print_percent}, {'c', print_char}, {'s', print_string},
{'d', print_integer}, {'i', print_integer}, {'b', print_binary},
{'u', print_uns_int}, {'o', print_octal}, {'x', print_hexa},
{'X', print_hexa_cap}, {'r', print_reversed}, {'R', print_rot13},
{'S', print_string_ascii}, {'\0', NULL}
};
va_start(args, format);
if (!format || (format[0] == '%' && format[1] == '\0'))
return (-1);
count = auxiliar(format, args, options);
va_end(args);
return (count);
}