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

  1. /***
  2. *strlwr.c - routine to map upper-case characters in a string to lower-case
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Converts all the upper case characters in a string to lower 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 *_strlwr(string) - map upper-case characters in a string to lower-case
  27. *
  28. *Purpose:
  29. *       _strlwr() converts upper-case characters in a null-terminated string
  30. *       to their lower-case equivalents.  Conversion is done in place and
  31. *       characters other than upper-case letters are not modified.
  32. *
  33. *       In the C locale, this function modifies only 7-bit ASCII characters
  34. *       in the range 0x41 through 0x5A ('A' through 'Z').
  35. *
  36. *       If the locale is not the 'C' locale, MapString() 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 lower 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 _strlwr (
  51.         char * string
  52.         )
  53. {
  54. #if defined (_WIN32)
  55.  
  56.         int dstlen;                 /* len of dst string, with null  */
  57.         unsigned char *dst = NULL;  /* destination string */
  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.  
  66.                 for (cp=string; *cp; ++cp)
  67.                 {
  68.                         if ('A' <= *cp && *cp <= 'Z')
  69.                                 *cp += 'a' - 'A';
  70.                 }
  71.  
  72.                 return(string);
  73.         } /* C locale */
  74.  
  75.         _lock_locale( local_lock_flag )
  76.  
  77. #if defined (_MT)
  78.  
  79.         if (__lc_handle[LC_CTYPE] == _CLOCALEHANDLE)
  80.         {
  81.         char *cp;               /* traverses string for C locale conversion */
  82.                 _unlock_locale( local_lock_flag )
  83.  
  84.                 for (cp=string; *cp; ++cp)
  85.                 {
  86.                         if ('A' <= *cp && *cp <= 'Z')
  87.                                 *cp += 'a' - 'A';
  88.                 }
  89.  
  90.                 return(string);
  91.         } /* C locale */
  92. #endif  /* defined (_MT) */
  93.  
  94.         /* Inquire size of dst string */
  95.         if (0 == (dstlen = __crtLCMapStringA(__lc_handle[LC_CTYPE], LCMAP_LOWERCASE,
  96.             string, -1, NULL, 0, 0, TRUE)))
  97.                 goto error_cleanup;
  98.  
  99.         /* Allocate space for dst */
  100.         if (NULL == (dst = (unsigned char *) _malloc_crt(dstlen*sizeof(unsigned char))))
  101.                 goto error_cleanup;
  102.  
  103.         /* Map src string to dst string in alternate case */
  104.         if (0 == __crtLCMapStringA(__lc_handle[LC_CTYPE], LCMAP_LOWERCASE,
  105.             string, -1, dst, dstlen, 0, TRUE))
  106.                 goto error_cleanup;
  107.  
  108.         /* copy dst string to return string */
  109.         strcpy(string, dst);
  110.  
  111. error_cleanup:
  112.         _unlock_locale( local_lock_flag )
  113.         _free_crt (dst);
  114.  
  115.         return (string);
  116.  
  117. #else  /* defined (_WIN32) */
  118.  
  119.         char * cp;
  120.  
  121.         for (cp=string; *cp; ++cp)
  122.         {
  123.                 if ('A' <= *cp && *cp <= 'Z')
  124.                         *cp += 'a' - 'A';
  125.         }
  126.  
  127.         return(string);
  128.  
  129.  
  130. #endif  /* defined (_WIN32) */
  131. }
  132.