-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspec_str.c
157 lines (155 loc) · 3.31 KB
/
spec_str.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
#include "holberton.h"
/**
* _spec_s - sends va_arg with appropriate tags into buffer
* @b_r: a pointer to the struct buffer
* @t: a pointer to the struct tags
*/
void _spec_s(buffer *b_r, tags *t)
{
char *hold;
char *b_str;
int i, j, l, b_str_size, hold_len;
/* get arg from va_arg and store */
hold = va_arg(b_r->ap, char *);
if (hold == NULL)
hold = "(null)";
i = l = 0;
/*check the precision tag*/
hold_len = str_len(hold);
/* if prec is found, ignore width */
if (t->prec != -1 && t->prec < hold_len)
hold_len = t->prec;
/* if width if found */
if (t->width > hold_len)
{
b_str = _str_whelp(t, hold, hold_len);
b_str_size = t->width;
}
/* no width given */
else
{
b_str_size = hold_len;
b_str = malloc(b_str_size * sizeof(char));
while (i < b_str_size)
b_str[i] = hold[i], i++;
}
for (j = 0; j < b_str_size; j++)
_write(b_r, b_str[j]);
free(b_str);
}
/**
* _spec_S - sends va_arg with appropriate tags into buffer
* @b_r: a pointer to the struct buffer
* @t: a pointer to the struct tags
*/
void _spec_S(buffer *b_r, tags *t)
{
char *hold, *holds;
char *b_str;
int i, j, l, b_str_size, hold_len;
/* get arg from va_arg and store */
hold = va_arg(b_r->ap, char *);
i = l = 0;
holds = hold;
holds = _to_hex_unreadable(hold);
/*check the precision tag*/
hold_len = str_len(holds);
/* if prec is found, ignore width */
if (t->prec != -1 && t->prec < hold_len)
hold_len = t->prec;
/* if width if found */
if (t->width > hold_len)
{
b_str = _str_whelp(t, holds, hold_len);
b_str_size = t->width;
}
/* no width given */
else
{
b_str_size = hold_len;
b_str = malloc(b_str_size * sizeof(char));
while (i < b_str_size)
b_str[i] = holds[i], i++;
}
for (j = 0; j < b_str_size; j++)
_write(b_r, b_str[j]);
free(b_str);
free(holds);
}
/**
* _str_whelp - sends va_arg with appropriate tags into buffer
* @t: a pointer to the struct tags
* @hold: string pulled from va_arg
* @hold_len: length of hold after precision cut
*
* Return: Pointer to b_str
*/
char *_str_whelp(tags *t, char *hold, int hold_len)
{
int i, b_str_size, minus, k, l;
char *b_str;
minus = i = k = l = 0;
b_str_size = t->width;
b_str = malloc((b_str_size) * sizeof(char *));
/*if - flag is found */
for (k = 0; t->flags[k] != '\0'; k++)
{
if (t->flags[k] == '-')
minus = 1;
}
/*- flag found, hold left most */
if (minus == 1)
{
while (i < hold_len)
b_str[i] = hold[i], i++;
while (i < b_str_size)
b_str[i++] = ' ';
}
/* - flag not found, hold right most*/
else
{
while (i < b_str_size - hold_len)
b_str[i++] = ' ';
while (i < b_str_size)
b_str[i++] = hold[l++];
}
return (b_str);
}
/**
* _to_hex_unreadable - sends va_arg with appropriate tags into buffer
* @hold: string to convert
*
* Return: Pointer to edited string
*/
char *_to_hex_unreadable(char *hold)
{
char *holdconv, *hexhold;
int i, j;
i = j = 0;
holdconv = malloc(1024);
while (hold[i] != '\0')
{
if (hold[i] >= 32 && hold[i] <= 126)
holdconv[j++] = hold[i];
else
{
holdconv[j++] = '\\';
holdconv[j++] = 'x';
hexhold = _int_to_caphexstr(hold[i]);
if (hexhold[1] == '\0')
{
holdconv[j++] = '0';
holdconv[j++] = hexhold[0];
}
else
{
holdconv[j++] = hexhold[0];
holdconv[j++] = hexhold[1];
}
free(hexhold);
}
i++;
}
holdconv[j] = '\0';
return (holdconv);
}