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

  1. /***
  2. *towlower.c - convert wide character to lower case
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Defines towlower().
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <ctype.h>
  13. #include <stdio.h>
  14. #include <locale.h>
  15. #include <setlocal.h>
  16. #include <mtdll.h>
  17. #include <awint.h>
  18.  
  19. /***
  20. *wchar_t towlower(c) - convert wide character to lower case
  21. *
  22. *Purpose:
  23. *       towlower() returns the lowercase equivalent of its argument
  24. *
  25. *Entry:
  26. *       c - wchar_t value of character to be converted
  27. *
  28. *Exit:
  29. *       if c is an upper case letter, returns wchar_t value of lower case
  30. *       representation of c. otherwise, it returns c.
  31. *
  32. *Exceptions:
  33. *
  34. *******************************************************************************/
  35.  
  36. wchar_t __cdecl towlower (
  37.         wchar_t c
  38.         )
  39. {
  40.  
  41. #ifdef _MT
  42.  
  43.         int local_lock_flag;
  44.  
  45.         if ( c == WEOF )
  46.             return c;
  47.  
  48.         if ( __lc_handle[LC_CTYPE] == _CLOCALEHANDLE ) {
  49.             if ( (c >= L'A') && (c <= L'Z') )
  50.                 c = c - L'A' + L'a';
  51.             return c;
  52.         }
  53.  
  54.         _lock_locale( local_lock_flag )
  55.  
  56.         c = _towlower_lk(c);
  57.  
  58.         _unlock_locale( local_lock_flag )
  59.  
  60.         return c;
  61. }
  62.  
  63. /***
  64. *wchar_t _towlower_lk(c) - convert wide character to lower case
  65. *
  66. *Purpose:
  67. *       Multi-thread function only! Non-locking version of towlower.
  68. *
  69. *Entry:
  70. *
  71. *Exit:
  72. *
  73. *Exceptions:
  74. *
  75. *******************************************************************************/
  76.  
  77. wchar_t __cdecl _towlower_lk (
  78.         wchar_t c
  79.         )
  80. {
  81.  
  82. #endif  /* _MT */
  83.  
  84.         wchar_t widechar;
  85.  
  86.         if (c == WEOF)
  87.             return c;
  88.  
  89.  
  90.         if (__lc_handle[LC_CTYPE] == _CLOCALEHANDLE) {
  91.             if ( (c >= L'A') && (c <= L'Z') )
  92.                 c = c - L'A' + L'a';
  93.             return c;
  94.         }
  95.  
  96.         /* if checking case of c does not require API call, do it */
  97.         if (c < 256) {
  98.             if (!iswupper(c)) {
  99.                 return c;
  100.             }
  101.         }
  102.  
  103.         /* convert wide char to lowercase */
  104.         if ( 0 == __crtLCMapStringW( __lc_handle[LC_CTYPE],
  105.                                      LCMAP_LOWERCASE,
  106.                                      (LPCWSTR)&c,
  107.                                      1,
  108.                                      (LPWSTR)&widechar,
  109.                                      1,
  110.                                      0) )
  111.         {
  112.             return c;
  113.         }
  114.  
  115.         return widechar;
  116.  
  117. }
  118.