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

Annotation of /trunk/Src/wptUtil.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 271 - (hide annotations)
Sun Nov 5 08:57:45 2006 UTC (18 years, 3 months ago) by twoaday
File size: 4742 byte(s)


1 werner 36 /* 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 twoaday 190
21 werner 36 #ifdef HAVE_CONFIG_H
22     #include <config.h>
23     #endif
24    
25 twoaday 271 #include <windows.h>
26 werner 36 #include <string.h>
27 twoaday 214 #include <stdlib.h>
28 werner 36 #include <stdio.h>
29     #include <ctype.h>
30    
31 twoaday 271 #include "wptTypes.h"
32    
33 werner 36 /* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */
34 twoaday 190 char*
35 werner 36 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 twoaday 217 if (end) {
62 werner 36 /* 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 twoaday 190 const char *
76 werner 36 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 twoaday 190
96 twoaday 247 /* 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 twoaday 190 /* 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 twoaday 214 const char *allowed = "@._-%+;";
125 twoaday 260 size_t i, len = strlen (email);
126 twoaday 190
127 twoaday 260 if (len < 3 || !strchr (email, '@'))
128 twoaday 190 return -1;
129 twoaday 260 for (i=0; i < len; i++) {
130 twoaday 225 if (isxdigit (email[i]) || isalpha (email[i])
131 twoaday 190 || strchr (allowed, email[i]))
132     continue;
133     else
134     return -1;
135     }
136     return 0;
137     }
138 twoaday 214
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 twoaday 225 p = new char[end-begin+1];
152 twoaday 214 if (!p)
153     abort ();
154    
155     for (i = begin, pos=0; i < end; i++)
156     p[pos++] = str[i];
157     p[pos] = '\0';
158     return p;
159     }
160 twoaday 217
161    
162     /* Remove %AB sequences from the input buffer @in
163     and store the raw data in @out. */
164     void
165 twoaday 225 unhexify_buffer (const char *in, char **r_out)
166 twoaday 217 {
167 twoaday 225 char temp[3], *out;
168     size_t len, pos, i=0;
169 twoaday 217
170 twoaday 225 len = strlen (in);
171     out = new char[len+1];
172     if (!out)
173 twoaday 217 abort ();
174 twoaday 225 memset (out, 0, len+1);
175     for (pos = 0; pos < len; pos++) {
176 twoaday 217 if (in[pos] == '%' && in[pos+1] == '%')
177 twoaday 225 out[i++] = '%';
178 twoaday 217 else if (in[pos] == '%') {
179     temp[0] = in[++pos];
180     temp[1] = in[++pos];
181     temp[2] = 0;
182 twoaday 225 out[i++] = (char)strtoul (temp, NULL, 16);
183 twoaday 217 }
184     else
185 twoaday 225 out[i++] = in[pos];
186 twoaday 217 }
187 twoaday 225 out[i] = 0;
188     *r_out = out;
189 twoaday 217 }
190 twoaday 271
191     char*
192     m_strdup (const char *str)
193     {
194     char *p = new char[strlen (str)+1];
195     if (!p)
196     BUG (0);
197     strcpy (p, str);
198     return p;
199     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26