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

  1. /***
  2. * mbschr.c - Search MBCS string for character
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Search MBCS string for a character
  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. * _mbschr - Search MBCS string for character
  24. *
  25. *Purpose:
  26. *       Search the given string for the specified character.
  27. *       MBCS characters are handled correctly.
  28. *
  29. *Entry:
  30. *       unsigned char *string = string to search
  31. *       int c = character to search for
  32. *
  33. *Exit:
  34. *       returns a pointer to the first occurence of the specified char
  35. *       within the string.
  36. *
  37. *       returns NULL if the character is not found n the string.
  38. *
  39. *Exceptions:
  40. *
  41. *******************************************************************************/
  42.  
  43.  
  44. unsigned char * __cdecl _mbschr(
  45.         const unsigned char *string,
  46.         unsigned int c
  47.         )
  48. {
  49.         unsigned short cc;
  50.  
  51.         if ( _ISNOTMBCP )
  52.             return strchr(string, c);
  53.  
  54.         _mlock(_MB_CP_LOCK);
  55.  
  56.         for (; (cc = *string); string++)
  57.         {
  58.             if (_ISLEADBYTE(cc))
  59.             {
  60.                 if (*++string == '\0')
  61.                 {
  62.                     _munlock(_MB_CP_LOCK);
  63.                     return NULL;        /* error */
  64.                 }
  65.                 if ( c == (unsigned int)((cc << 8) | *string) ) /* DBCS match */
  66.                 {
  67.                     _munlock(_MB_CP_LOCK);
  68.                     return (unsigned char *)(string - 1);
  69.                 }
  70.             }
  71.             else if (c == (unsigned int)cc)
  72.                 break;  /* SBCS match */
  73.         }
  74.  
  75.         _munlock(_MB_CP_LOCK);
  76.  
  77.         if (c == (unsigned int)cc)      /* check for SBCS match--handles NUL char */
  78.             return (unsigned char *)(string);
  79.  
  80.         return NULL;
  81. }
  82.  
  83. #endif  /* _MBCS */
  84.