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

  1. /***
  2. *mbsdec.c - Move MBCS string pointer backward one charcter.
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Move MBCS string pointer backward one character.
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef _MBCS
  12.  
  13. #include <mtdll.h>
  14. #include <cruntime.h>
  15. #include <mbdata.h>
  16. #include <mbstring.h>
  17. #include <mbctype.h>
  18. #include <stddef.h>
  19.  
  20.  
  21. /***
  22. *_mbsdec - Move MBCS string pointer backward one charcter.
  23. *
  24. *Purpose:
  25. *       Move the supplied string pointer backwards by one
  26. *       character.  MBCS characters are handled correctly.
  27. *
  28. *Entry:
  29. *       const unsigned char *string = pointer to beginning of string
  30. *       const unsigned char *current = current char pointer (legal MBCS boundary)
  31. *
  32. *Exit:
  33. *       Returns pointer after moving it.
  34. *       Returns NULL if string >= current.
  35. *
  36. *Exceptions:
  37. *
  38. *******************************************************************************/
  39.  
  40. unsigned char * __cdecl _mbsdec(
  41.         const unsigned char *string,
  42.         const unsigned char *current
  43.         )
  44. {
  45.         const unsigned char *temp;
  46.  
  47.         if (string >= current)
  48.                 return(NULL);
  49.  
  50.         if ( _ISNOTMBCP )
  51.             return (unsigned char *)--current;
  52.  
  53.         _mlock(_MB_CP_LOCK);
  54.  
  55.         temp = current - 1;
  56.  
  57. /*
  58.  *  If (current-1) returns true from _ISLEADBTYE, it is a trail byte, because
  59.  *  it is not a legal single byte MBCS character.  Therefore, is so, return
  60.  *  (current-2) because it is the trailbyte's lead.
  61.  */
  62.  
  63.         if (_ISLEADBYTE(*temp))
  64.         {
  65.             _munlock(_MB_CP_LOCK);
  66.             return (unsigned char *)(temp - 1);
  67.         }
  68.  
  69. /*
  70.  *  It is unknown whether (current - 1) is a single byte character or a
  71.  *  trail.  Now decrement temp until
  72.  *      a)  The beginning of the string is reached, or
  73.  *      b)  A non-lead byte (either single or trail) is found.
  74.  *  The difference between (current-1) and temp is the number of non-single
  75.  *  byte characters preceding (current-1).  There are two cases for this:
  76.  *      a)  (current - temp) is odd, and
  77.  *      b)  (current - temp) is even.
  78.  *  If odd, then there are an odd number of "lead bytes" preceding the
  79.  *  single/trail byte (current - 1), indicating that it is a trail byte.
  80.  *  If even, then there are an even number of "lead bytes" preceding the
  81.  *  single/trail byte (current - 1), indicating a single byte character.
  82.  */
  83.  
  84.         while ((string <= --temp) && (_ISLEADBYTE(*temp)))
  85.                 ;
  86.  
  87.         _munlock(_MB_CP_LOCK);
  88.         return (unsigned char *)(current - 1 - ((current - temp) & 0x01) );
  89.  
  90. }
  91.  
  92. #endif  /* _MBCS */
  93.