-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
97 lines (90 loc) · 1.81 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
#include "main.h"
#include <stdarg.h>
#include <stdio.h>
/**
* _printf - Prints formatted output to stdout
* @format: The format string
* Owned by flamonrose23 && lili852
* Return: The number of characters printed
*/
int _printf(const char *format, ...)
{
va_list args;
int count = 0;
va_start(args, format);
count = parse_format(format, args);
va_end(args);
return (count);
}
/**
* parse_format - Parses and prints the format string
* @format: The format string
* @args: The variable arguments list
*
* Return: The number of characters printed
*/
int parse_format(const char *format, va_list args)
{
int count = 0;
int i = 0;
if (!format || (format[0] == '%' && !format[1]))
{
return (-1);
}
if (format[0] == '%' && format[1] == ' ' && !format[2])
{
return (-1);
}
while (format && format[i])
{
if (format[i] != '%')
{
count += _putchar(format[i]);
}
else if (format[i + 1])
{
count += handle_conversion(format[i + 1], args);
i++;
}
else
{
count += _putchar(format[i]);
}
i++;
}
return (count);
}
/**
* handle_conversion - Handles the conversion specifier
* @specifier: The conversion specifier
* @args: The variable arguments list
*
* Return: The number of characters printed
*/
int handle_conversion(char specifier, va_list args)
{
switch (specifier)
{
case 'c':
return (print_char(va_arg(args, int)));
case 's':
return (print_string(va_arg(args, char *)));
case '%':
_putchar('%');
return (1);
case 'd':
return (print_decimal(va_arg(args, int)));
case 'i':
return (print_integer(va_arg(args, int)));
case 'u':
return (print_unsigned(va_arg(args, unsigned int)));
case 'o':
return (print_unsigned_octal(va_arg(args, unsigned int)));
case 'x':
return (print_unsigned_hexadecimal(va_arg(args, unsigned int)));
default:
_putchar('%');
_putchar(specifier);
return (2);
}
}