-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_spec_funcs.c
143 lines (126 loc) · 2.61 KB
/
custom_spec_funcs.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
#include "holberton.h"
#include <stdlib.h>
/**
* get_reverse - Get the reversed copy of the string.
* @args: va_list containing the string.
* @mods: length modifiers, voided, not used with this specifier
*
* Return: char pointer to the reversed string.
*/
char *get_reverse(va_list args, char *mods)
{
int i;
int length;
char *str, *ret;
(void)mods;
str = va_arg(args, char *);
length = _strlen(str);
ret = malloc(length + 1);
if (!ret)
return (NULL);
for (i = 0; i < length; i++)
ret[i] = str[length - i - 1];
ret[i] = '\0';
return (ret);
}
/**
* get_rot - Gets the Rot13'd copy of the string
* @args: va_list containing the string
* @mods: length modifiers, voided, not used with this specifier
*
* Return: char pointer to newly allocated and rot13'd string
*/
char *get_rot(va_list args, char *mods)
{
int index, length;
unsigned char temp_c;
char is_upper;
char *str, *ret;
(void)mods;
str = va_arg(args, char *);
length = _strlen(str);
ret = malloc(length + 1);
if (!ret)
return (NULL);
index = 0;
while (str[index])
{
temp_c = (unsigned char)str[index];
if (_isalpha(temp_c))
{
is_upper = (temp_c >= 'A' && temp_c <= 'Z');
temp_c += 13;
if (is_upper)
{
if (temp_c > 'Z')
temp_c -= 26;
}
else
{
if (temp_c > 'z')
temp_c -= 26;
}
}
ret[index] = (char)temp_c;
index++;
}
ret[length] = '\0';
return (ret);
}
/**
* get_nonprint_string - gets a pointer to a string in memory containing np's
* @args: va_list to get string containing non printables
* @mods: length modifiers, voided, not used with this specifier
*
* Return: pointer to newly allocated string containing hex's for np's
*/
char *get_nonprint_string(va_list args, char *mods)
{
char *str, *ret;
int length, i;
(void)mods;
str = va_arg(args, char *);
length = 0;
i = 0;
while (str[i])
{
if (!is_printable(str[i]))
{
length += 4;
}
else
length++;
i++;
}
i = 0;
ret = malloc(length + 1);
if (!ret)
return (NULL);
if (!fill_nonprint_buffer(length, ret, str))
{
free(ret);
return (NULL);
}
return (ret);
}
/**
* get_binary - convert unsigned integer to binary representation.
* @args: va_list containing the unsigned integer.
* @mods: length modifiers, voided, not used with this specifier
*
* Return: char pointer to the binary string. NULL if malloc fails.
*/
char *get_binary(va_list args, char *mods)
{
unsigned int num;
int length;
char *ret;
(void)mods;
num = va_arg(args, unsigned int);
length = get_numbase_len(num, 2);
ret = malloc(length + 1);
if (!ret)
return (NULL);
fill_numbase_buff(num, 2, ret, length);
return (ret);
}