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

  1. /***
  2. *strupr.c - routine to map lower-case characters in a string to upper-case
  3. *
  4. *   Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *   Converts all the lower case characters in string to upper case
  8. *   in place.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <string.h>
  14.  
  15. #ifdef _WIN32
  16. #include <malloc.h>
  17. #include <locale.h>
  18. #include <setlocal.h>
  19. #include <limits.h> /* for INT_MAX */
  20. #include <mtdll.h>
  21. #include <awint.h>
  22. #include <dbgint.h>
  23. #endif  /* _WIN32 */
  24.  
  25. /***
  26. *char *_strupr(string) - map lower-case characters in a string to upper-case
  27. *
  28. *Purpose:
  29. *   _strupr() converts lower-case characters in a null-terminated string
  30. *   to their upper-case equivalents.  Conversion is done in place and
  31. *   characters other than lower-case letters are not modified.
  32. *
  33. *   In the C locale, this function modifies only 7-bit ASCII characters
  34. *   in the range 0x61 through 0x7A ('a' through 'z').
  35. *
  36. *   If the locale is not the 'C' locale, MapStringW() is used to do
  37. *   the work.  Assumes enough space in the string to hold result.
  38. *
  39. *Entry:
  40. *   char *string - string to change to upper case
  41. *
  42. *Exit:
  43. *   input string address
  44. *
  45. *Exceptions:
  46. *   The original string is returned unchanged on any error.
  47. *
  48. *******************************************************************************/
  49.  
  50. char * __cdecl _strupr (
  51.     char * string
  52.     )
  53. {
  54. #if defined (_WIN32)
  55.  
  56.     int dstlen;                 /* len of dst string, with null  */
  57.     unsigned char *dst = NULL;  /* wide version of string in alternate case */
  58. #if defined (_MT)
  59.     int local_lock_flag;
  60. #endif  /* defined (_MT) */
  61.  
  62.     if (__lc_handle[LC_CTYPE] == _CLOCALEHANDLE)
  63.     {
  64.         char *cp;       /* traverses string for C locale conversion */
  65.         for (cp = string; *cp; ++cp)
  66.         {
  67.             if ('a' <= *cp && *cp <= 'z')
  68.                 *cp += 'A' - 'a';
  69.         }
  70.         return(string);
  71.     } /* C locale */
  72.  
  73.     _lock_locale( local_lock_flag )
  74.  
  75. #if defined (_MT)
  76.     if (__lc_handle[LC_CTYPE] == _CLOCALEHANDLE)
  77.     {
  78.         char *cp;       /* traverses string for C locale conversion */
  79.         _unlock_locale( local_lock_flag )
  80.  
  81.         for (cp=string; *cp; ++cp)
  82.         {
  83.             if ('a' <= *cp && *cp <= 'z')
  84.                 *cp += 'A' - 'a';
  85.         }
  86.  
  87.         return(string);
  88.     } /* C locale */
  89. #endif  /* defined (_MT) */
  90.  
  91.     if (0 == (dstlen =__crtLCMapStringA(__lc_handle[LC_CTYPE],
  92.                                        LCMAP_UPPERCASE, string, -1,
  93.                                        NULL, 0, 0, TRUE)))
  94.         goto error_cleanup;
  95.  
  96.     /* Allocate space for dst */
  97.     if (NULL == (dst = (unsigned char *)
  98.                              _malloc_crt(dstlen * sizeof(unsigned char))))
  99.         goto error_cleanup;
  100.  
  101.     /* Map src string to dst string in alternate case */
  102.     if (0 == __crtLCMapStringA(__lc_handle[LC_CTYPE], LCMAP_UPPERCASE,
  103.             string, -1, dst, dstlen, 0, TRUE))
  104.         goto error_cleanup;
  105.  
  106.     /* copy dst string to return string */
  107.     strcpy(string, dst);
  108.  
  109. error_cleanup:
  110.     _unlock_locale( local_lock_flag )
  111.     _free_crt (dst);
  112.     return (string);
  113.  
  114. #else  /* defined (_WIN32) */
  115.  
  116.     char * cp;
  117.  
  118.     for (cp=string; *cp; ++cp)
  119.     {
  120.         if ('a' <= *cp && *cp <= 'z')
  121.             *cp += 'A' - 'a';
  122.     }
  123.  
  124.     return(string);
  125.  
  126. #endif  /* defined (_WIN32) */
  127. }
  128.