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

  1. /***
  2. *mbsrchr.c - Search for last occurence of character (MBCS)
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Search for last occurence of character (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. * _mbsrchr - Search for last occurence of character (MBCS)
  24. *
  25. *Purpose:
  26. *       Find the last occurrence of the specified character in
  27. *       the supplied string.  Handles MBCS chars/strings correctly.
  28. *
  29. *Entry:
  30. *       unsigned char *str = string to search in
  31. *       unsigned int c = character to search for
  32. *
  33. *Exit:
  34. *       returns pointer to last occurrence of c in str
  35. *       returns NULL if c not found
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40.  
  41. unsigned char * __cdecl _mbsrchr(
  42.         const unsigned char *str,
  43.         unsigned int c
  44.         )
  45. {
  46.         char *r = NULL;
  47.         unsigned int cc;
  48.  
  49.         if ( _ISNOTMBCP )
  50.             return strrchr(str, c);
  51.  
  52.         _mlock(_MB_CP_LOCK);
  53.  
  54.         do {
  55.             cc = *str;
  56.             if (_ISLEADBYTE(cc)) {
  57.                 if(*++str) {
  58.                     if (c == ((cc<<8)|*str))
  59.                         r = (char *)str - 1;
  60.                 }
  61.                 else if(!r)
  62.                     /* return pointer to '\0' */
  63.                     r = (char *)str;
  64.             }
  65.             else if (c == cc)
  66.                 r = (char *)str;
  67.         }
  68.         while (*str++);
  69.  
  70.         _munlock(_MB_CP_LOCK);
  71.         return(r);
  72. }
  73.  
  74. #endif  /* _MBCS */
  75.