wctrans.c revision 101313
1101313Stjr/*-
2101313Stjr * Copyright (c) 2002 Tim J. Robbins.
3101313Stjr * All rights reserved.
4101313Stjr *
5101313Stjr * Redistribution and use in source and binary forms, with or without
6101313Stjr * modification, are permitted provided that the following conditions
7101313Stjr * are met:
8101313Stjr * 1. Redistributions of source code must retain the above copyright
9101313Stjr *    notice, this list of conditions and the following disclaimer.
10101313Stjr * 2. Redistributions in binary form must reproduce the above copyright
11101313Stjr *    notice, this list of conditions and the following disclaimer in the
12101313Stjr *    documentation and/or other materials provided with the distribution.
13101313Stjr *
14101313Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15101313Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16101313Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17101313Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18101313Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19101313Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20101313Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21101313Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22101313Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23101313Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24101313Stjr * SUCH DAMAGE.
25101313Stjr */
26101313Stjr
27101313Stjr#include <sys/cdefs.h>
28101313Stjr__FBSDID("$FreeBSD: head/lib/libc/locale/wctrans.c 101313 2002-08-04 12:09:08Z tjr $");
29101313Stjr
30101313Stjr#include <ctype.h>
31101313Stjr#include <errno.h>
32101313Stjr#include <string.h>
33101313Stjr#include <wctype.h>
34101313Stjr
35101313Stjrenum {
36101313Stjr	_WCT_ERROR	= 0,
37101313Stjr	_WCT_TOLOWER	= 1,
38101313Stjr	_WCT_TOUPPER	= 2
39101313Stjr};
40101313Stjr
41101313Stjr/*
42101313Stjr * TODO: Supply a macro version of this.
43101313Stjr */
44101313Stjrwint_t
45101313Stjrtowctrans(wint_t wc, wctrans_t desc)
46101313Stjr{
47101313Stjr
48101313Stjr	switch (desc) {
49101313Stjr	case _WCT_TOLOWER:
50101313Stjr		wc = tolower(wc);
51101313Stjr		break;
52101313Stjr	case _WCT_TOUPPER:
53101313Stjr		wc = toupper(wc);
54101313Stjr		break;
55101313Stjr	case _WCT_ERROR:
56101313Stjr	default:
57101313Stjr		errno = EINVAL;
58101313Stjr		break;
59101313Stjr	}
60101313Stjr
61101313Stjr	return (wc);
62101313Stjr}
63101313Stjr
64101313Stjrwctrans_t
65101313Stjrwctrans(const char *charclass)
66101313Stjr{
67101313Stjr	struct {
68101313Stjr		const char	*name;
69101313Stjr		wctrans_t	 trans;
70101313Stjr	} ccls[] = {
71101313Stjr		{ "tolower",	_WCT_TOLOWER },
72101313Stjr		{ "toupper",	_WCT_TOUPPER },
73101313Stjr		{ NULL,		_WCT_ERROR },		/* Default */
74101313Stjr	};
75101313Stjr	int i;
76101313Stjr
77101313Stjr	i = 0;
78101313Stjr	while (ccls[i].name != NULL && strcmp(ccls[i].name, charclass) != 0)
79101313Stjr		i++;
80101313Stjr
81101313Stjr	if (ccls[i].trans == _WCT_ERROR)
82101313Stjr		errno = EINVAL;
83101313Stjr	return (ccls[i].trans);
84101313Stjr}
85