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

  1. /***
  2. *mbsrev.c - Reverse a string in place (MBCS)
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Reverse a string in place (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.  
  20.  
  21. /***
  22. * _mbsrev - Reverse a string in place (MBCS)
  23. *
  24. *Purpose:
  25. *       Reverses the order of characters in the string.  The terminating
  26. *       null character remains in place.  The order of MBCS characters
  27. *       is not changed.
  28. *
  29. *Entry:
  30. *       unsigned char *string = string to reverse
  31. *
  32. *Exit:
  33. *       returns string - now with reversed characters
  34. *
  35. *Exceptions:
  36. *
  37. *******************************************************************************/
  38.  
  39. unsigned char * __cdecl _mbsrev(
  40.         unsigned char *string
  41.         )
  42. {
  43.  
  44.         unsigned char *start = string;
  45.         unsigned char *left  = string;
  46.         unsigned char c;
  47.  
  48.         if ( _ISNOTMBCP )
  49.                 return _strrev(string);
  50.  
  51.         _mlock(_MB_CP_LOCK);
  52.  
  53.         /* first go through and reverse the bytes in MBCS chars */
  54.         while ( *string ) {
  55.  
  56.                 if ( _ISLEADBYTE(*string++) ) {
  57.                         if ( *string ) {
  58.                                 c = *string;
  59.                                 *string = *(string - 1);
  60.                                 *(string - 1) = c;
  61.                                 string++;
  62.                         }
  63.                         else /* second byte is EOS */
  64.                                 break;
  65.                 }
  66.         }
  67.  
  68.         /* now reverse the whole string */
  69.         string--;
  70.         while ( left < string ) {
  71.                 c = *left;
  72.                 *left++ = *string;
  73.                 *string-- = c;
  74.         }
  75.  
  76.         _munlock(_MB_CP_LOCK);
  77.         return ( start );
  78. }
  79.  
  80. #endif  /* _MBCS */
  81.