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

  1. /***
  2. *wsetlocal.c - Contains the setlocale function (wchar_t version)
  3. *
  4. *       Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Contains the _wsetlocale() function.
  8. *
  9. *******************************************************************************/
  10.  
  11.  
  12. #include <wchar.h>
  13. #include <stdlib.h>
  14. #include <setlocal.h>
  15. #include <locale.h>
  16. #include <dbgint.h>
  17.  
  18. wchar_t * __cdecl _wsetlocale (
  19.         int _category,
  20.         const wchar_t *_wlocale
  21.         )
  22. {
  23.         int size;
  24.         char *inlocale = NULL;
  25.         char *outlocale;
  26.         static wchar_t *outwlocale = NULL;
  27.  
  28.         /* convert WCS string into ASCII string */
  29.  
  30.         if (_wlocale)
  31.         {
  32.             size = wcslen(_wlocale) + 1;
  33.             if (NULL == (inlocale = (char *)_malloc_crt(size * sizeof(char))))
  34.                 return NULL;
  35.             if (-1 == wcstombs(inlocale, _wlocale, size))
  36.             {
  37.                 _free_crt (inlocale);
  38.                 return NULL;
  39.             }
  40.         }
  41.  
  42.         /* set the locale and get ASCII return string */
  43.  
  44.         outlocale = setlocale(_category, inlocale);
  45.         _free_crt (inlocale);
  46.         if (NULL == outlocale)
  47.             return NULL;
  48.  
  49.         /* get space for WCS return value */
  50.  
  51.         _free_crt(outwlocale);
  52.  
  53.         if (-1 == (size = mbstowcs(NULL, outlocale, 0)))
  54.             return NULL;
  55.  
  56.         size++;
  57.  
  58.         if (NULL == (outwlocale = (wchar_t *)_malloc_crt(size * sizeof(wchar_t))))
  59.             return NULL;
  60.  
  61.         /* convert return value to WCS */
  62.  
  63.         if (-1 == mbstowcs(outwlocale, outlocale, size))
  64.         {
  65.             _free_crt(outwlocale);
  66.             return NULL;
  67.         }
  68.  
  69.         return outwlocale;
  70. }
  71.  
  72.