summaryrefslogtreecommitdiffstats
path: root/private/crt32/string/wcslwr.c
blob: b8cf567cccd9ac581241b327aa113fbc4b65d8a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***
*wcslwr.c - routine to map upper-case characters in a wchar_t string 
*	to lower-case
*
*	Copyright (c) 1985-1992, Microsoft Corporation. All rights reserved.
*
*Purpose:
*	Converts all the upper case characters in a wchar_t string 
*	to lower case, in place.
*
*Revision History:
*	09-09-91   ETC	Created from strlwr.c.
*	04-06-92   KRS	Make work without _INTL also.
*	08-19-92   KRS	Activate NLS support.
*	08-22-92   SRW	Allow INTL definition to be conditional for building ntcrt.lib
*	09-02-92   SRW	Get _INTL definition via ..\crt32.def
*       06-02-93   SRW  ignore _INTL if _NTSUBSET_ defined.
*
*******************************************************************************/

#include <cruntime.h>
#include <string.h>
#include <malloc.h>
#include <locale.h>
#include <ctype.h>
#include <setlocal.h>
#include <os2dll.h>

/***
*wchar_t *_wcslwr(string) - map upper-case characters in a string to lower-case
*
*Purpose:
*	wcslwr converts upper-case characters in a null-terminated wchar_t 
*	string to their lower-case equivalents.  The result may be longer or
*	shorter than the original string.  Assumes enough space in string
*	to hold the result.
*
*Entry:
*	wchar_t *wsrc - wchar_t string to change to lower case
*
*Exit:
*	input string address
*
*Exceptions:
*	on an error, the original string is unaltered
*
*******************************************************************************/

wchar_t * _CALLTYPE1 _wcslwr (
	wchar_t * wsrc
	)
{
#if defined(_INTL) && !defined(_NTSUBSET_)
	wchar_t *p;		/* traverses string for C locale conversion */
	wchar_t *wdst = NULL;	/* wide version of string in alternate case */
	int srclen;		/* general purpose length of source string */
	int dstlen;		/* len of wdst string, wide chars, no null  */

	_mlock (_LC_CTYPE_LOCK);

	if (_lc_handle[LC_CTYPE] == _CLOCALEHANDLE) {
		_munlock (_LC_CTYPE_LOCK);
		for (p=wsrc; *p; p++)
			if (iswupper(*p))
				*p = *p - L'A' + L'a';
		return (wsrc);
	} /* C locale */

	/* Inquire size of wdst string */
	srclen = wcslen(wsrc) + 1;
	if ((dstlen=LCMapStringW(_lc_handle[LC_CTYPE], LCMAP_LOWERCASE, wsrc, 
		srclen, wdst, 0)) == 0)
		goto error_cleanup;

	/* Allocate space for wdst */
	if ((wdst = (wchar_t *) malloc(dstlen*sizeof(wchar_t))) == NULL)
		goto error_cleanup;

	/* Map wrc string to wide-character wdst string in alternate case */
	if (LCMapStringW(_lc_handle[LC_CTYPE], LCMAP_LOWERCASE, wsrc,
		srclen, wdst, dstlen) == 0)
		goto error_cleanup;

	/* Copy wdst string to user string */
	wcscpy (wsrc, wdst);

error_cleanup:
	_munlock (_LC_CTYPE_LOCK);
	free (wdst);
#else
	wchar_t * p;

	for (p=wsrc; *p; ++p)
	{
		if (L'A' <= *p && *p <= L'Z')
			*p += (wchar_t)L'a' - (wchar_t)L'A';
	}
#endif /* _INTL */
	return (wsrc);
}