1166124Srafan/****************************************************************************
2184989Srafan * Copyright (c) 2003-2005,2008 Free Software Foundation, Inc.              *
3166124Srafan *                                                                          *
4166124Srafan * Permission is hereby granted, free of charge, to any person obtaining a  *
5166124Srafan * copy of this software and associated documentation files (the            *
6166124Srafan * "Software"), to deal in the Software without restriction, including      *
7166124Srafan * without limitation the rights to use, copy, modify, merge, publish,      *
8166124Srafan * distribute, distribute with modifications, sublicense, and/or sell       *
9166124Srafan * copies of the Software, and to permit persons to whom the Software is    *
10166124Srafan * furnished to do so, subject to the following conditions:                 *
11166124Srafan *                                                                          *
12166124Srafan * The above copyright notice and this permission notice shall be included  *
13166124Srafan * in all copies or substantial portions of the Software.                   *
14166124Srafan *                                                                          *
15166124Srafan * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16166124Srafan * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17166124Srafan * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18166124Srafan * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19166124Srafan * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20166124Srafan * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21166124Srafan * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22166124Srafan *                                                                          *
23166124Srafan * Except as contained in this notice, the name(s) of the above copyright   *
24166124Srafan * holders shall not be used in advertising or otherwise to promote the     *
25166124Srafan * sale, use or other dealings in this Software without prior written       *
26166124Srafan * authorization.                                                           *
27166124Srafan ****************************************************************************/
28166124Srafan
29166124Srafan/*
30166124Srafan**	Support functions for wide/narrow conversion.
31166124Srafan*/
32166124Srafan
33166124Srafan#include <curses.priv.h>
34166124Srafan
35184989SrafanMODULE_ID("$Id: charable.c,v 1.5 2008/07/05 20:51:41 tom Exp $")
36166124Srafan
37166124SrafanNCURSES_EXPORT(bool) _nc_is_charable(wchar_t ch)
38166124Srafan{
39166124Srafan    bool result;
40166124Srafan#if HAVE_WCTOB
41184989Srafan    result = (wctob((wint_t) ch) == (int) ch);
42166124Srafan#else
43166124Srafan    result = (_nc_to_char(ch) >= 0);
44166124Srafan#endif
45166124Srafan    return result;
46166124Srafan}
47166124Srafan
48166124SrafanNCURSES_EXPORT(int) _nc_to_char(wint_t ch)
49166124Srafan{
50166124Srafan    int result;
51166124Srafan#if HAVE_WCTOB
52166124Srafan    result = wctob(ch);
53166124Srafan#elif HAVE_WCTOMB
54166124Srafan    char temp[MB_LEN_MAX];
55166124Srafan    result = wctomb(temp, ch);
56166124Srafan    if (strlen(temp) == 1)
57166124Srafan	result = UChar(temp[0]);
58166124Srafan    else
59166124Srafan	result = -1;
60166124Srafan#endif
61166124Srafan    return result;
62166124Srafan}
63166124Srafan
64166124SrafanNCURSES_EXPORT(wint_t) _nc_to_widechar(int ch)
65166124Srafan{
66166124Srafan    wint_t result;
67166124Srafan#if HAVE_BTOWC
68166124Srafan    result = btowc(ch);
69166124Srafan#elif HAVE_MBTOWC
70166124Srafan    wchar_t convert;
71166124Srafan    char temp[2];
72166124Srafan    temp[0] = ch;
73166124Srafan    temp[1] = '\0';
74166124Srafan    if (mbtowc(&convert, temp, 1) >= 0)
75166124Srafan	result = convert;
76166124Srafan    else
77166124Srafan	result = WEOF;
78166124Srafan#endif
79166124Srafan    return result;
80166124Srafan}
81