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

Annotation of /trunk/Src/wptUtil.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 340 - (hide annotations)
Sun Nov 27 13:15:07 2011 UTC (13 years, 3 months ago) by twoaday
File size: 3986 byte(s)


1 werner 36 /* wptUtil.cpp - Helper functions
2 twoaday 328 * Copyright (C) 2005, 2006, 2008 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 twoaday 328 #include "StringBuffer.h"
28 twoaday 271
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 twoaday 328 for (buf=t++, buflen = n--, s++;
81     n && toupper(*t) == toupper(*s); t++, s++, n--)
82 werner 36 ;
83 twoaday 278 if (!*s)
84 werner 36 return buf;
85 twoaday 328 t = buf;
86     n = buflen;
87     s = sub;
88 werner 36 }
89     }
90     return NULL ;
91     }
92    
93 twoaday 190
94 twoaday 247 /* 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 twoaday 278 if (!is_path && (strchr (fname, ':') || strchr (fname, '\\')))
104     return -1;
105 twoaday 247
106 twoaday 328 for (size_t i=0; i < strlen (fname); i++) {
107 twoaday 247 if (strchr (not_allowed, fname[i]))
108     return -1;
109     }
110     return 0;
111     }
112    
113    
114 twoaday 190 /* 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 twoaday 214 const char *allowed = "@._-%+;";
120 twoaday 260 size_t i, len = strlen (email);
121 twoaday 190
122 twoaday 260 if (len < 3 || !strchr (email, '@'))
123 twoaday 190 return -1;
124 twoaday 260 for (i=0; i < len; i++) {
125 twoaday 225 if (isxdigit (email[i]) || isalpha (email[i])
126 twoaday 190 || strchr (allowed, email[i]))
127     continue;
128     else
129     return -1;
130     }
131     return 0;
132     }
133 twoaday 214
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 twoaday 225 p = new char[end-begin+1];
147 twoaday 214 if (!p)
148 twoaday 273 BUG (0);
149 twoaday 214 for (i = begin, pos=0; i < end; i++)
150     p[pos++] = str[i];
151     p[pos] = '\0';
152     return p;
153     }
154 twoaday 217
155    
156    
157 twoaday 271
158 twoaday 273 /* Safe strdup version (C++ version). */
159 twoaday 271 char*
160     m_strdup (const char *str)
161     {
162 twoaday 273 char *p = new char[strlen (str) + 1];
163 twoaday 271 if (!p)
164 twoaday 328 BUG (0);
165 twoaday 271 strcpy (p, str);
166     return p;
167     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26