-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecifier.c
More file actions
138 lines (128 loc) · 2.49 KB
/
Copy pathspecifier.c
File metadata and controls
138 lines (128 loc) · 2.49 KB
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
#include "main.h"
/**
* get_specifier - Find the appropriate format specifier function.
* @s: The format string.
*
* Return: Pointer to the corresponding format specifier function.
*/
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 - Find the appropriate print
* function for the format specifier.
* @s: The format string.
* @ap: Argument pointer.
* @params: The parameters struct.
*
* Return: The number of bytes printed by the format specifier.
*/
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 - Determine if a flag is present in the format string.
* @s: The format string.
* @params: The parameters struct.
*
* Return: 1 if the flag was found, 0 otherwise.
*/
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 - Determine if a modifier is present in the format string.
* @s: The format string.
* @params: The parameters struct.
*
* Return: 1 if a modifier was found, 0 otherwise.
*/
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 - Extract the width value from the format string.
* @s: The format string.
* @params: The parameters struct.
* @ap: The argument pointer.
*
* Return: A pointer to the next character after the width.
*/
char *get_width(char *s, params_t *params, va_list ap)
{
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);
}