/[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 25 by twoaday, Wed Oct 12 10:04:26 2005 UTC revision 260 by twoaday, Wed Aug 16 10:01:30 2006 UTC
# Line 1  Line 1 
1  /* wptUtil.cpp - Helper functions  /* wptUtil.cpp - Helper functions
2   *      Copyright (C) 2005 Timo Schulz   *      Copyright (C) 2005 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
6   * WinPT is free software; you can redistribute it and/or   * WinPT is free software; you can redistribute it and/or
7   * modify it under the terms of the GNU General Public License   * modify it under the terms of the GNU General Public License
8   * as published by the Free Software Foundation; either version 2   * as published by the Free Software Foundation; either version 2
9   * of the License, or (at your option) any later version.   * of the License, or (at your option) any later version.
10   *     *  
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * General Public License for more details.   * General Public License for more details.
15   *   *
16   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
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  #include <string.h>  
21  #include <stdio.h>  #ifdef HAVE_CONFIG_H
22  #include <ctype.h>  #include <config.h>
23    #endif
24  /* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */  
25  char*  #include <string.h>
26  strsep (char **stringp, const char *delim)  #include <stdlib.h>
27  {  #include <stdio.h>
28      char *begin, *end;  #include <ctype.h>
29      begin = *stringp;  
30    /* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */
31      if (begin == NULL)  char*
32          return NULL;  strsep (char **stringp, const char *delim)
33    {
34      /* A frequent case is when the delimiter string contains only one      char *begin, *end;
35         character.  Here we don't need to call the expensive `strpbrk'      begin = *stringp;
36         function and instead work using `strchr'.  */  
37      if (delim[0] == '\0' || delim[1] == '\0') {      if (begin == NULL)
38          char ch = delim[0];          return NULL;
39          if (ch == '\0')  
40              end = NULL;      /* A frequent case is when the delimiter string contains only one
41          else {         character.  Here we don't need to call the expensive `strpbrk'
42              if (*begin == ch)         function and instead work using `strchr'.  */
43                  end = begin;                              if (delim[0] == '\0' || delim[1] == '\0') {
44              else if (*begin == '\0')          char ch = delim[0];
45                  end = NULL;          if (ch == '\0')
46              else              end = NULL;
47                  end = strchr (begin + 1, ch);          else {
48          }                if (*begin == ch)
49      }                  end = begin;                        
50      else /* Find the end of the token.  */              else if (*begin == '\0')
51          end = strpbrk (begin, delim);                  end = NULL;
52      if ( end ){              else
53          /* Terminate the token and set *STRINGP past NUL character.  */                  end = strchr (begin + 1, ch);
54          *end++ = '\0';          }  
55          *stringp = end;      }
56      }      else /* Find the end of the token.  */
57      else          end = strpbrk (begin, delim);
58          /* No more delimiters; this is the last token.  */      if (end) {
59          *stringp = NULL;          /* Terminate the token and set *STRINGP past NUL character.  */
60            *end++ = '\0';
61      return begin;          *stringp = end;
62  }      }
63        else
64  const char *          /* No more delimiters; this is the last token.  */
65  stristr (const char *buf, const char *sub)          *stringp = NULL;
66  {  
67      const char *t, *s ;      return begin;
68      size_t n;  }
69      size_t buflen = strlen (buf);  
70    
71      for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) {  /* Like strstr but this version is case in-sentensive. */
72          if( toupper(*t) == toupper(*s) ) {  const char *
73              for( buf=t++, buflen = n--, s++;  stristr (const char *buf, const char *sub)
74                   n && toupper(*t) == toupper(*s); t++, s++, n-- )  {
75                  ;      const char *t, *s ;
76              if( !*s )      size_t n;
77                  return buf;      size_t buflen = strlen (buf);
78              t = buf; n = buflen; s = sub ;  
79          }      for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) {
80      }          if( toupper(*t) == toupper(*s) ) {
81      return NULL ;              for( buf=t++, buflen = n--, s++;
82  }                   n && toupper(*t) == toupper(*s); t++, s++, n-- )
83                    ;
84                if( !*s )
85                    return buf;
86                t = buf; n = buflen; s = sub ;
87            }
88        }
89        return NULL ;
90    }
91    
92    
93    /* Check if @fname is a valid file name on a Window file system.
94       If is_path = 1, @fname is treated as a file name without any separators. */
95    int
96    check_file_name (const char *fname, int is_path)
97    {
98        const char *not_allowed = "/*?\"<>|";
99        size_t i;
100    
101        /* If we are not in path mode (@is_path = 1) we also consider
102           path separators as illegal chars. */
103        if (!is_path) {
104            if (strchr (fname, ':') || strchr (fname, '\\'))
105                return -1;
106        }
107    
108        for (i=0; i < strlen (fname); i++) {
109            if (strchr (not_allowed, fname[i]))
110                return -1;
111        }
112        return 0;
113    }
114    
115    
116    /* Check if the email address @email only contain valid characters.
117       Return 0 on success. */
118    int
119    check_email_address (const char *email)
120    {
121        const char *allowed = "@._-%+;";
122        size_t i, len = strlen (email);
123    
124        if (len < 3 || !strchr (email, '@'))
125            return -1;
126        for (i=0; i < len; i++) {
127            if (isxdigit (email[i]) || isalpha (email[i])
128                || strchr (allowed, email[i]))
129                continue;
130            else
131                return -1;
132        }
133        return 0;
134    }
135    
136    
137    /* Return a substring of @str from the position @begin
138       to position @end. */
139    char*
140    substr (const char *str, unsigned int begin, unsigned int end)
141    {    
142        char *p;
143        size_t i, pos;
144    
145        if (end > strlen (str) || begin > strlen (str) || (end-begin) < 0)
146            return NULL;
147    
148        p = new char[end-begin+1];
149        if (!p)
150            abort ();
151    
152        for (i = begin, pos=0; i < end; i++)
153            p[pos++] = str[i];
154        p[pos] = '\0';
155        return p;
156    }
157    
158    
159    /* Remove %AB sequences from the input buffer @in
160       and store the raw data in @out. */
161    void
162    unhexify_buffer (const char *in, char **r_out)
163    {
164        char temp[3], *out;
165        size_t len, pos, i=0;
166    
167        len = strlen (in);
168        out = new char[len+1];
169        if (!out)
170            abort ();
171        memset (out, 0, len+1);
172        for (pos = 0; pos < len; pos++) {
173            if (in[pos] == '%' && in[pos+1] == '%')
174                out[i++] = '%';
175            else if (in[pos] == '%') {
176                temp[0] = in[++pos];
177                temp[1] = in[++pos];
178                temp[2] = 0;
179                out[i++] = (char)strtoul (temp, NULL, 16);
180            }
181            else
182                out[i++] = in[pos];
183        }
184        out[i] = 0;
185        *r_out = out;
186    }

Legend:
Removed from v.25  
changed lines
  Added in v.260

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26