-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
142 lines (136 loc) · 2.38 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
#include "main.h"
/**
* if_specifier - Function that compare the specifier to get
* the function of the specifier.
* @spec: character argument
* @s: character argument.
* Return: i to get the first index from the spec[i]
*/
int if_specifier(choose_t spec[], char s)
{
int i;
for (i = 0; i < 12; i++)
{
if (s == spec[i].c)
return (i);
}
return (-5);
}
/**
* decimal - Function that print Numbers.
*@arg: Variadic Arguments from user [Int].
* Return: Length of digits
*/
int decimal(va_list arg)
{
int n = va_arg(arg, int);
int len = 0;
unsigned int m, d, count;
if (n == 0)
{
write(1, "0", 1);
return (++len);
}
if (n < 0)
{
write(1, "-", 1);
len++;
m = n * -1;
}
else
{
m = n;
}
d = m;
count = 1;
while (d > 9)
{
d /= 10;
count *= 10;
}
while (count >= 1)
{
char s = ((m / count) % 10) + '0';
write(1, &s, 1);
len++;
count /= 10;
}
return (len);
}
/**
* string - Function that print String.
*@arg: Variadic Arguments from user [String]
* Return: Length of string
*/
int string(va_list arg)
{
char *s = va_arg(arg, char*);
if (s == NULL)
{
write(1, "(null)", 6);
return (6);
}
else
{
write(1, s, ((int)strlen(s)));
}
return ((int)strlen(s));
}
/**
* ch - Function that print characters.
*@arg: Variadic Arguments from user [character]
* Return: Length 1 that equal one character
*/
int ch(va_list arg)
{
char s = va_arg(arg, int);
write(1, &s, 1);
return (1);
}
/**
* _printf - Function that print the format with handling the specifier.
*@format: Fixed argument that take specifier
* Return: Length of format
*/
int _printf(const char *format, ...)
{
va_list arg;
int len = 0, i = 0;
int spec_idx;
choose_t spec[] = {{'c', ch}, {'s', string}, {'d', decimal},
{'i', decimal}, {'r', reverse}, {'R', rot13}, {'b', binary},
{'X', hex_upper}, {'x', hex_lower}, {'o', octal}, {'u', un_int}, {'p', po}};
if (format == NULL)
return (-1);
va_start(arg, format);
while (format[i] && format)
{
spec_idx = if_specifier(spec, format[i + 1]);
if ((format[i] == '%'))
{
if (format[i + 1] == '\0')
return (-1);
else if (format[i + 1] == '%')
{
write(1, "%", 1);
len++;
i += 2;
} else if (spec_idx >= 0)
{
len += spec[spec_idx].ptr(arg);
i += 2;
} else
{
len++;
write(1, &(format[i]), 1);
i++;
}
} else
{
len++;
write(1, &(format[i]), 1);
i++;
}
}
return (len);
}