-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_flag_functions.c
More file actions
147 lines (139 loc) · 3.09 KB
/
apply_flag_functions.c
File metadata and controls
147 lines (139 loc) · 3.09 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
139
140
141
142
143
144
145
146
147
#include "holberton.h"
/**
* apply_left_align - apply left justification
* Description: Converts text to left justification instead of default right
* @text: String to format
* Return: Formatted text
*/
char *apply_left_align(char *text)
{
int index, adjust;
if (!text)
return (NULL);
for (adjust = 0; *(text + adjust); adjust++)
{
if (*(text + adjust) != ' ')
break;
}
if (adjust == _strlen(text))
return (text);
for (index = adjust; *(text + index); index++)
*(text + index - adjust) = *(text + index);
for (index--; adjust > 0 ; index--, adjust--)
*(text + index) = ' ';
return (text);
}
/**
* apply_zero - apply zero formatting
* Description: Applies '0' formatting to a given string.
* @text: String to be formatted
* Return: Formatted string
*/
char *apply_zero(char *text)
{
int index = 0;
if (!text)
return (NULL);
while (*(text + index))
{
if (*(text + index) != ' ')
break;
*(text + index) = '0';
index++;
}
if (*(text + index) == '-')
{
*(text + index) = '0';
*text = '-';
}
return (text);
}
/**
* apply_hash - apply '#' formatting
* Description: Puts a leading 0 in front of octal conversions if the first
* digit isn't already a 0. Puts a leading 0x for x conversions,
* or a leading 0X for X conversions.
* @text: String to format
* @type: Type specifier, should only be o, x, or X
* Return: Formatted text
*/
char *apply_hash(char *text, char type)
{
char *hash_text;
int index, distance, needed;
if (!text)
return (NULL);
needed = (type == 'o' ? 1 : 2);
for (distance = 0; *(text + distance); distance++)
{
if (*(text + distance) != ' ')
break;
}
if (distance - needed >= 0)
{
if (type == 'o')
*(text + distance - 1) = '0';
else
{
*(text + distance - 1) = (type == 'x' ? 'x' : 'X');
*(text + distance - 2) = '0';
}
return (text);
}
needed = (distance - needed) * -1;
hash_text = _calloc(_strlen(text) + needed + 1, 1, 0);
if (!hash_text)
return (NULL);
if (type == 'o')
{
*hash_text = '0';
for (index = 0; *(text + index); index++)
*(hash_text + index + 1) = *(text + index);
}
else
{
*hash_text = '0';
*(hash_text + 1) = (type == 'x' ? 'x' : 'X');
for (index = 0; *(text + index); index++)
*(hash_text + index + 2) = *(text + index);
}
free(text);
return (hash_text);
}
/**
* apply_sign - apply sign
* Description: Applies either + or ' ' formatting to a string.
* @text: String to be formatted
* @flags: String of formatting flags present in parent formatting string.
* Return: Formatted text
*/
char *apply_sign(char *text, char *flags)
{
char *sign_text;
int index;
char flag;
flag = (*(flags + 2) == '+' ? '+' : ' ');
if (*text == '-')
return (text);
if (*text == ' ')
{
index = 0;
while (*(text + index) == ' ')
index++;
*(text + index - 1) = '+';
return (text);
}
if (*text == '0')
{
*text = flag;
return (text);
}
sign_text = _calloc(_strlen(text) + 2, 1, '\0');
if (!sign_text)
return(NULL);
*sign_text = flag;
for (index = 0; *(text + index); index++)
*(sign_text + index + 1) = *(text + index);
free(text);
return (sign_text);
}