/[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 32 by twoaday, Mon Oct 24 08:03:48 2005 UTC revision 36 by werner, Thu Oct 27 15:25:13 2005 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>  #ifdef HAVE_CONFIG_H
21  #include <stdio.h>  #include <config.h>
22  #include <ctype.h>  #endif
23    
24  /* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */  #include <string.h>
25  char*  #include <string.h>
26  strsep (char **stringp, const char *delim)  #include <stdio.h>
27  {  #include <ctype.h>
28      char *begin, *end;  
29      begin = *stringp;  /* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */
30    char*
31      if (begin == NULL)  strsep (char **stringp, const char *delim)
32          return NULL;  {
33        char *begin, *end;
34      /* A frequent case is when the delimiter string contains only one      begin = *stringp;
35         character.  Here we don't need to call the expensive `strpbrk'  
36         function and instead work using `strchr'.  */      if (begin == NULL)
37      if (delim[0] == '\0' || delim[1] == '\0') {          return NULL;
38          char ch = delim[0];  
39          if (ch == '\0')      /* A frequent case is when the delimiter string contains only one
40              end = NULL;         character.  Here we don't need to call the expensive `strpbrk'
41          else {         function and instead work using `strchr'.  */
42              if (*begin == ch)      if (delim[0] == '\0' || delim[1] == '\0') {
43                  end = begin;                                  char ch = delim[0];
44              else if (*begin == '\0')          if (ch == '\0')
45                  end = NULL;              end = NULL;
46              else          else {
47                  end = strchr (begin + 1, ch);              if (*begin == ch)
48          }                    end = begin;                        
49      }              else if (*begin == '\0')
50      else /* Find the end of the token.  */                  end = NULL;
51          end = strpbrk (begin, delim);              else
52      if ( end ){                  end = strchr (begin + 1, ch);
53          /* Terminate the token and set *STRINGP past NUL character.  */          }  
54          *end++ = '\0';      }
55          *stringp = end;      else /* Find the end of the token.  */
56      }          end = strpbrk (begin, delim);
57      else      if ( end ){
58          /* No more delimiters; this is the last token.  */          /* Terminate the token and set *STRINGP past NUL character.  */
59          *stringp = NULL;          *end++ = '\0';
60            *stringp = end;
61      return begin;      }
62  }      else
63            /* No more delimiters; this is the last token.  */
64            *stringp = NULL;
65  /* Like strstr but this version is case in-sentensive. */  
66  const char *      return begin;
67  stristr (const char *buf, const char *sub)  }
68  {  
69      const char *t, *s ;  
70      size_t n;  /* Like strstr but this version is case in-sentensive. */
71      size_t buflen = strlen (buf);  const char *
72    stristr (const char *buf, const char *sub)
73      for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) {  {
74          if( toupper(*t) == toupper(*s) ) {      const char *t, *s ;
75              for( buf=t++, buflen = n--, s++;      size_t n;
76                   n && toupper(*t) == toupper(*s); t++, s++, n-- )      size_t buflen = strlen (buf);
77                  ;  
78              if( !*s )      for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) {
79                  return buf;          if( toupper(*t) == toupper(*s) ) {
80              t = buf; n = buflen; s = sub ;              for( buf=t++, buflen = n--, s++;
81          }                   n && toupper(*t) == toupper(*s); t++, s++, n-- )
82      }                  ;
83      return NULL ;              if( !*s )
84  }                  return buf;
85                t = buf; n = buflen; s = sub ;
86            }
87        }
88        return NULL ;
89    }
90    

Legend:
Removed from v.32  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26