TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcmisc
strext.c
Go to the documentation of this file.
1
5
/*****************************************************************************/
6
#include "
libtpcmisc.h
"
7
#include <string.h>
8
/*****************************************************************************/
9
10
/*****************************************************************************/
17
int
strTokenNr
(
19
const
char
*str1,
21
const
char
*str2
22
) {
23
int
i=0, n=0;
24
char
*cptr;
25
if
(str1==NULL || str2==NULL || strlen(str1)==0 || strlen(str2)==0)
return
(0);
26
27
cptr=(
char
*)str1;
28
do
{
29
// pass delimiter characters
30
i=strspn(cptr, str2); cptr+=i;
31
// pass characters between delimiters
32
i=strcspn(cptr, str2); cptr+=i;
if
(i>0) n++;
33
}
while
(i>0);
34
return
(n);
35
}
36
/*****************************************************************************/
37
38
/*****************************************************************************/
45
int
strTokenNCpy
(
47
const
char
*str1,
49
const
char
*str2,
51
int
i,
53
char
*str3,
55
int
count
56
) {
57
int
j=0, n=0;
58
char
*cptr;
59
if
(str1==NULL || str2==NULL || strlen(str1)==0 || strlen(str2)==0)
return
(0);
60
if
(i<1 || str3==NULL || count<2)
return
(0);
61
62
cptr=(
char
*)str1;
63
do
{
64
// pass delimiter characters
65
j=strspn(cptr, str2); cptr+=j;
66
// pass characters between delimiters
67
j=strcspn(cptr, str2);
if
(j>0) n++;
68
// if this is the required token nr, then stop here
69
if
(n==i) {
70
if
(j>count-1) j=count-1;
71
strlcpy
(str3, cptr, j+1);
//strncpy(str3, cptr, j); str3[j]=(char)0;
72
break
;
73
}
74
cptr+=j;
75
}
while
(j>0);
76
if
(n>i) {str3[0]=(char)0;
return
(0);}
77
return
(j);
78
}
79
/*****************************************************************************/
80
81
/*****************************************************************************/
89
char
*
strTokenDup
(
91
const
char
*s1,
93
const
char
*s2,
95
int
*next
96
) {
97
if
(next!=NULL) *next=0;
98
if
(s1==NULL)
return
NULL;
99
100
char
*s3=NULL, *cptr;
101
size_t
j;
102
103
/* If no delimiters, then return copy of s1 */
104
if
(s2==NULL || strlen(s2)<1) {
105
s3=strdup(s1);
if
(next!=NULL) *next=strlen(s1);
106
return
s3;
107
}
108
/* Pass initial delimiter characters */
109
cptr=(
char
*)s1; j=strspn(cptr, s2); cptr+=j;
if
(next!=NULL) *next=j;
110
/* calculate characters between delimiters */
111
j=strcspn(cptr, s2);
if
(j==0) {
return
NULL;}
112
if
(next!=NULL) *next+=j;
113
/* Allocate space for token */
114
s3=calloc(j+1,
sizeof
(
char
));
if
(s3==NULL)
return
NULL;
115
strlcpy
(s3, cptr, j+1);
116
return
s3;
117
}
118
/*****************************************************************************/
119
120
/*****************************************************************************/
126
int
strChrCount
(
128
const
char
*str1,
130
const
char
*str2
131
) {
132
unsigned
int
n=0, i, j;
133
if
(str1==NULL || str2==NULL || strlen(str1)==0 || strlen(str2)==0)
return
n;
134
for
(i=0; i<strlen(str1); i++)
135
for
(j=0; j<strlen(str2); j++)
136
if
(str1[i]==str2[j]) n++;
137
return
n;
138
}
139
/*****************************************************************************/
140
141
/*****************************************************************************/
146
int
strUppercaseCount
(
148
const
char
*s
149
) {
150
unsigned
int
n=0, i;
151
if
(s==NULL || strlen(s)==0)
return
(
int
)n;
152
for
(i=0; i<strlen(s); i++)
if
(isupper(s[i])) n++;
153
return
(
int
)n;
154
}
155
/*****************************************************************************/
156
157
/*****************************************************************************/
159
void
strReplaceChar
(
161
char
*str,
163
char
c1,
165
char
c2
166
) {
167
char
*cptr;
168
if
(strlen(str)==0)
return
;
169
while
((cptr=strchr(str, c1))!=NULL) *cptr=c2;
170
return
;
171
}
172
/*****************************************************************************/
173
174
/*****************************************************************************/
175
#ifndef HAVE_STRNLEN
181
size_t
strnlen
(
183
const
char
*s,
186
size_t
n
187
) {
188
char
*ps=(
char
*)s;
189
size_t
i=0;
190
while
(i<n && *ps!=
'\0'
) {i++; ps++;}
191
return
(i);
192
}
193
#endif
// HAVE_STRNLEN
194
/*****************************************************************************/
195
196
/*****************************************************************************/
197
#ifndef HAVE_STRLCAT
205
size_t
strlcat
(
207
char
*dst,
209
const
char
*src,
212
size_t
dstsize
213
) {
214
char
*d;
215
const
char
*s=src;
216
size_t
dlen, n;
217
218
/* Find the current length of dst */
219
dlen=
strnlen
(dst, dstsize);
220
if
(s==NULL)
return
(dlen);
221
n=dstsize-dlen;
222
if
(n==0)
return
(dlen+strlen(s));
223
d=dst+dlen;
224
while
(*s!=
'\0'
) {
225
if
(n!=1) {*d=*s; d++; n--;}
226
s++;
227
}
228
*d=
'\0'
;
229
return
(dlen+(s-src));
230
}
231
#endif
// HAVE_STRLCAT
232
/*****************************************************************************/
233
234
/*****************************************************************************/
235
#ifndef HAVE_STRLCPY
244
size_t
strlcpy
(
246
char
*dst,
248
const
char
*src,
251
size_t
dstsize
252
) {
253
if
(dstsize>0) dst[0]=
'\0'
;
254
if
(strlen(src)==0)
return
(0);
255
256
char
*d=dst;
257
const
char
*s=src;
258
size_t
n;
259
260
/* Copy as many chars as allowed */
261
n=dstsize;
262
if
(n!=0)
while
(--n!=0) {*d=*s;
if
(*d==
'\0'
) {d++; s++;
break
;} d++; s++;}
263
if
(n==0) {
// not enough space, add NUL, and check how much space were needed
264
if
(dstsize!=0) *d=
'\0'
;
265
while
(*s++) {}
266
}
267
return
(s-src-1);
268
}
269
#endif
// HAVE_STRLCPY
270
/*****************************************************************************/
271
272
/*****************************************************************************/
273
// This function is currently found in Linux gcc and macOS clang, but not in MinGW gcc
274
#ifndef HAVE_STRCASESTR
278
char
*
strcasestr
(
280
const
char
*haystack,
282
const
char
*needle
283
284
) {
285
if
(!*haystack || !*needle)
return
(0);
286
287
const
char
*s=haystack, *p=needle;
288
do
{
289
if
(!*p)
return
(
char
*)haystack;
290
if
((*p==*s) || (tolower(*p)==tolower(*s))) {
291
p++; s++;
292
}
else
{
293
p=needle;
if
(!*s)
return
(NULL);
294
s=++haystack;
295
}
296
}
while
(1);
297
return
*p ? NULL : (
char
*)haystack;
298
}
299
#endif
// HAVE_STRCASESTR
300
/*****************************************************************************/
301
302
/*****************************************************************************/
308
int
strncpyCleanSpaces
(
311
char
*s1,
313
const
char
*s2,
315
int
maxlen
316
) {
317
if
(s1==NULL)
return
(0);
318
s1[0]=(char)0;
if
(maxlen<1)
return
(0);
319
if
(maxlen<2) {strcpy(s1,
""
);
return
(0);}
320
if
(s2==NULL || strlen(s2)<1)
return
(0);
321
322
char
*cptr;
323
int
i;
324
325
cptr=(
char
*)s2; cptr+=strspn(s2,
"\t\n\r "
);
326
strlcpy
(s1, cptr, maxlen); i=strlen(s1);
if
(i<1)
return
(0);
327
cptr=s1+(i-1);
328
while
(i>0) {
329
if
(*cptr!=
'\t'
&& *cptr!=
'\n'
&& *cptr!=
'\r'
&& *cptr!=
' '
)
break
;
330
i--; s1[i]=(char)0; cptr--;
331
}
332
return
(i);
333
}
334
/*****************************************************************************/
335
336
/*****************************************************************************/
343
int
strCleanSpaces
(
345
char
*s
346
) {
347
if
(s==NULL)
return
(0);
348
int
len=strlen(s);
if
(len<0)
return
(0);
349
char
*s2; s2=strdup(s);
if
(s2==NULL)
return
(1);
350
int
n=
strncpyCleanSpaces
(s2, s, len+1);
351
if
(n<1) strcpy(s,
""
);
else
strcpy(s, s2);
352
free(s2);
353
return
(0);
354
}
355
/*****************************************************************************/
356
357
/*****************************************************************************/
364
char
*
strEncodeForXML
(
366
const
char
*s
367
) {
368
if
(s==NULL)
return
(NULL);
369
/* Count the characters needing encoding */
370
int
n=0;
371
for
(
size_t
i=0; i<strlen(s); i++) {
372
if
(s[i]==
'&'
) {n++;
continue
;}
373
if
(s[i]==
'\''
) {n++;
continue
;}
374
if
(s[i]==
'\"'
) {n++;
continue
;}
375
if
(s[i]==
'<'
) {n++;
continue
;}
376
if
(s[i]==
'>'
) {n++;
continue
;}
377
}
378
if
(n==0)
return
(NULL);
379
/* Allocate memory for new string (one char to max 6 chars) */
380
n*=5; n+=strlen(s)+1;
381
char
*ns=(
char
*)malloc(n*
sizeof
(
char
));
382
if
(ns==NULL)
return
(NULL);
383
/* Process the string */
384
for
(
int
i=0; i<n; i++) ns[i]=(
char
)0;
385
for
(
size_t
i=0; i<strlen(s); i++) {
386
if
(s[i]==
'&'
) {strcat(ns,
"&"
);
continue
;}
387
if
(s[i]==
'\''
) {strcat(ns,
"'"
);
continue
;}
388
if
(s[i]==
'\"'
) {strcat(ns,
"""
);
continue
;}
389
if
(s[i]==
'<'
) {strcat(ns,
"<"
);
continue
;}
390
if
(s[i]==
'>'
) {strcat(ns,
">"
);
continue
;}
391
ns[strlen(ns)]=s[i];
392
}
393
return
(ns);
394
}
395
/*****************************************************************************/
396
397
/*****************************************************************************/
402
void
strCleanForXML
(
404
char
*s
405
) {
406
if
(s==NULL || *s==
'\0'
)
return
;
407
strReplaceChar
(s,
'&'
,
'-'
);
408
strReplaceChar
(s,
'\''
,
'-'
);
409
strReplaceChar
(s,
'\"'
,
'-'
);
410
strReplaceChar
(s,
'<'
,
'-'
);
411
strReplaceChar
(s,
'>'
,
'-'
);
412
return
;
413
}
414
/*****************************************************************************/
415
416
/*****************************************************************************/
libtpcmisc.h
Header file for libtpcmisc.
strCleanForXML
void strCleanForXML(char *s)
Definition
strext.c:402
strCleanSpaces
int strCleanSpaces(char *s)
Definition
strext.c:343
strEncodeForXML
char * strEncodeForXML(const char *s)
Definition
strext.c:364
strChrCount
int strChrCount(const char *str1, const char *str2)
Definition
strext.c:126
strReplaceChar
void strReplaceChar(char *str, char c1, char c2)
Definition
strext.c:159
strnlen
size_t strnlen(const char *s, size_t n)
Definition
strext.c:181
strTokenNCpy
int strTokenNCpy(const char *str1, const char *str2, int i, char *str3, int count)
Definition
strext.c:45
strncpyCleanSpaces
int strncpyCleanSpaces(char *s1, const char *s2, int maxlen)
Definition
strext.c:308
strTokenNr
int strTokenNr(const char *str1, const char *str2)
Definition
strext.c:17
strlcpy
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition
strext.c:244
strTokenDup
char * strTokenDup(const char *s1, const char *s2, int *next)
Definition
strext.c:89
strUppercaseCount
int strUppercaseCount(const char *s)
Definition
strext.c:146
strcasestr
char * strcasestr(const char *haystack, const char *needle)
Definition
strext.c:278
strlcat
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition
strext.c:205
Generated on
for TPCCLIB by
1.17.0