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

  1. /***
  2. *xwcsxfrm.c - Transform a wide-character string using locale information
  3. *
  4. *       Copyright (c) 1996-1998, Microsoft Corporation. All rights reserved.
  5. *               
  6. *
  7. *Purpose:
  8. *       Transform a wide-character string using the locale information as set by
  9. *       LC_COLLATE.
  10. *
  11. *Revision History:
  12. *       01-XX-96  PJP   Created from wcsxfrm.c January 1996 by P.J. Plauger
  13. *       04-18-96  GJF   Updated for current locale locking. Also, reformatted
  14. *                       and made several cosmetic changes.
  15. *       12-02-97  GJF   Removed bogus codepage determination.
  16. *       01-12-98  GJF   Use _lc_collate_cp codepage.
  17. *
  18. *******************************************************************************/
  19.  
  20.  
  21. #include <cruntime.h>
  22. #include <windows.h>
  23. #include <string.h>
  24. #include <limits.h>
  25. #include <locale.h>
  26. #include <setlocal.h>
  27. #include <stdlib.h>
  28. #include <mtdll.h>
  29. #include <awint.h>
  30. #include <dbgint.h>
  31. #include <xlocinfo.h>   /* for _Collvec, _Wcsxfrm */
  32.  
  33. /***
  34. *size_t _Wcsxfrm() - Transform a string using locale information
  35. *
  36. *Purpose:
  37. *       Transform the wide string pointed to by _string2 and place the
  38. *       resulting wide string into the array pointed to by _string1.
  39. *       No more than _end1 - _string1 wide characters are placed into the
  40. *       resulting string (including the null).
  41. *
  42. *       The transformation is such that if wcscmp() is applied to
  43. *       the two transformed strings, the return value is equal to
  44. *       the result of wcscoll() applied to the two original strings.
  45. *       Thus, the conversion must take the locale LC_COLLATE info
  46. *       into account.
  47. *
  48. *       In the C locale, wcsxfrm() simply resolves to wcsncpy()/wcslen().
  49. *
  50. *Entry:
  51. *       wchar_t *_string1       = pointer to beginning of result string
  52. *       wchar_t *_end1          = pointer past end of result string
  53. *       const wchar_t *_string2 = pointer to beginning of source string
  54. *       const wchar_t *_end2    = pointer past end of source string
  55. *       const _Collvec *ploc = pointer to locale info
  56. *
  57. *Exit:
  58. *       Length of the transformed string.
  59. *       If the value returned is too big, the contents of the
  60. *       _string1 array are indeterminate.
  61. *
  62. *Exceptions:
  63. *       Non-standard: if OM/API error, return INT_MAX.
  64. *
  65. *******************************************************************************/
  66.  
  67. size_t __cdecl _Wcsxfrm (
  68.         wchar_t *_string1,
  69.         wchar_t *_end1,
  70.         const wchar_t *_string2,
  71.         const wchar_t *_end2,
  72.         const _Collvec *ploc
  73.         )
  74. {
  75.         size_t _n1 = _end1 - _string1;
  76.         size_t _n2 = _end2 - _string2;
  77.         int size = INT_MAX;
  78.         unsigned char *bbuffer;
  79.         LCID handle;
  80. #ifdef _MT
  81.         int local_lock_flag;
  82.  
  83.         _lock_locale( local_lock_flag )
  84. #endif
  85.  
  86.         if (ploc == 0)
  87.             handle = __lc_handle[LC_COLLATE];
  88.         else
  89.             handle = ploc->_Hand;
  90.  
  91.         if (handle == _CLOCALEHANDLE) {
  92.             _unlock_locale( local_lock_flag )
  93.             if (_n2 <= _n1)
  94.                 memcpy(_string1, _string2, _n2 * sizeof (wchar_t));
  95.                 return _n2;
  96.         }
  97.  
  98.         /*
  99.          * When using LCMAP_SORTKEY, LCMapStringW handles BYTES not wide
  100.          * chars. We use a byte buffer to hold bytes and then convert the
  101.          * byte string to a wide char string and return this so it can be
  102.          * compared using wcscmp(). User's buffer is _n1 wide chars, so
  103.          * use an internal buffer of _n1 bytes.
  104.          */
  105.  
  106.         if (NULL == (bbuffer = (unsigned char *)_malloc_crt(_n1)))
  107.             goto error_cleanup;
  108.  
  109.         if (0 == (size = __crtLCMapStringW(handle, 
  110.                                            LCMAP_SORTKEY,
  111.                                            _string2,
  112.                                            _n2,
  113.                                            (wchar_t *)bbuffer,
  114.                                            _n1,
  115.                                            __lc_collate_cp)))
  116.         {
  117.             /* buffer not big enough, get size required. */
  118.  
  119.             if (0 == (size = __crtLCMapStringW(handle,
  120.                                                LCMAP_SORTKEY,
  121.                                                _string2,
  122.                                                _n2,
  123.                                                NULL,
  124.                                                0,
  125.                                                __lc_collate_cp)))
  126.                 size = INT_MAX; /* default error */
  127.  
  128.         } else {
  129.             int i;
  130.             /* string successfully mapped, convert to wide char */
  131.  
  132.             for (i = 0; i < size; i++)
  133.                 _string1[i] = (wchar_t)bbuffer[i];
  134.         }
  135.  
  136. error_cleanup:
  137.         _unlock_locale( local_lock_flag )
  138.         _free_crt(bbuffer);
  139.  
  140.         return (size_t)size;
  141. }
  142.