-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrutils.c
213 lines (182 loc) · 4.12 KB
/
strutils.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
204
205
206
207
208
209
210
211
212
213
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "macros.h"
char char_is_invisible( const char c )
{
return ( (c==' ') || (c=='\t') || (c=='\r') || (c=='\n') );
}
char* applyInversion(char* str) {
int head, tail;
char t;
for ( head=0,tail=(strlen(str)-1) ; head<tail ; head++,tail-- ) {
t = str[head];
str[head] = str[tail];
str[tail] = t;
}
return str;
}
char *getFileName(char *destiny, const char *fileWithPath) {
int i,pos;
/* Returning till found a slash */
for ( i=(strlen(fileWithPath)-1),pos=0 ; (i>=0) ; i--,pos++ ) {
if (fileWithPath[i]=='/') break;
destiny[pos] = fileWithPath[i];
}
destiny[pos]='\0';
applyInversion(destiny);
/* Now, removing the . */
const int endIdx = strlen(destiny)-1;
for ( i=endIdx ; (i>=0) ; i-- )
{
if (destiny[i]=='.')
break;
}
return destiny;
}
char *str_remove_sps_sol( char *dest, const char *str )
{
char *startDest = dest;
const char *send = str + strlen( str );
const char *s = str;
while ( (char_is_invisible(*s)) && (s<send) )
s++;
while ( (s<send) )
{
*dest = *s;
++dest;
++s;
}
*dest = '\0';
return startDest;
}
char* getParamName(char* target, const char* str) {
unsigned int i;
unsigned int size;
unsigned int pos;
pos = 0;
size = strlen(str);
for ( i=0 ; i<size ; i++ ) {
if (str[i] != '-') {
if (str[i] != '=') {
target[pos++] = str[i];
} else {
break;
}
}
}
target[pos] = '\0';
return target;
}
char* getParamValue(char* target, const char* str) {
unsigned int i;
unsigned int size;
size = strlen(str);
unsigned int destSize = 0;
for ( i=0 ; i<size ; i++) {
if (str[i]=='=') break;
}
i++;
for ( ; i<size ; i++) {
target[destSize] = str[i];
destSize++;
}
target[destSize] = 0;
return target;
}
char *str_remove_sps_eol( char *str )
{
char *p;
p = str + strlen(str)-1;
while ( p>=str )
{
if (char_is_invisible(*p))
*p = '\0';
else
break;
--p;
}
return str;
}
void str_all_to_upper( char *str )
{
int l = strlen( str );
{
int i;
for ( i=0 ; (i<l) ; ++i )
str[i] = toupper(str[i]);
}
}
void str_fill_spaces_left( char* dest, const char* str, int n )
{
int len = strlen( str );
int toFill = n-len;
toFill = MAX( 0, toFill );
if (toFill)
memset( dest ,' ', sizeof(char)*toFill );
strcpy( dest+toFill, str );
}
void str_fill_spaces_right(char* dest, const char* str, int n)
{
int len = strlen( str );
int toFill = n-len;
toFill = MAX( 0, toFill );
strcpy( dest, str );
if (toFill)
memset( dest+len ,' ', sizeof(char)*toFill );
*(dest+len+toFill) = '\0';
}
void str_fill_spaces_both( char *dest, const char *str, int n )
{
str_fill_spaces_left( dest, str, n/2);
str_fill_spaces_right( dest, str, n/2);
}
char *str_clear_spaces(char *input) {
int i,j;
char *output=input;
for (i = 0, j = 0; i<strlen(input); i++,j++)
{
if (input[i]!=' '&&input[i]!='\n')
output[j]=input[i];
else
j--;
}
output[j]='\0';
return output;
}
char *str_remove_dbl_spaces( char *str )
{
/* removing spaces at extremes */
char dest[LINE_SIZE];
str_remove_sps_sol( dest, str );
str_remove_sps_eol( dest );
strcpy( str, dest );
if (strlen(str)<=3) /* no double spaces possible A A */
return str;
char *s, *v, *prev;
/* transforming all intermediate
invisible chars in spaces */
for ( s=str+1; (*s!='\0') ; ++s )
if (char_is_invisible(*s))
*s = ' ';
v = str+2,
s = str+2,
prev = str+1;
SKIP_CHAR:
/* jump just one if valid is a repeated space or tab */
if ( *prev==' ' )
while ( *v==' ' )
++v;
if ( *v == '\0' )
goto CLOSE_STR;
prev = v;
if ( v!=s )
*s = *v;
++v; ++s;
if ( *v != '\0' )
goto SKIP_CHAR;
CLOSE_STR:
*s = '\0';
return str;
}