-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_printf.c
144 lines (136 loc) · 3.84 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "holberton.h"
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
/**
* copy_to_buffer - Copies the given character over to the buffer
* @formatter: Character to copy over
* @buffer: Buffer being copied to
* @buflenptr: Pointer to the length of the buffer, the number of
* characters in the buffer
* @bufposptr: Pointer to the position in the buffer
*
* Return: Number of characters copied to buffer
*/
int copy_to_buffer(char formatter, char buffer[],
int *buflenptr, int *bufposptr)
{
int chars;
chars = 0;
buffer[*bufposptr] = formatter;
*bufposptr += 1;
*buflenptr += 1;
if (*buflenptr == 1024)
{
write_buffer(buffer, buflenptr, bufposptr);
}
chars++;
return (chars);
}
/**
* check_conversion - Checks formatter character to see if
* it's a conversion specifier
* @formatter: The format character being checked
* @conversions: Struct holding conversion specifiers & function pointers to
* @appropriate functions for corresponding conversion specifier
* @buffer: Buffer needed to copy to when calling function
* @buflenptr: Pointer to the length of the buffer
* @bufposptr: Pointer to the position within the buffer
* @print_this: va_list holding all given arguments to _printf function
*
* Return: Return the number of characters copied to buffer if a
* function is called, 0 if no function is called
*/
int check_conversion(char formatter, char_funcs_t conversions[], char buffer[],
int *buflenptr, int *bufposptr, va_list print_this)
{
int j, chars;
chars = 0;
for (j = 0; j < 13; j++)
{
if (formatter == *conversions[j].c)
{
chars += conversions[j].f(print_this, buffer, buflenptr,
bufposptr);
return (chars);
}
}
return (0);
}
/**
* formatPrinter - finds the formatters function and prints its arguement
* @format: The format character being checked
* @conversions: Struct holding conversion specifiers & function pointers to
* appropriate functions for corresponding conversion specifier
* @buffer: Buffer needed to copy to when calling function
* @buflenptr: Pointer to the length of the buffer
* @bufposptr: Pointer to the position within the buffer
* @print_this: va_list holding all given arguments to _printf function
*
* Return: Return the number of characters copied to buffer if a
* function is called, 0 if no function is called
*/
int formatPrinter(const char *format, va_list print_this, char buffer[],
int *buflenptr, int *bufposptr, char_funcs_t conversions[])
{
int i, chars, print;
chars = 0;
for (i = 0; format[i] != '\0' && format != NULL; i++)
{
if (format[i] == '%')
{
i++;
print = check_conversion(format[i], conversions,
buffer, buflenptr, bufposptr,
print_this);
if (print == 0)
chars += copy_to_buffer(format[i], buffer,
buflenptr, bufposptr);
chars += print;
}
else
{
chars += copy_to_buffer(format[i], buffer, buflenptr,
bufposptr);
}
}
return (chars);
}
/**
* _printf - Print out a formatted string
* @format: Format of the string
* Return: Number of characters printed
*/
int _printf(const char *format, ...)
{
va_list print_this;
char buffer[1024];
int chars, buflen, bufpos, *buflenptr, *bufposptr;
char_funcs_t conversions[] = {{"c", print_c},
{"s", print_s},
{"i", print_int},
{"d", print_int},
{"u", print_u},
{"o", print_o},
{"x", print_hex},
{"X", print_heX},
{"b", print_b},
{"S", print_S},
{"r", print_r},
{"R", print_R},
{"p", print_p},
};
initialize_buffer(buffer);
chars = bufpos = 0;
buflen = 1;
buflenptr = &buflen;
bufposptr = &bufpos;
va_start(print_this, format);
if (format == NULL || print_this == NULL)
return (chars);
chars = formatPrinter(format, print_this, buffer,
buflenptr, bufposptr, conversions);
write_buffer(buffer, buflenptr, bufposptr);
va_end(print_this);
return (chars);
}