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

Annotation of /trunk/Src/wptUtil.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 278 - (hide annotations)
Mon Jan 15 22:02:04 2007 UTC (18 years, 1 month ago) by twoaday
File size: 4573 byte(s)
See ChangeLog.


1 werner 36 /* wptUtil.cpp - Helper functions
2 twoaday 273 * Copyright (C) 2005, 2006 Timo Schulz
3 werner 36 *
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 twoaday 271 #include <windows.h>
21 werner 36 #include <string.h>
22 twoaday 214 #include <stdlib.h>
23 werner 36 #include <stdio.h>
24     #include <ctype.h>
25    
26 twoaday 271 #include "wptTypes.h"
27    
28 twoaday 278
29 werner 36 /* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */
30 twoaday 190 char*
31 werner 36 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 twoaday 217 if (end) {
58 werner 36 /* 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 twoaday 190 const char *
72 werner 36 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 twoaday 278 for (t=buf, n=buflen, s=sub ; n ; t++, n--) {
79     if (toupper (*t) == toupper (*s)) {
80 werner 36 for( buf=t++, buflen = n--, s++;
81     n && toupper(*t) == toupper(*s); t++, s++, n-- )
82     ;
83 twoaday 278 if (!*s)
84 werner 36 return buf;
85     t = buf; n = buflen; s = sub ;
86     }
87     }
88     return NULL ;
89     }
90    
91 twoaday 190
92 twoaday 247 /* Check if @fname is a valid file name on a Window file system.
93     If is_path = 1, @fname is treated as a file name without any separators. */
94     int
95     check_file_name (const char *fname, int is_path)
96     {
97     const char *not_allowed = "/*?\"<>|";
98     size_t i;
99    
100     /* If we are not in path mode (@is_path = 1) we also consider
101     path separators as illegal chars. */
102 twoaday 278 if (!is_path && (strchr (fname, ':') || strchr (fname, '\\')))
103     return -1;
104 twoaday 247
105     for (i=0; i < strlen (fname); i++) {
106     if (strchr (not_allowed, fname[i]))
107     return -1;
108     }
109     return 0;
110     }
111    
112    
113 twoaday 190 /* Check if the email address @email only contain valid characters.
114     Return 0 on success. */
115     int
116     check_email_address (const char *email)
117     {
118 twoaday 214 const char *allowed = "@._-%+;";
119 twoaday 260 size_t i, len = strlen (email);
120 twoaday 190
121 twoaday 260 if (len < 3 || !strchr (email, '@'))
122 twoaday 190 return -1;
123 twoaday 260 for (i=0; i < len; i++) {
124 twoaday 225 if (isxdigit (email[i]) || isalpha (email[i])
125 twoaday 190 || strchr (allowed, email[i]))
126     continue;
127     else
128     return -1;
129     }
130     return 0;
131     }
132 twoaday 214
133    
134     /* Return a substring of @str from the position @begin
135     to position @end. */
136     char*
137     substr (const char *str, unsigned int begin, unsigned int end)
138     {
139     char *p;
140     size_t i, pos;
141    
142     if (end > strlen (str) || begin > strlen (str) || (end-begin) < 0)
143     return NULL;
144    
145 twoaday 225 p = new char[end-begin+1];
146 twoaday 214 if (!p)
147 twoaday 273 BUG (0);
148 twoaday 214 for (i = begin, pos=0; i < end; i++)
149     p[pos++] = str[i];
150     p[pos] = '\0';
151     return p;
152     }
153 twoaday 217
154    
155     /* Remove %AB sequences from the input buffer @in
156     and store the raw data in @out. */
157     void
158 twoaday 225 unhexify_buffer (const char *in, char **r_out)
159 twoaday 217 {
160 twoaday 225 char temp[3], *out;
161     size_t len, pos, i=0;
162 twoaday 217
163 twoaday 225 len = strlen (in);
164     out = new char[len+1];
165     if (!out)
166 twoaday 217 abort ();
167 twoaday 225 memset (out, 0, len+1);
168     for (pos = 0; pos < len; pos++) {
169 twoaday 217 if (in[pos] == '%' && in[pos+1] == '%')
170 twoaday 225 out[i++] = '%';
171 twoaday 217 else if (in[pos] == '%') {
172     temp[0] = in[++pos];
173     temp[1] = in[++pos];
174     temp[2] = 0;
175 twoaday 225 out[i++] = (char)strtoul (temp, NULL, 16);
176 twoaday 217 }
177     else
178 twoaday 225 out[i++] = in[pos];
179 twoaday 217 }
180 twoaday 225 out[i] = 0;
181     *r_out = out;
182 twoaday 217 }
183 twoaday 271
184 twoaday 273
185     /* Safe strdup version (C++ version). */
186 twoaday 271 char*
187     m_strdup (const char *str)
188     {
189 twoaday 273 char *p = new char[strlen (str) + 1];
190 twoaday 271 if (!p)
191 twoaday 273 BUG (NULL);
192 twoaday 271 strcpy (p, str);
193     return p;
194     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26