-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_print.c
164 lines (149 loc) · 2.82 KB
/
_print.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "main.h"
/**
* is_flag - function to check for flags
*
* @s: string to check
*
* Return: true || false
*/
int is_flag(const char s)
{
char flags[] = {'-', '#', '0', 'l', 'h', '.', '+', ' ', '\0'};
int i;
if (s >= '0' && s <= '9')
return (1);
for (i = 0; flags[i] != '\0'; i++)
if (s == flags[i])
return (1);
return (0);
}
/**
* dot_null - to check for dot nolifiers
*
* @s: pointer to string
*
* Return: 1 if true else 0
*/
int dot_null(const char *s)
{
int i, d = 0;
for (i = 0; is_flag(s[i]); i++)
{
if (s[i] == '.')
d++;
if (d > 1)
if (s[i] >= '0' && s[i] <= '9')
return (1);
}
return (0);
}
/**
* cfmt - check for specifier
* @s: format
*
* Return: format specifier function
*/
int (*cfmt(const char **s))(const char *, va_list)
{
int i, len = 13;
PrtFmt prt_fmt[] = {
{"s", output_alpha},
{"c", output_char},
{"d", output_decimal},
{"i", output_integer},
{"R", output_ROT13},
{"p", output_ptraddress},
{"r", output_revers},
{"b", output_bits},
{"S", output_bigS},
{"x", output_hexlower},
{"X", output_hexupper},
{"u", output_unsignedint},
{"o", output_octal}
};
while (is_flag(**s))
(*s)++;
for (i = 0; i < len; i++)
if (**s == *((prt_fmt + i)->spec))
return ((prt_fmt + i)->selectprint);
return (NULL);
}
/**
* percent_handler - a finction to handle percentage.
*
* @s: pointer to sting pointer
* @no_perc: number of percentage counted
* @args: variable argument
*
* Return: length of printed string
*/
int percent_handler(const char **s, va_list args, int no_perc)
{
int i, p_length = 0;
const char *check1, *check2;
int (*output)(const char *, va_list arg);
check1 = check2 = *s;
for (; *check1 == '%'; check1++)
no_perc++;
check2 = check1;
(dot_null(check1)) ? (output = NULL) : (output = cfmt(&check2));
if (output)
{
for (i = 0; i < (no_perc / 2); i++)
{
my_putchar('%');
p_length++;
}
if ((no_perc % 2) == 1)
{
p_length += output(check1, args);
*s = check2;
}
else
*s = check1 - 1;
}
else
{
if (*check2 == '%' && (no_perc % 2) == 1)
p_length += percent_handler(&check2, args, no_perc);
else
{
for (i = 0; i < (no_perc / 2) + (no_perc % 2); i++)
{
my_putchar('%');
p_length++;
}
*s = check1 - 1;
}
}
return (p_length);
}
/**
* _printf - Printf function
* @format: format.
* Return: fomatted
*/
int _printf(const char *format, ...)
{
const char *check;
int p_length = 0;
va_list args;
check = format;
va_start(args, format);
if (!format || (format[0] == '%' && !format[1]))
return (-1);
if (format[0] == '%' && format[1] == ' ' && !format[2])
return (-1);
while (*check != '\0')
{
if (*check == '%')
p_length += percent_handler(&check, args, 0);
else
{
p_length += my_putchar(*check);
}
check++;
}
va_end(args);
return (p_length);
}