1 |
/* wptUtil.cpp - Helper functions |
2 |
* Copyright (C) 2005 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
#include <string.h> |
21 |
#include <stdio.h> |
22 |
#include <ctype.h> |
23 |
|
24 |
/* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */ |
25 |
char* |
26 |
strsep (char **stringp, const char *delim) |
27 |
{ |
28 |
char *begin, *end; |
29 |
begin = *stringp; |
30 |
|
31 |
if (begin == NULL) |
32 |
return NULL; |
33 |
|
34 |
/* A frequent case is when the delimiter string contains only one |
35 |
character. Here we don't need to call the expensive `strpbrk' |
36 |
function and instead work using `strchr'. */ |
37 |
if (delim[0] == '\0' || delim[1] == '\0') { |
38 |
char ch = delim[0]; |
39 |
if (ch == '\0') |
40 |
end = NULL; |
41 |
else { |
42 |
if (*begin == ch) |
43 |
end = begin; |
44 |
else if (*begin == '\0') |
45 |
end = NULL; |
46 |
else |
47 |
end = strchr (begin + 1, ch); |
48 |
} |
49 |
} |
50 |
else /* Find the end of the token. */ |
51 |
end = strpbrk (begin, delim); |
52 |
if ( end ){ |
53 |
/* Terminate the token and set *STRINGP past NUL character. */ |
54 |
*end++ = '\0'; |
55 |
*stringp = end; |
56 |
} |
57 |
else |
58 |
/* No more delimiters; this is the last token. */ |
59 |
*stringp = NULL; |
60 |
|
61 |
return begin; |
62 |
} |
63 |
|
64 |
const char * |
65 |
stristr (const char *buf, const char *sub) |
66 |
{ |
67 |
const char *t, *s ; |
68 |
size_t n; |
69 |
size_t buflen = strlen (buf); |
70 |
|
71 |
for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) { |
72 |
if( toupper(*t) == toupper(*s) ) { |
73 |
for( buf=t++, buflen = n--, s++; |
74 |
n && toupper(*t) == toupper(*s); t++, s++, n-- ) |
75 |
; |
76 |
if( !*s ) |
77 |
return buf; |
78 |
t = buf; n = buflen; s = sub ; |
79 |
} |
80 |
} |
81 |
return NULL ; |
82 |
} |
83 |
|