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

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