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

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