xref: /illumos-gate/usr/src/lib/libc/port/locale/wctrans.c (revision 4a38094c)
14297a3b0SGarrett D'Amore /*
22d08521bSGarrett D'Amore  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
36b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
44297a3b0SGarrett D'Amore  * Copyright (c) 2002 Tim J. Robbins.
54297a3b0SGarrett D'Amore  * All rights reserved.
64297a3b0SGarrett D'Amore  *
74297a3b0SGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
84297a3b0SGarrett D'Amore  * modification, are permitted provided that the following conditions
94297a3b0SGarrett D'Amore  * are met:
104297a3b0SGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
114297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
124297a3b0SGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
134297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
144297a3b0SGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
154297a3b0SGarrett D'Amore  *
164297a3b0SGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174297a3b0SGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184297a3b0SGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194297a3b0SGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204297a3b0SGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214297a3b0SGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224297a3b0SGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234297a3b0SGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244297a3b0SGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254297a3b0SGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264297a3b0SGarrett D'Amore  * SUCH DAMAGE.
274297a3b0SGarrett D'Amore  */
284297a3b0SGarrett D'Amore 
294297a3b0SGarrett D'Amore #include "lint.h"
304297a3b0SGarrett D'Amore #include <errno.h>
314297a3b0SGarrett D'Amore #include <string.h>
324297a3b0SGarrett D'Amore #include <wctype.h>
332d08521bSGarrett D'Amore #include <locale.h>
344297a3b0SGarrett D'Amore 
354297a3b0SGarrett D'Amore enum {
364297a3b0SGarrett D'Amore 	_WCT_ERROR	= 0,
374297a3b0SGarrett D'Amore 	_WCT_TOLOWER	= 1,
384297a3b0SGarrett D'Amore 	_WCT_TOUPPER	= 2
394297a3b0SGarrett D'Amore };
404297a3b0SGarrett D'Amore 
414297a3b0SGarrett D'Amore wint_t
towctrans_l(wint_t wc,wctrans_t desc,locale_t loc)422d08521bSGarrett D'Amore towctrans_l(wint_t wc, wctrans_t desc, locale_t loc)
434297a3b0SGarrett D'Amore {
444297a3b0SGarrett D'Amore 	switch (desc) {
454297a3b0SGarrett D'Amore 	case _WCT_TOLOWER:
462d08521bSGarrett D'Amore 		wc = towlower_l(wc, loc);
474297a3b0SGarrett D'Amore 		break;
484297a3b0SGarrett D'Amore 	case _WCT_TOUPPER:
492d08521bSGarrett D'Amore 		wc = towupper_l(wc, loc);
504297a3b0SGarrett D'Amore 		break;
514297a3b0SGarrett D'Amore 	case _WCT_ERROR:
524297a3b0SGarrett D'Amore 	default:
534297a3b0SGarrett D'Amore 		errno = EINVAL;
544297a3b0SGarrett D'Amore 		break;
554297a3b0SGarrett D'Amore 	}
564297a3b0SGarrett D'Amore 
574297a3b0SGarrett D'Amore 	return (wc);
584297a3b0SGarrett D'Amore }
594297a3b0SGarrett D'Amore 
602d08521bSGarrett D'Amore wint_t
towctrans(wint_t wc,wctrans_t desc)612d08521bSGarrett D'Amore towctrans(wint_t wc, wctrans_t desc)
622d08521bSGarrett D'Amore {
632d08521bSGarrett D'Amore 	return (towctrans_l(wc, desc, uselocale(NULL)));
642d08521bSGarrett D'Amore }
652d08521bSGarrett D'Amore 
662d08521bSGarrett D'Amore /*
672d08521bSGarrett D'Amore  * For *now* we don't support locale sensitive transforms besides toupper
682d08521bSGarrett D'Amore  * and tolower.
692d08521bSGarrett D'Amore  */
704297a3b0SGarrett D'Amore wctrans_t
wctrans_l(const char * charclass,locale_t loc __unused)71*4a38094cSToomas Soome wctrans_l(const char *charclass, locale_t loc __unused)
724297a3b0SGarrett D'Amore {
734297a3b0SGarrett D'Amore 	struct {
744297a3b0SGarrett D'Amore 		const char	*name;
754297a3b0SGarrett D'Amore 		wctrans_t	 trans;
764297a3b0SGarrett D'Amore 	} ccls[] = {
774297a3b0SGarrett D'Amore 		{ "tolower",	_WCT_TOLOWER },
784297a3b0SGarrett D'Amore 		{ "toupper",	_WCT_TOUPPER },
794297a3b0SGarrett D'Amore 		{ NULL,		_WCT_ERROR },		/* Default */
804297a3b0SGarrett D'Amore 	};
814297a3b0SGarrett D'Amore 	int i;
824297a3b0SGarrett D'Amore 
834297a3b0SGarrett D'Amore 	i = 0;
844297a3b0SGarrett D'Amore 	while (ccls[i].name != NULL && strcmp(ccls[i].name, charclass) != 0)
854297a3b0SGarrett D'Amore 		i++;
864297a3b0SGarrett D'Amore 
874297a3b0SGarrett D'Amore 	if (ccls[i].trans == _WCT_ERROR)
884297a3b0SGarrett D'Amore 		errno = EINVAL;
894297a3b0SGarrett D'Amore 	return (ccls[i].trans);
904297a3b0SGarrett D'Amore }
912d08521bSGarrett D'Amore 
922d08521bSGarrett D'Amore wctrans_t
wctrans(const char * charclass)932d08521bSGarrett D'Amore wctrans(const char *charclass)
942d08521bSGarrett D'Amore {
952d08521bSGarrett D'Amore 	return (wctrans_l(charclass, uselocale(NULL)));
962d08521bSGarrett D'Amore }
97