home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / mbscspn.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  118 lines

  1. /***
  2. *mbscspn.c - Find first string char in charset (MBCS)
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Find first string char in charset (MBCS)
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef _MBCS
  12.  
  13. #include <mtdll.h>
  14. #include <cruntime.h>
  15. #include <string.h>
  16. #include <mbdata.h>
  17. #include <mbctype.h>
  18. #include <mbstring.h>
  19. #include <stddef.h>
  20.  
  21.  
  22. /***
  23. *ifndef _RETURN_PTR
  24. * _mbscspn - Find first string char in charset (MBCS)
  25. *else
  26. * _mbspbrk - Find first string char in charset, pointer return (MBCS)
  27. *endif
  28. *
  29. *Purpose:
  30. *       Returns maximum leading segment of string
  31. *       which consists solely of characters NOT from charset.
  32. *       Handles MBCS chars correctly.
  33. *
  34. *Entry:
  35. *       char *string = string to search in
  36. *       char *charset = set of characters to scan over
  37. *
  38. *Exit:
  39. *
  40. *ifndef _RETURN_PTR
  41. *       Returns the index of the first char in string
  42. *       that is in the set of characters specified by control.
  43. *
  44. *       Returns 0, if string begins with a character in charset.
  45. *else
  46. *       Returns pointer to first character in charset.
  47. *
  48. *       Returns NULL if string consists entirely of characters
  49. *       not from charset.
  50. *endif
  51. *
  52. *Exceptions:
  53. *
  54. *******************************************************************************/
  55.  
  56. #ifndef _RETURN_PTR
  57.  
  58. size_t __cdecl _mbscspn( string, charset )
  59. const unsigned char *string;
  60. const unsigned char *charset;
  61.  
  62. #else  /* _RETURN_PTR */
  63.  
  64. unsigned char * __cdecl _mbspbrk( string, charset )
  65. const unsigned char *string;
  66. const unsigned char  *charset;
  67.  
  68. #endif  /* _RETURN_PTR */
  69.  
  70. {
  71.         unsigned char *p, *q;
  72.  
  73.         if ( _ISNOTMBCP )
  74. #ifndef _RETURN_PTR
  75.             return strcspn(string, charset);
  76. #else  /* _RETURN_PTR */
  77.             return strpbrk(string, charset);
  78. #endif  /* _RETURN_PTR */
  79.  
  80.         _mlock(_MB_CP_LOCK);
  81.  
  82.         /* loop through the string to be inspected */
  83.         for (q = (char *)string; *q ; q++) {
  84.  
  85.                 /* loop through the charset */
  86.                 for (p = (char *)charset; *p ; p++) {
  87.  
  88.                         if (_ISLEADBYTE(*p)) {
  89.                                 if (((*p == *q) && (p[1] == q[1])) || p[1] == '\0')
  90.                                         break;
  91.                                 p++;
  92.                         }
  93.  
  94.                         else
  95.                                 if (*p == *q)
  96.                                         break;
  97.                 }
  98.  
  99.                 if (*p != '\0')         /* end of charset? */
  100.                         break;          /* no, match on this char */
  101.  
  102.                 if (_ISLEADBYTE(*q))
  103.                         if (*++q == '\0')
  104.                                 break;
  105.         }
  106.  
  107.         _munlock(_MB_CP_LOCK);
  108.  
  109. #ifndef _RETURN_PTR
  110.         return((size_t) (q - string));          /* index */
  111. #else  /* _RETURN_PTR */
  112.         return((*q) ? q : NULL);        /* pointer */
  113. #endif  /* _RETURN_PTR */
  114.  
  115. }
  116.  
  117. #endif  /* _MBCS */
  118.