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

  1. /***
  2. *wcsupr.c - routine to map lower-case characters in a wchar_t string
  3. *       to upper-case
  4. *
  5. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  6. *
  7. *Purpose:
  8. *       Converts all the lower case characters in a wchar_t string
  9. *       to upper case, in place.
  10. *
  11. *******************************************************************************/
  12.  
  13.  
  14. #include <cruntime.h>
  15. #include <string.h>
  16. #include <malloc.h>
  17. #include <locale.h>
  18. #include <ctype.h>
  19. #include <setlocal.h>
  20. #include <mtdll.h>
  21. #include <awint.h>
  22. #include <dbgint.h>
  23.  
  24. /***
  25. *wchar_t *_wcsupr(string) - map lower-case characters in a string to upper-case
  26. *
  27. *Purpose:
  28. *       wcsupr converts lower-case characters in a null-terminated wchar_t
  29. *       string to their upper-case equivalents.  The result may be longer or
  30. *       shorter than the original string.  Assumes enough space in string
  31. *       to hold the result.
  32. *
  33. *Entry:
  34. *       wchar_t *wsrc - wchar_t string to change to upper case
  35. *
  36. *Exit:
  37. *       input string address
  38. *
  39. *Exceptions:
  40. *       on an error, the original string is unaltered
  41. *
  42. *******************************************************************************/
  43.  
  44. wchar_t * __cdecl _wcsupr (
  45.         wchar_t * wsrc
  46.         )
  47. {
  48.  
  49.         wchar_t *p;             /* traverses string for C locale conversion */
  50.         wchar_t *wdst = NULL;   /* wide version of string in alternate case */
  51.         int dstlen;             /* len of wdst string, wide chars, with null  */
  52. #if defined (_MT)
  53.         int local_lock_flag;
  54. #endif  /* defined (_MT) */
  55.  
  56.         if (__lc_handle[LC_CTYPE] == _CLOCALEHANDLE) {
  57.                 for (p=wsrc; *p; p++)
  58.                 {
  59.                         if (*p >= (wchar_t)L'a' && *p <= (wchar_t)L'z')
  60.                                 *p = *p - (L'a' - L'A');
  61.                 }
  62.                 return (wsrc);
  63.         } /* C locale */
  64.  
  65.         _lock_locale( local_lock_flag )
  66.  
  67. #if defined (_MT)
  68.         if (__lc_handle[LC_CTYPE] == _CLOCALEHANDLE) {
  69.                 _unlock_locale( local_lock_flag )
  70.                 for (p=wsrc; *p; p++)
  71.                 {
  72.                         if (*p >= (wchar_t)L'a' && *p <= (wchar_t)L'z')
  73.                                 *p = *p - (L'a' - L'A');
  74.                 }
  75.                 return (wsrc);
  76.         } /* C locale */
  77. #endif  /* defined (_MT) */
  78.  
  79.         /* Inquire size of wdst string */
  80.         if (0 == (dstlen=__crtLCMapStringW(__lc_handle[LC_CTYPE], LCMAP_UPPERCASE,
  81.             wsrc, -1, wdst, 0, 0)))
  82.                 goto error_cleanup;
  83.  
  84.         /* Allocate space for wdst */
  85.         if ((wdst = (wchar_t *) _malloc_crt(dstlen*sizeof(wchar_t))) == NULL)
  86.                 goto error_cleanup;
  87.  
  88.         /* Map wrc string to wide-character wdst string in alternate case */
  89.         if (__crtLCMapStringW(__lc_handle[LC_CTYPE], LCMAP_UPPERCASE,
  90.             wsrc, -1, wdst, dstlen, 0) == 0)
  91.                 goto error_cleanup;
  92.  
  93.         /* Copy wdst string to user string */
  94.         wcscpy (wsrc, wdst);
  95.  
  96. error_cleanup:
  97.         _unlock_locale( local_lock_flag )
  98.         _free_crt (wdst);
  99.  
  100.         return (wsrc);
  101. }
  102.  
  103.