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

  1. /***
  2. *tolower.c - convert character to lower case
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Defines function versions of _tolower() and tolower().
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <ctype.h>
  13. #include <stddef.h>
  14.  
  15. #ifndef _MAC
  16. #include <locale.h>
  17. #include <setlocal.h>
  18. #include <mtdll.h>
  19. #include <awint.h>
  20. #endif  /* _MAC */
  21.  
  22. /* remove macro defintions of _tolower() and tolower()
  23.  */
  24. #undef  _tolower
  25. #undef  tolower
  26.  
  27. /* define function-like macro equivalent to _tolower()
  28.  */
  29. #define mklower(c)  ( (c)-'A'+'a' )
  30.  
  31. /***
  32. *int _tolower(c) - convert character to lower case
  33. *
  34. *Purpose:
  35. *       _tolower() is simply a function version of the macro of the same name.
  36. *
  37. *Entry:
  38. *       c - int value of character to be converted
  39. *
  40. *Exit:
  41. *       returns int value of lower case representation of c
  42. *
  43. *Exceptions:
  44. *
  45. *******************************************************************************/
  46.  
  47. int __cdecl _tolower (
  48.         int c
  49.         )
  50. {
  51.         return(mklower(c));
  52. }
  53.  
  54. /***
  55. *int tolower(c) - convert character to lower case
  56. *
  57. *Purpose:
  58. *       tolower() is simply a function version of the macro of the same name.
  59. *
  60. *Entry:
  61. *       c - int value of character to be converted
  62. *
  63. *Exit:
  64. *       if c is an upper case letter, returns int value of lower case
  65. *       representation of c. otherwise, it returns c.
  66. *
  67. *Exceptions:
  68. *
  69. *******************************************************************************/
  70.  
  71.  
  72. int __cdecl tolower (
  73.         int c
  74.         )
  75. {
  76. #if !defined (_MAC)
  77.  
  78. #ifdef _MT
  79.         int local_lock_flag;
  80.  
  81.         if (__lc_handle[LC_CTYPE] == _CLOCALEHANDLE)
  82.         {
  83.             if ( (c >= 'A') && (c <= 'Z') )
  84.                 c = c + ('a' - 'A');
  85.             return c;
  86.         }
  87.  
  88.         _lock_locale( local_lock_flag )
  89.  
  90.         c = _tolower_lk(c);
  91.  
  92.         _unlock_locale( local_lock_flag )
  93.  
  94.         return c;
  95.  
  96. }
  97.  
  98. /***
  99. *int _tolower_lk(c) - convert character to lower case
  100. *
  101. *Purpose:
  102. *       Multi-thread function only! Non-locking version of tolower.
  103. *
  104. *Entry:
  105. *
  106. *Exit:
  107. *
  108. *Exceptions:
  109. *
  110. *******************************************************************************/
  111.  
  112. int __cdecl _tolower_lk (
  113.         int c
  114.         )
  115. {
  116.  
  117. #endif  /* _MT */
  118.  
  119.         int size;
  120.         unsigned char inbuffer[3];
  121.         unsigned char outbuffer[3];
  122.  
  123.         if (__lc_handle[LC_CTYPE] == _CLOCALEHANDLE)
  124.         {
  125.             if ( (c >= 'A') && (c <= 'Z') )
  126.                 c = c + ('a' - 'A');
  127.             return c;
  128.         }
  129.  
  130.         /* if checking case of c does not require API call, do it */
  131.         if (c < 256)
  132.         {
  133.             if (!isupper(c))
  134.             {
  135.                 return c;
  136.             }
  137.         }
  138.  
  139.         /* convert int c to multibyte string */
  140.         if (isleadbyte(c >> 8 & 0xff))
  141.         {
  142.             inbuffer[0] = (c >> 8 & 0xff); /* put lead-byte at start of str */
  143.             inbuffer[1] = (unsigned char)c;
  144.             inbuffer[2] = 0;
  145.             size = 2;
  146.         } else {
  147.             inbuffer[0] = (unsigned char)c;
  148.             inbuffer[1] = 0;
  149.             size = 1;
  150.         }
  151.  
  152.         /* convert to lowercase */
  153.         if ( 0 == (size = __crtLCMapStringA( __lc_handle[LC_CTYPE],
  154.                                              LCMAP_LOWERCASE,
  155.                                              inbuffer,
  156.                                              size,
  157.                                              outbuffer,
  158.                                              3,
  159.                                              0,
  160.                                              TRUE)) )
  161.         {
  162.             return c;
  163.         }
  164.  
  165.         /* construct integer return value */
  166.         if (size == 1)
  167.             return ((int)outbuffer[0]);
  168.         else
  169.             return ((int)outbuffer[0] | ((int)outbuffer[1] << 8));
  170.  
  171. #else  /* !defined (_MAC) */
  172.  
  173.         return(isupper(c) ? mklower(c) : c);
  174.  
  175. #endif  /* !defined (_MAC) */
  176. }
  177.