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 |
|
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
23 |
#endif |
24 |
|
25 |
#include <string.h> |
26 |
#include <stdio.h> |
27 |
#include <ctype.h> |
28 |
|
29 |
/* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */ |
30 |
char* |
31 |
strsep (char **stringp, const char *delim) |
32 |
{ |
33 |
char *begin, *end; |
34 |
begin = *stringp; |
35 |
|
36 |
if (begin == NULL) |
37 |
return NULL; |
38 |
|
39 |
/* A frequent case is when the delimiter string contains only one |
40 |
character. Here we don't need to call the expensive `strpbrk' |
41 |
function and instead work using `strchr'. */ |
42 |
if (delim[0] == '\0' || delim[1] == '\0') { |
43 |
char ch = delim[0]; |
44 |
if (ch == '\0') |
45 |
end = NULL; |
46 |
else { |
47 |
if (*begin == ch) |
48 |
end = begin; |
49 |
else if (*begin == '\0') |
50 |
end = NULL; |
51 |
else |
52 |
end = strchr (begin + 1, ch); |
53 |
} |
54 |
} |
55 |
else /* Find the end of the token. */ |
56 |
end = strpbrk (begin, delim); |
57 |
if ( end ){ |
58 |
/* Terminate the token and set *STRINGP past NUL character. */ |
59 |
*end++ = '\0'; |
60 |
*stringp = end; |
61 |
} |
62 |
else |
63 |
/* No more delimiters; this is the last token. */ |
64 |
*stringp = NULL; |
65 |
|
66 |
return begin; |
67 |
} |
68 |
|
69 |
|
70 |
/* Like strstr but this version is case in-sentensive. */ |
71 |
const char * |
72 |
stristr (const char *buf, const char *sub) |
73 |
{ |
74 |
const char *t, *s ; |
75 |
size_t n; |
76 |
size_t buflen = strlen (buf); |
77 |
|
78 |
for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) { |
79 |
if( toupper(*t) == toupper(*s) ) { |
80 |
for( buf=t++, buflen = n--, s++; |
81 |
n && toupper(*t) == toupper(*s); t++, s++, n-- ) |
82 |
; |
83 |
if( !*s ) |
84 |
return buf; |
85 |
t = buf; n = buflen; s = sub ; |
86 |
} |
87 |
} |
88 |
return NULL ; |
89 |
} |
90 |
|
91 |
|
92 |
/* Check if the email address @email only contain valid characters. |
93 |
Return 0 on success. */ |
94 |
int |
95 |
check_email_address (const char *email) |
96 |
{ |
97 |
const char *allowed = "@._-%+"; |
98 |
size_t i; |
99 |
|
100 |
if (!strchr (email, '@')) |
101 |
return -1; |
102 |
for (i=0; i < strlen (email); i++) { |
103 |
if (isdigit (email[i]) || isalpha (email[i]) |
104 |
|| strchr (allowed, email[i])) |
105 |
continue; |
106 |
else |
107 |
return -1; |
108 |
} |
109 |
return 0; |
110 |
} |