-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_strings.c
203 lines (171 loc) · 5.16 KB
/
convert_strings.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "main.h"
unsigned int convert_s(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
unsigned int convert_S(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
unsigned int convert_r(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
unsigned int convert_R(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
/**
* convert_s - Converts an argument to a string and
* stores it to a buffer contained in a struct.
* @args: A va_list pointing to the argument to be converted.
* @flags: Flag modifiers.
* @wid: A width modifier.
* @prec: A precision modifier.
* @len: A length modifier.
* @output: A buffer_t struct containing a character array.
*
* Return: The number of bytes stored to the buffer.
*/
unsigned int convert_s(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
char *str, *null = "(null)";
int size;
unsigned int ret = 0;
(void)flags;
(void)len;
str = va_arg(args, char *);
if (str == NULL)
return (_memcpy(output, null, 6));
for (size = 0; *(str + size);)
size++;
ret += print_string_width(output, flags, wid, prec, size);
prec = (prec == -1) ? size : prec;
while (*str != '\0' && prec > 0)
{
ret += _memcpy(output, str, 1);
prec--;
str++;
}
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}
/**
* convert_S - Converts an argument to a string and
* stores it to a buffer contained in a struct.
* @args: A va_list pointing to the argument to be converted.
* @flags: Flag modifiers.
* @wid: A width modifier.
* @prec: A precision modifier.
* @len: A length modifier.
* @output: A buffer_t struct containing a character array.
*
* Return: The number of bytes stored to the buffer.
*
* Description: Non-printable characteres (ASCII values < 32 or >= 127)
* are stored as \x followed by the ASCII code value in hex.
*/
unsigned int convert_S(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
char *str, *null = "(null)", *hex = "\\x", zero = '0';
int size, index;
unsigned int ret = 0;
(void)len;
str = va_arg(args, char *);
if (str == NULL)
return (_memcpy(output, null, 6));
for (size = 0; str[size];)
size++;
ret += print_string_width(output, flags, wid, prec, size);
prec = (prec == -1) ? size : prec;
for (index = 0; *(str + index) != '\0' && index < prec; index++)
{
if (*(str + index) < 32 || *(str + index) >= 127)
{
ret += _memcpy(output, hex, 2);
if (*(str + index) < 16)
ret += _memcpy(output, &zero, 1);
ret += convert_ubase(output, *(str + index),
"0123456789ABCDEF", flags, 0, 0);
continue;
}
ret += _memcpy(output, (str + index), 1);
}
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}
/**
* convert_r - Reverses a string and stores it
* to a buffer contained in a struct.
* @args: A va_list pointing to the string to be reversed.
* @flags: Flag modifiers.
* @wid: A width modifier.
* @prec: A precision modifier.
* @len: A length modifier.
* @output: A buffer_t struct containing a character array.
*
* Return: The number of bytes stored to the buffer.
*/
unsigned int convert_r(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
char *str, *null = "(null)";
int size, end, i;
unsigned int ret = 0;
(void)flags;
(void)len;
str = va_arg(args, char *);
if (str == NULL)
return (_memcpy(output, null, 6));
for (size = 0; *(str + size);)
size++;
ret += print_string_width(output, flags, wid, prec, size);
end = size - 1;
prec = (prec == -1) ? size : prec;
for (i = 0; end >= 0 && i < prec; i++)
{
ret += _memcpy(output, (str + end), 1);
end--;
}
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}
/**
* convert_R - Converts a string to ROT13 and stores
* it to a buffer contained in a struct.
* @args: A va_list pointing to the string to be converted.
* @flags: Flag modifiers.
* @wid: A width modifier.
* @prec: A precision modifier.
* @len: A lenth modifier.
* @output: A buffer_t struct containing a character array.
*
* Return: The number of bytes stored to the buffer.
*/
unsigned int convert_R(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
char *alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *rot13 = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM";
char *str, *null = "(null)";
int i, j, size;
unsigned int ret = 0;
(void)flags;
(void)len;
str = va_arg(args, char *);
if (str == NULL)
return (_memcpy(output, null, 6));
for (size = 0; *(str + size);)
size++;
ret += print_string_width(output, flags, wid, prec, size);
prec = (prec == -1) ? size : prec;
for (i = 0; *(str + i) != '\0' && i < prec; i++)
{
for (j = 0; j < 52; j++)
{
if (*(str + i) == *(alpha + j))
{
ret += _memcpy(output, (rot13 + j), 1);
break;
}
}
if (j == 52)
ret += _memcpy(output, (str + i), 1);
}
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}