1 |
twoaday |
25 |
/* 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 |
twoaday |
32 |
|
65 |
|
|
/* Like strstr but this version is case in-sentensive. */ |
66 |
twoaday |
25 |
const char * |
67 |
|
|
stristr (const char *buf, const char *sub) |
68 |
|
|
{ |
69 |
|
|
const char *t, *s ; |
70 |
|
|
size_t n; |
71 |
|
|
size_t buflen = strlen (buf); |
72 |
|
|
|
73 |
|
|
for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) { |
74 |
|
|
if( toupper(*t) == toupper(*s) ) { |
75 |
|
|
for( buf=t++, buflen = n--, s++; |
76 |
|
|
n && toupper(*t) == toupper(*s); t++, s++, n-- ) |
77 |
|
|
; |
78 |
|
|
if( !*s ) |
79 |
|
|
return buf; |
80 |
|
|
t = buf; n = buflen; s = sub ; |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
return NULL ; |
84 |
|
|
} |
85 |
|
|
|