charable.c revision 184989
162607Sitojun/****************************************************************************
2122677Sume * Copyright (c) 2003-2005,2008 Free Software Foundation, Inc.              *
355163Sshin *                                                                          *
455163Sshin * Permission is hereby granted, free of charge, to any person obtaining a  *
555163Sshin * copy of this software and associated documentation files (the            *
655163Sshin * "Software"), to deal in the Software without restriction, including      *
762607Sitojun * without limitation the rights to use, copy, modify, merge, publish,      *
855163Sshin * distribute, distribute with modifications, sublicense, and/or sell       *
955163Sshin * copies of the Software, and to permit persons to whom the Software is    *
1055163Sshin * furnished to do so, subject to the following conditions:                 *
1155163Sshin *                                                                          *
1255163Sshin * The above copyright notice and this permission notice shall be included  *
1355163Sshin * in all copies or substantial portions of the Software.                   *
1455163Sshin *                                                                          *
1555163Sshin * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1655163Sshin * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1755163Sshin * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1855163Sshin * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1962607Sitojun * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2055163Sshin * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2155163Sshin * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2255163Sshin *                                                                          *
2355163Sshin * Except as contained in this notice, the name(s) of the above copyright   *
2455163Sshin * holders shall not be used in advertising or otherwise to promote the     *
2555163Sshin * sale, use or other dealings in this Software without prior written       *
2655163Sshin * authorization.                                                           *
2755163Sshin ****************************************************************************/
2855163Sshin
2955163Sshin/*
3055163Sshin**	Support functions for wide/narrow conversion.
3155163Sshin*/
3255163Sshin
3355163Sshin#include <curses.priv.h>
34243232Shrs
3555163SshinMODULE_ID("$Id: charable.c,v 1.5 2008/07/05 20:51:41 tom Exp $")
3655163Sshin
3755163SshinNCURSES_EXPORT(bool) _nc_is_charable(wchar_t ch)
3855163Sshin{
3955163Sshin    bool result;
4055163Sshin#if HAVE_WCTOB
4155163Sshin    result = (wctob((wint_t) ch) == (int) ch);
4255163Sshin#else
4355163Sshin    result = (_nc_to_char(ch) >= 0);
4455163Sshin#endif
4555163Sshin    return result;
4655163Sshin}
4755163Sshin
4855163SshinNCURSES_EXPORT(int) _nc_to_char(wint_t ch)
4955163Sshin{
5055163Sshin    int result;
5162607Sitojun#if HAVE_WCTOB
5255163Sshin    result = wctob(ch);
53119076Sume#elif HAVE_WCTOMB
54119076Sume    char temp[MB_LEN_MAX];
55119076Sume    result = wctomb(temp, ch);
5655163Sshin    if (strlen(temp) == 1)
5755163Sshin	result = UChar(temp[0]);
5855163Sshin    else
5955163Sshin	result = -1;
6055163Sshin#endif
6155163Sshin    return result;
6255163Sshin}
6355163Sshin
6455163SshinNCURSES_EXPORT(wint_t) _nc_to_widechar(int ch)
6555163Sshin{
6662607Sitojun    wint_t result;
6755163Sshin#if HAVE_BTOWC
6862607Sitojun    result = btowc(ch);
6955163Sshin#elif HAVE_MBTOWC
7055163Sshin    wchar_t convert;
7155163Sshin    char temp[2];
7255163Sshin    temp[0] = ch;
7355163Sshin    temp[1] = '\0';
7462607Sitojun    if (mbtowc(&convert, temp, 1) >= 0)
7555163Sshin	result = convert;
7655163Sshin    else
7755163Sshin	result = WEOF;
7855163Sshin#endif
7955163Sshin    return result;
8055163Sshin}
8155163Sshin