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

  1. /***
  2. *w_loc.c - W version of GetLocaleInfo.
  3. *
  4. *       Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Use either GetLocaleInfoA or GetLocaleInfoW depending on which is
  8. *       available
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <internal.h>
  14. #include <stdlib.h>
  15. #include <setlocal.h>
  16. #include <awint.h>
  17. #include <dbgint.h>
  18.  
  19. #define USE_W   1
  20. #define USE_A   2
  21.  
  22. /***
  23. *int __cdecl __crtGetLocaleInfoW - Get locale info and return it as a wide
  24. *       string
  25. *
  26. *Purpose:
  27. *       Internal support function. Assumes info in wide string format. Tries
  28. *       to use NLS API call GetLocaleInfoW if available (NT) and uses
  29. *       GetLocaleInfoA if it must (Chicago). If neither are available it fails
  30. *       and returns 0.
  31. *
  32. *Entry:
  33. *       LCID     Locale      - locale context for the comparison.
  34. *       LCTYPE   LCType      - see NT\Chicago docs
  35. *       LPWSTR   lpLCData    - pointer to memory to return data
  36. *       int      cchData     - wide char (word) count of buffer (including
  37. *                              NULL) (if 0, lpLCData is not referenced, size
  38. *                              needed is returned)
  39. *       int      code_page   - for MB/WC conversion. If 0, use __lc_codepage
  40. *
  41. *Exit:
  42. *       Success: the number of characters copied (including NULL).
  43. *       Failure: 0
  44. *
  45. *Exceptions:
  46. *
  47. *******************************************************************************/
  48.  
  49. int __cdecl __crtGetLocaleInfoW(
  50.         LCID    Locale,
  51.         LCTYPE  LCType,
  52.         LPWSTR  lpLCData,
  53.         int     cchData,
  54.         int     code_page
  55.         )
  56. {
  57.         static int f_use = 0;
  58.  
  59.         /*
  60.          * Look for unstubbed 'preferred' flavor. Otherwise use available flavor.
  61.          * Must actually call the function to ensure it's not a stub.
  62.          */
  63.  
  64.         if (0 == f_use)
  65.         {
  66.             if (0 != GetLocaleInfoW(0, LOCALE_ILANGUAGE, NULL, 0))
  67.                 f_use = USE_W;
  68.  
  69.             else if (0 != GetLocaleInfoA(0, LOCALE_ILANGUAGE, NULL, 0))
  70.                 f_use = USE_A;
  71.  
  72.             else
  73.                 return 0;
  74.         }
  75.  
  76.         /* Use "W" version */
  77.  
  78.         if (USE_W == f_use)
  79.         {
  80.             return GetLocaleInfoW(Locale, LCType, lpLCData, cchData);
  81.         }
  82.  
  83.         /* Use "A" version */
  84.  
  85.         if (USE_A == f_use)
  86.         {
  87.             int retval;
  88.             int buff_size;
  89.             unsigned char *buffer;
  90.  
  91.             /*
  92.              * Use __lc_codepage for conversion if code_page not specified
  93.              */
  94.  
  95.             if (0 == code_page)
  96.                 code_page = __lc_codepage;
  97.  
  98.             /* find out how big buffer needs to be */
  99.             if (0 == (buff_size = GetLocaleInfoA(Locale, LCType, NULL, 0)))
  100.                 return 0;
  101.  
  102.             /* allocate buffer */
  103.             __try {
  104.                 buffer = (unsigned char *)_alloca( buff_size * sizeof(char) );
  105.             }
  106.             __except( EXCEPTION_EXECUTE_HANDLER ) {
  107.                 buffer = NULL;
  108.             }
  109.  
  110.             if ( buffer == NULL )
  111.                 return 0;
  112.  
  113.             /* get the info in ANSI format */
  114.             if (0 == GetLocaleInfoA(Locale, LCType, buffer, buff_size))
  115.                 return 0;
  116.  
  117.             if (0 == cchData)
  118.             {
  119.                 /* find out how much space needed */
  120.                 retval = MultiByteToWideChar( code_page,
  121.                                               MB_PRECOMPOSED,
  122.                                               buffer,
  123.                                               -1,
  124.                                               NULL,
  125.                                               0 );
  126.             }
  127.             else {
  128.                 /* convert into user buffer */
  129.                 retval = MultiByteToWideChar( code_page,
  130.                                               MB_PRECOMPOSED,
  131.                                               buffer,
  132.                                               -1,
  133.                                               lpLCData,
  134.                                               cchData );
  135.             }
  136.  
  137.             return retval;
  138.  
  139.         }
  140.         else   /* f_use is neither USE_A nor USE_W */
  141.             return 0;
  142. }
  143.