-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.c
142 lines (123 loc) · 2.3 KB
/
spec.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"
/**
* get_specifier - finds the format function
* @s: string of the format
* Return: the number of bytes printed
*/
int (*get_specifier(char *s))(va_list ap, params_t *params)
{
specifier_t specifiers[] = {
{"c", print_char},
{"d", print_int},
{"i", print_int},
{"s", print_string},
{"%", print_percent},
{"b", print_binary},
{"o", print_octal},
{"u", print_unsigned},
{"x", print_hex},
{"X", print_HEX},
{"p", print_address},
{"S", print_S},
{"r", print_rev},
{"R", print_rot13},
{NULL, NULL}
};
int i = 0;
while (specifiers[i].specifier)
{
if (*s == specifiers[i].specifier[0])
{
return (specifiers[i].f);
}
i++;
}
return (NULL);
}
/**
* get_print_func - finds the format function
* @s: string of the format
* @ap: argument pointer
* @params: the parameters struct
* Return: the number of bytes printed
*/
int get_print_func(char *s, va_list ap, params_t *params)
{
int (*f)(va_list, params_t *) = get_specifier(s);
if (f)
return (f(ap, params));
return (0);
}
/**
* get_flag - finds the flag functions
* @s: the format string
* @params: the parameters struct
* Return: if flag was valid
*/
int get_flag(char *s, params_t *params)
{
int i = 0;
switch (*s)
{
case '+':
i = params->plus_flag = 1;
break;
case ' ':
i = params->space_flag = 1;
break;
case '#':
i = params->hashtag_flag = 1;
break;
case '-':
i = params->minus_flag = 1;
break;
case '0':
i = params->zero_flag = 1;
break;
}
return (i);
}
/**
* get_modifier - finds the modifier function
* @s: string for format
* @params: parameter structure
* Return: if modifier was valid
*/
int get_modifier(char *s, params_t *params)
{
int i = 0;
switch (*s)
{
case 'h':
i = params->h_modifier = 1;
break;
case 'l':
i = params->l_modifier = 1;
break;
}
return (i);
}
/**
* get_width - gets the width from the format string
* @s: the format string
* @params: the parameters struct
* @ap: the argument pointer
* Return: new pointer
*/
char *get_width(char *s, params_t *params, va_list ap)
/* should this function use char **s and modify the pointer? */
{
int d = 0;
if (*s == '*')
{
d = va_arg(ap, int);
s++;
}
else
{
while (_isdigit(*s))
d = d * 10 + (*s++ - '0');
}
params->width = d;
return (s);
}