xref: /freebsd/lib/libc/locale/wctrans.c (revision 4645079944183e75b2d56f8481c1b3a4b28b0975)
146450799STim J. Robbins /*-
246450799STim J. Robbins  * Copyright (c) 2002 Tim J. Robbins.
346450799STim J. Robbins  * All rights reserved.
446450799STim J. Robbins  *
546450799STim J. Robbins  * Redistribution and use in source and binary forms, with or without
646450799STim J. Robbins  * modification, are permitted provided that the following conditions
746450799STim J. Robbins  * are met:
846450799STim J. Robbins  * 1. Redistributions of source code must retain the above copyright
946450799STim J. Robbins  *    notice, this list of conditions and the following disclaimer.
1046450799STim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
1146450799STim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
1246450799STim J. Robbins  *    documentation and/or other materials provided with the distribution.
1346450799STim J. Robbins  *
1446450799STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1546450799STim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1646450799STim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1746450799STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1846450799STim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1946450799STim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2046450799STim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2146450799STim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2246450799STim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2346450799STim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2446450799STim J. Robbins  * SUCH DAMAGE.
2546450799STim J. Robbins  */
2646450799STim J. Robbins 
2746450799STim J. Robbins #include <sys/cdefs.h>
2846450799STim J. Robbins __FBSDID("$FreeBSD$");
2946450799STim J. Robbins 
3046450799STim J. Robbins #include <ctype.h>
3146450799STim J. Robbins #include <errno.h>
3246450799STim J. Robbins #include <string.h>
3346450799STim J. Robbins #include <wctype.h>
3446450799STim J. Robbins 
3546450799STim J. Robbins enum {
3646450799STim J. Robbins 	_WCT_ERROR	= 0,
3746450799STim J. Robbins 	_WCT_TOLOWER	= 1,
3846450799STim J. Robbins 	_WCT_TOUPPER	= 2
3946450799STim J. Robbins };
4046450799STim J. Robbins 
4146450799STim J. Robbins /*
4246450799STim J. Robbins  * TODO: Supply a macro version of this.
4346450799STim J. Robbins  */
4446450799STim J. Robbins wint_t
4546450799STim J. Robbins towctrans(wint_t wc, wctrans_t desc)
4646450799STim J. Robbins {
4746450799STim J. Robbins 
4846450799STim J. Robbins 	switch (desc) {
4946450799STim J. Robbins 	case _WCT_TOLOWER:
5046450799STim J. Robbins 		wc = tolower(wc);
5146450799STim J. Robbins 		break;
5246450799STim J. Robbins 	case _WCT_TOUPPER:
5346450799STim J. Robbins 		wc = toupper(wc);
5446450799STim J. Robbins 		break;
5546450799STim J. Robbins 	case _WCT_ERROR:
5646450799STim J. Robbins 	default:
5746450799STim J. Robbins 		errno = EINVAL;
5846450799STim J. Robbins 		break;
5946450799STim J. Robbins 	}
6046450799STim J. Robbins 
6146450799STim J. Robbins 	return (wc);
6246450799STim J. Robbins }
6346450799STim J. Robbins 
6446450799STim J. Robbins wctrans_t
6546450799STim J. Robbins wctrans(const char *charclass)
6646450799STim J. Robbins {
6746450799STim J. Robbins 	struct {
6846450799STim J. Robbins 		const char	*name;
6946450799STim J. Robbins 		wctrans_t	 trans;
7046450799STim J. Robbins 	} ccls[] = {
7146450799STim J. Robbins 		{ "tolower",	_WCT_TOLOWER },
7246450799STim J. Robbins 		{ "toupper",	_WCT_TOUPPER },
7346450799STim J. Robbins 		{ NULL,		_WCT_ERROR },		/* Default */
7446450799STim J. Robbins 	};
7546450799STim J. Robbins 	int i;
7646450799STim J. Robbins 
7746450799STim J. Robbins 	i = 0;
7846450799STim J. Robbins 	while (ccls[i].name != NULL && strcmp(ccls[i].name, charclass) != 0)
7946450799STim J. Robbins 		i++;
8046450799STim J. Robbins 
8146450799STim J. Robbins 	if (ccls[i].trans == _WCT_ERROR)
8246450799STim J. Robbins 		errno = EINVAL;
8346450799STim J. Robbins 	return (ccls[i].trans);
8446450799STim J. Robbins }
85