Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
  28. *               DESCRIBE IT HERE!
  29. *
  30. ****************************************************************************/
  31.  
  32.  
  33. #include "variety.h"
  34. #include "widechar.h"
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include "rtdata.h"
  38. #ifdef __WIDECHAR__
  39.     #include "nextwtok.h"
  40. #else
  41.     #include "nexttok.h"
  42. #endif
  43. #include "setbits.h"
  44.  
  45.  
  46. #ifdef __WIDECHAR__
  47. _WCRTLINK wchar_t *_ustrtok( wchar_t *str, const wchar_t *charset )
  48. {
  49.     return( wcstok( str, charset, NULL ) );
  50. }
  51. #endif
  52.  
  53.  
  54. #ifdef __WIDECHAR__
  55.  _WCRTLINK wchar_t *wcstok( wchar_t *str, const wchar_t *charset, wchar_t **ptr )
  56. #else
  57.  _WCRTLINK char *strtok( char *str, const char *charset )
  58. #endif
  59. {
  60. #if defined(__WIDECHAR__)
  61.     CHAR_TYPE           *p1;
  62.     const CHAR_TYPE             *p2;
  63.     CHAR_TYPE           tc1;
  64.     CHAR_TYPE           tc2;
  65. #else
  66.     unsigned /*char*/ tc;
  67.     unsigned char vector[32];
  68.     unsigned char *p1;
  69. #endif
  70.  
  71. #if defined(__WIDECHAR__)
  72.     /* if necessary, continue from where we left off */
  73.     if( str == NULL ) {
  74.         if( ptr == NULL ) {
  75.             str = _RWD_nextwtok;
  76.         } else {
  77.             str = *ptr;                 /* use previous value */
  78.         }
  79.         if( str == NULL )  return( NULL );
  80.     }
  81.  
  82.     /* skip characters until we reach one not in charset */
  83.     for( ; tc1 = *str; str++ ) {
  84.         for( p2 = charset; tc2 = *p2; p2++ ) {
  85.             if( tc1 == tc2 ) break;
  86.         }
  87.         if( tc2 == NULLCHAR ) break;
  88.     }
  89.     if( tc1 == NULLCHAR ) return( NULL );
  90.     /* skip characters until we reach one in charset */
  91.     for( p1 = str; tc1 = *p1; p1++ ) {
  92.         for( p2 = charset; tc2 = *p2; p2++ ) {
  93.             if( tc1 == tc2 ) break;
  94.         }
  95.         if( tc2 != NULLCHAR ){
  96.             *p1 = NULLCHAR;         /* terminate the token  */
  97.             p1++;                   /* start of next token  */
  98.             if( ptr == NULL ) {
  99.                 _RWD_nextwtok = p1;
  100.             } else {
  101.                 *ptr = p1;
  102.             }
  103.             return( str );
  104.         }
  105.     }
  106.     if( ptr == NULL ) {
  107.         _RWD_nextwtok = NULL;
  108.     } else {
  109.         *ptr = NULL;
  110.     }
  111. #else
  112.     /* if necessary, continue from where we left off */
  113.     _INITNEXTTOK
  114.     if( str == NULL ) {
  115.         str = (CHAR_TYPE*)_RWD_nexttok; /* use previous value */
  116.         if( str == NULL ) return( NULL );
  117.     }
  118.  
  119.     __setbits( vector, charset );
  120.     for( ; tc = (unsigned char) *str; ++str ) {
  121.         /* quit if we find any char not in charset */
  122.         if( ( vector[ tc >> 3 ] & _Bits[ tc & 0x07 ] ) == 0 )  break;
  123.     }
  124.     if( tc == '\0' ) return( NULL );
  125.     p1 = str;
  126.     for( ; tc = *p1; ++p1 ) {
  127.         /* quit when we find any char in charset */
  128.         if( ( vector[ tc >> 3 ] & _Bits[ tc & 0x07 ] ) != 0 ) {
  129.             *p1 = '\0';             /* terminate the token  */
  130.             p1++;                   /* start of next token  */
  131.             _RWD_nexttok = p1;
  132.             return( str );
  133.         }
  134.     }
  135.     _RWD_nexttok = NULL;
  136. #endif
  137.  
  138.     return( str );
  139. }
  140.