1 |
/* wptUtil.cpp - Helper functions |
2 |
* Copyright (C) 2005, 2006, 2008 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 |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
18 |
#endif |
19 |
|
20 |
#include <windows.h> |
21 |
#include <string.h> |
22 |
#include <stdlib.h> |
23 |
#include <stdio.h> |
24 |
#include <ctype.h> |
25 |
|
26 |
#include "wptTypes.h" |
27 |
#include "StringBuffer.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; |
86 |
n = buflen; |
87 |
s = sub; |
88 |
} |
89 |
} |
90 |
return NULL ; |
91 |
} |
92 |
|
93 |
|
94 |
/* Check if @fname is a valid file name on a Window file system. |
95 |
If is_path = 1, @fname is treated as a file name without any separators. */ |
96 |
int |
97 |
check_file_name (const char *fname, int is_path) |
98 |
{ |
99 |
const char *not_allowed = "/*?\"<>|"; |
100 |
|
101 |
/* If we are not in path mode (@is_path = 1) we also consider |
102 |
path separators as illegal chars. */ |
103 |
if (!is_path && (strchr (fname, ':') || strchr (fname, '\\'))) |
104 |
return -1; |
105 |
|
106 |
for (size_t i=0; i < strlen (fname); i++) { |
107 |
if (strchr (not_allowed, fname[i])) |
108 |
return -1; |
109 |
} |
110 |
return 0; |
111 |
} |
112 |
|
113 |
|
114 |
/* Check if the email address @email only contain valid characters. |
115 |
Return 0 on success. */ |
116 |
int |
117 |
check_email_address (const char *email) |
118 |
{ |
119 |
const char *allowed = "@._-%+;"; |
120 |
size_t i, len = strlen (email); |
121 |
|
122 |
if (len < 3 || !strchr (email, '@')) |
123 |
return -1; |
124 |
for (i=0; i < len; i++) { |
125 |
if (isxdigit (email[i]) || isalpha (email[i]) |
126 |
|| strchr (allowed, email[i])) |
127 |
continue; |
128 |
else |
129 |
return -1; |
130 |
} |
131 |
return 0; |
132 |
} |
133 |
|
134 |
|
135 |
/* Return a substring of @str from the position @begin |
136 |
to position @end. */ |
137 |
char* |
138 |
substr (const char *str, unsigned int begin, unsigned int end) |
139 |
{ |
140 |
char *p; |
141 |
size_t i, pos; |
142 |
|
143 |
if (end > strlen (str) || begin > strlen (str) || (end-begin) < 0) |
144 |
return NULL; |
145 |
|
146 |
p = new char[end-begin+1]; |
147 |
if (!p) |
148 |
BUG (0); |
149 |
for (i = begin, pos=0; i < end; i++) |
150 |
p[pos++] = str[i]; |
151 |
p[pos] = '\0'; |
152 |
return p; |
153 |
} |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
/* Safe strdup version (C++ version). */ |
159 |
char* |
160 |
m_strdup (const char *str) |
161 |
{ |
162 |
char *p = new char[strlen (str) + 1]; |
163 |
if (!p) |
164 |
BUG (0); |
165 |
strcpy (p, str); |
166 |
return p; |
167 |
} |