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

  1. /***
  2. *wcslwr.c - routine to map upper-case characters in a wchar_t string
  3. *       to lower-case
  4. *
  5. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  6. *
  7. *Purpose:
  8. *       Converts all the upper case characters in a wchar_t string
  9. *       to lower 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 *_wcslwr(string) - map upper-case characters in a string to lower-case
  26. *
  27. *Purpose:
  28. *       wcslwr converts upper-case characters in a null-terminated wchar_t
  29. *       string to their lower-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 lower 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 _wcslwr (
  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.                         if ( (*p >= (wchar_t)L'A') && (*p <= (wchar_t)L'Z') )
  59.                                 *p = *p - L'A' + L'a';
  60.                 return (wsrc);
  61.         } /* C locale */
  62.  
  63.         _lock_locale( local_lock_flag )
  64.  
  65. #if defined (_MT)
  66.         if (__lc_handle[LC_CTYPE] == _CLOCALEHANDLE) {
  67.                 _unlock_locale( local_lock_flag )
  68.                 for (p=wsrc; *p; p++)
  69.                         if ( (*p >= (wchar_t)L'A') && (*p <= (wchar_t)L'Z') )
  70.                                 *p = *p - L'A' + L'a';
  71.                 return (wsrc);
  72.         } /* C locale */
  73. #endif  /* defined (_MT) */
  74.  
  75.         /* Inquire size of wdst string */
  76.         if ((dstlen=__crtLCMapStringW(__lc_handle[LC_CTYPE], LCMAP_LOWERCASE,
  77.             wsrc, -1, wdst, 0, 0)) == 0)
  78.                 goto error_cleanup;
  79.  
  80.         /* Allocate space for wdst */
  81.         if ((wdst = (wchar_t *) _malloc_crt(dstlen*sizeof(wchar_t))) == NULL)
  82.                 goto error_cleanup;
  83.  
  84.         /* Map wrc string to wide-character wdst string in alternate case */
  85.         if (__crtLCMapStringW(__lc_handle[LC_CTYPE], LCMAP_LOWERCASE,
  86.             wsrc, -1, wdst, dstlen, 0) == 0)
  87.                 goto error_cleanup;
  88.  
  89.         /* Copy wdst string to user string */
  90.         wcscpy (wsrc, wdst);
  91.  
  92. error_cleanup:
  93.         _unlock_locale( local_lock_flag )
  94.         _free_crt (wdst);
  95.  
  96.         return (wsrc);
  97. }
  98.  
  99.