/[winpt]/trunk/Src/wptUtil.cpp
ViewVC logotype

Contents of /trunk/Src/wptUtil.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 273 - (show annotations)
Fri Dec 8 10:22:17 2006 UTC (18 years, 2 months ago) by twoaday
File size: 4793 byte(s)


1 /* wptUtil.cpp - Helper functions
2 * Copyright (C) 2005, 2006 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 <windows.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <ctype.h>
30
31 #include "wptTypes.h"
32
33 /* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */
34 char*
35 strsep (char **stringp, const char *delim)
36 {
37 char *begin, *end;
38 begin = *stringp;
39
40 if (begin == NULL)
41 return NULL;
42
43 /* A frequent case is when the delimiter string contains only one
44 character. Here we don't need to call the expensive `strpbrk'
45 function and instead work using `strchr'. */
46 if (delim[0] == '\0' || delim[1] == '\0') {
47 char ch = delim[0];
48 if (ch == '\0')
49 end = NULL;
50 else {
51 if (*begin == ch)
52 end = begin;
53 else if (*begin == '\0')
54 end = NULL;
55 else
56 end = strchr (begin + 1, ch);
57 }
58 }
59 else /* Find the end of the token. */
60 end = strpbrk (begin, delim);
61 if (end) {
62 /* Terminate the token and set *STRINGP past NUL character. */
63 *end++ = '\0';
64 *stringp = end;
65 }
66 else
67 /* No more delimiters; this is the last token. */
68 *stringp = NULL;
69
70 return begin;
71 }
72
73
74 /* Like strstr but this version is case in-sentensive. */
75 const char *
76 stristr (const char *buf, const char *sub)
77 {
78 const char *t, *s ;
79 size_t n;
80 size_t buflen = strlen (buf);
81
82 for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) {
83 if( toupper(*t) == toupper(*s) ) {
84 for( buf=t++, buflen = n--, s++;
85 n && toupper(*t) == toupper(*s); t++, s++, n-- )
86 ;
87 if( !*s )
88 return buf;
89 t = buf; n = buflen; s = sub ;
90 }
91 }
92 return NULL ;
93 }
94
95
96 /* Check if @fname is a valid file name on a Window file system.
97 If is_path = 1, @fname is treated as a file name without any separators. */
98 int
99 check_file_name (const char *fname, int is_path)
100 {
101 const char *not_allowed = "/*?\"<>|";
102 size_t i;
103
104 /* If we are not in path mode (@is_path = 1) we also consider
105 path separators as illegal chars. */
106 if (!is_path) {
107 if (strchr (fname, ':') || strchr (fname, '\\'))
108 return -1;
109 }
110
111 for (i=0; i < strlen (fname); i++) {
112 if (strchr (not_allowed, fname[i]))
113 return -1;
114 }
115 return 0;
116 }
117
118
119 /* Check if the email address @email only contain valid characters.
120 Return 0 on success. */
121 int
122 check_email_address (const char *email)
123 {
124 const char *allowed = "@._-%+;";
125 size_t i, len = strlen (email);
126
127 if (len < 3 || !strchr (email, '@'))
128 return -1;
129 for (i=0; i < len; i++) {
130 if (isxdigit (email[i]) || isalpha (email[i])
131 || strchr (allowed, email[i]))
132 continue;
133 else
134 return -1;
135 }
136 return 0;
137 }
138
139
140 /* Return a substring of @str from the position @begin
141 to position @end. */
142 char*
143 substr (const char *str, unsigned int begin, unsigned int end)
144 {
145 char *p;
146 size_t i, pos;
147
148 if (end > strlen (str) || begin > strlen (str) || (end-begin) < 0)
149 return NULL;
150
151 p = new char[end-begin+1];
152 if (!p)
153 BUG (0);
154 for (i = begin, pos=0; i < end; i++)
155 p[pos++] = str[i];
156 p[pos] = '\0';
157 return p;
158 }
159
160
161 /* Remove %AB sequences from the input buffer @in
162 and store the raw data in @out. */
163 void
164 unhexify_buffer (const char *in, char **r_out)
165 {
166 char temp[3], *out;
167 size_t len, pos, i=0;
168
169 len = strlen (in);
170 out = new char[len+1];
171 if (!out)
172 abort ();
173 memset (out, 0, len+1);
174 for (pos = 0; pos < len; pos++) {
175 if (in[pos] == '%' && in[pos+1] == '%')
176 out[i++] = '%';
177 else if (in[pos] == '%') {
178 temp[0] = in[++pos];
179 temp[1] = in[++pos];
180 temp[2] = 0;
181 out[i++] = (char)strtoul (temp, NULL, 16);
182 }
183 else
184 out[i++] = in[pos];
185 }
186 out[i] = 0;
187 *r_out = out;
188 }
189
190
191 /* Safe strdup version (C++ version). */
192 char*
193 m_strdup (const char *str)
194 {
195 char *p = new char[strlen (str) + 1];
196 if (!p)
197 BUG (NULL);
198 strcpy (p, str);
199 return p;
200 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26