forked from dzabeligan/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf.c
More file actions
94 lines (83 loc) · 1.69 KB
/
printf.c
File metadata and controls
94 lines (83 loc) · 1.69 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "print_helpers.h"
#include "main.h"
/**
* print_all - Print all formats
* @spec: specifier object
* @args: pointer to arguments to be printed
*
* Return: length of bytes written
*/
static int print_all(specifier_t *spec, va_list args)
{
int format_t_len = 0;
int j = 0;
int len = 0;
format_t f[] = {
{'c', print_char},
{'i', print_int},
{'d', print_int},
{'u', print_unsigned},
{'o', print_octal},
{'x', print_hex},
{'X', print_hex_cap},
{'p', print_address},
{'s', print_string},
{'S', print_custom_S},
{'%', print_percent},
{'r', print_reverse},
{'R', print_rot_13},
{'b', print_binary},
};
format_t_len = sizeof(f) / sizeof(f[0]);
while (spec && j < format_t_len && spec->specifier != f[j].specifier)
j++;
if (j < format_t_len)
{
return (f[j].print(spec, args));
}
else if (spec && spec->specifier)
{
len += buffered_print("%", 1);
len += buffered_print(&spec->specifier, 1);
}
return (len);
}
/**
* _printf - Print all formats
* @format: format string
*
* Return: 0
*/
int _printf(const char *format, ...)
{
va_list args;
int len = 0;
if (format == NULL)
return (-1);
va_start(args, format);
while (format && *format)
{
if (*format == '%' && *(format + 1))
{
int skip = 0;
specifier_t spec;
memset(&spec, 0, sizeof(spec));
skip = get_specifier(&spec, format + 1, args);
format += skip;
if (spec.specifier == '\0')
break;
len += print_all(&spec, args);
format++;
continue;
}
if (*format == '%' && *(format + 1) == '\0')
break;
len += buffered_print(format++, 1);
}
len += buffered_print(NULL, UINT_MAX);
va_end(args);
return (len);
}