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

Diff of /trunk/Src/wptUtil.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 60 by twoaday, Wed Nov 2 14:35:48 2005 UTC revision 271 by twoaday, Sun Nov 5 08:57:45 2006 UTC
# Line 17  Line 17 
17   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
18   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19   */   */
20    
21  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
22  #include <config.h>  #include <config.h>
23  #endif  #endif
24    
25    #include <windows.h>
26  #include <string.h>  #include <string.h>
27  #include <string.h>  #include <stdlib.h>
28  #include <stdio.h>  #include <stdio.h>
29  #include <ctype.h>  #include <ctype.h>
30    
31    #include "wptTypes.h"
32    
33  /* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */  /* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */
34  extern "C" char*  char*
35  strsep (char **stringp, const char *delim)  strsep (char **stringp, const char *delim)
36  {  {
37      char *begin, *end;      char *begin, *end;
# Line 54  strsep (char **stringp, const char *deli Line 58  strsep (char **stringp, const char *deli
58      }      }
59      else /* Find the end of the token.  */      else /* Find the end of the token.  */
60          end = strpbrk (begin, delim);          end = strpbrk (begin, delim);
61      if ( end ){      if (end) {
62          /* Terminate the token and set *STRINGP past NUL character.  */          /* Terminate the token and set *STRINGP past NUL character.  */
63          *end++ = '\0';          *end++ = '\0';
64          *stringp = end;          *stringp = end;
# Line 68  strsep (char **stringp, const char *deli Line 72  strsep (char **stringp, const char *deli
72    
73    
74  /* Like strstr but this version is case in-sentensive. */  /* Like strstr but this version is case in-sentensive. */
75  extern "C" const char *  const char *
76  stristr (const char *buf, const char *sub)  stristr (const char *buf, const char *sub)
77  {  {
78      const char *t, *s ;      const char *t, *s ;
# Line 88  stristr (const char *buf, const char *su Line 92  stristr (const char *buf, const char *su
92      return NULL ;      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            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    
161    
162    /* Remove %AB sequences from the input buffer @in
163       and store the raw data in @out. */
164    void
165    unhexify_buffer (const char *in, char **r_out)
166    {
167        char temp[3], *out;
168        size_t len, pos, i=0;
169    
170        len = strlen (in);
171        out = new char[len+1];
172        if (!out)
173            abort ();
174        memset (out, 0, len+1);
175        for (pos = 0; pos < len; pos++) {
176            if (in[pos] == '%' && in[pos+1] == '%')
177                out[i++] = '%';
178            else if (in[pos] == '%') {
179                temp[0] = in[++pos];
180                temp[1] = in[++pos];
181                temp[2] = 0;
182                out[i++] = (char)strtoul (temp, NULL, 16);
183            }
184            else
185                out[i++] = in[pos];
186        }
187        out[i] = 0;
188        *r_out = out;
189    }
190    
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    }

Legend:
Removed from v.60  
changed lines
  Added in v.271

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26