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 <stdlib.h> |
27 |
#include <stdio.h> |
28 |
#include <ctype.h> |
29 |
|
30 |
/* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */ |
31 |
char* |
32 |
strsep (char **stringp, const char *delim) |
33 |
{ |
34 |
char *begin, *end; |
35 |
begin = *stringp; |
36 |
|
37 |
if (begin == NULL) |
38 |
return NULL; |
39 |
|
40 |
/* A frequent case is when the delimiter string contains only one |
41 |
character. Here we don't need to call the expensive `strpbrk' |
42 |
function and instead work using `strchr'. */ |
43 |
if (delim[0] == '\0' || delim[1] == '\0') { |
44 |
char ch = delim[0]; |
45 |
if (ch == '\0') |
46 |
end = NULL; |
47 |
else { |
48 |
if (*begin == ch) |
49 |
end = begin; |
50 |
else if (*begin == '\0') |
51 |
end = NULL; |
52 |
else |
53 |
end = strchr (begin + 1, ch); |
54 |
} |
55 |
} |
56 |
else /* Find the end of the token. */ |
57 |
end = strpbrk (begin, delim); |
58 |
if (end) { |
59 |
/* Terminate the token and set *STRINGP past NUL character. */ |
60 |
*end++ = '\0'; |
61 |
*stringp = end; |
62 |
} |
63 |
else |
64 |
/* No more delimiters; this is the last token. */ |
65 |
*stringp = NULL; |
66 |
|
67 |
return begin; |
68 |
} |
69 |
|
70 |
|
71 |
/* Like strstr but this version is case in-sentensive. */ |
72 |
const char * |
73 |
stristr (const char *buf, const char *sub) |
74 |
{ |
75 |
const char *t, *s ; |
76 |
size_t n; |
77 |
size_t buflen = strlen (buf); |
78 |
|
79 |
for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) { |
80 |
if( toupper(*t) == toupper(*s) ) { |
81 |
for( buf=t++, buflen = n--, s++; |
82 |
n && toupper(*t) == toupper(*s); t++, s++, n-- ) |
83 |
; |
84 |
if( !*s ) |
85 |
return buf; |
86 |
t = buf; n = buflen; s = sub ; |
87 |
} |
88 |
} |
89 |
return NULL ; |
90 |
} |
91 |
|
92 |
|
93 |
/* Check if the email address @email only contain valid characters. |
94 |
Return 0 on success. */ |
95 |
int |
96 |
check_email_address (const char *email) |
97 |
{ |
98 |
const char *allowed = "@._-%+;"; |
99 |
size_t i; |
100 |
|
101 |
if (!strchr (email, '@')) |
102 |
return -1; |
103 |
for (i=0; i < strlen (email); i++) { |
104 |
if (isxdigit (email[i]) || isalpha (email[i]) |
105 |
|| strchr (allowed, email[i])) |
106 |
continue; |
107 |
else |
108 |
return -1; |
109 |
} |
110 |
return 0; |
111 |
} |
112 |
|
113 |
|
114 |
/* Return a substring of @str from the position @begin |
115 |
to position @end. */ |
116 |
char* |
117 |
substr (const char *str, unsigned int begin, unsigned int end) |
118 |
{ |
119 |
char *p; |
120 |
size_t i, pos; |
121 |
|
122 |
if (end > strlen (str) || begin > strlen (str) || (end-begin) < 0) |
123 |
return NULL; |
124 |
|
125 |
p = new char[end-begin+1]; |
126 |
if (!p) |
127 |
abort (); |
128 |
|
129 |
for (i = begin, pos=0; i < end; i++) |
130 |
p[pos++] = str[i]; |
131 |
p[pos] = '\0'; |
132 |
return p; |
133 |
} |
134 |
|
135 |
|
136 |
/* Remove %AB sequences from the input buffer @in |
137 |
and store the raw data in @out. */ |
138 |
void |
139 |
unhexify_buffer (const char *in, char **r_out) |
140 |
{ |
141 |
char temp[3], *out; |
142 |
size_t len, pos, i=0; |
143 |
|
144 |
len = strlen (in); |
145 |
out = new char[len+1]; |
146 |
if (!out) |
147 |
abort (); |
148 |
memset (out, 0, len+1); |
149 |
for (pos = 0; pos < len; pos++) { |
150 |
if (in[pos] == '%' && in[pos+1] == '%') |
151 |
out[i++] = '%'; |
152 |
else if (in[pos] == '%') { |
153 |
temp[0] = in[++pos]; |
154 |
temp[1] = in[++pos]; |
155 |
temp[2] = 0; |
156 |
out[i++] = (char)strtoul (temp, NULL, 16); |
157 |
} |
158 |
else |
159 |
out[i++] = in[pos]; |
160 |
} |
161 |
out[i] = 0; |
162 |
*r_out = out; |
163 |
} |