]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/widechar/charable.c
ncurses 5.7 - patch 20100327
[ncurses.git] / ncurses / widechar / charable.c
1 /****************************************************************************
2  * Copyright (c) 2003-2005,2008 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /*
30 **      Support functions for wide/narrow conversion.
31 */
32
33 #include <curses.priv.h>
34
35 MODULE_ID("$Id: charable.c,v 1.5 2008/07/05 20:51:41 tom Exp $")
36
37 NCURSES_EXPORT(bool) _nc_is_charable(wchar_t ch)
38 {
39     bool result;
40 #if HAVE_WCTOB
41     result = (wctob((wint_t) ch) == (int) ch);
42 #else
43     result = (_nc_to_char(ch) >= 0);
44 #endif
45     return result;
46 }
47
48 NCURSES_EXPORT(int) _nc_to_char(wint_t ch)
49 {
50     int result;
51 #if HAVE_WCTOB
52     result = wctob(ch);
53 #elif HAVE_WCTOMB
54     char temp[MB_LEN_MAX];
55     result = wctomb(temp, ch);
56     if (strlen(temp) == 1)
57         result = UChar(temp[0]);
58     else
59         result = -1;
60 #endif
61     return result;
62 }
63
64 NCURSES_EXPORT(wint_t) _nc_to_widechar(int ch)
65 {
66     wint_t result;
67 #if HAVE_BTOWC
68     result = btowc(ch);
69 #elif HAVE_MBTOWC
70     wchar_t convert;
71     char temp[2];
72     temp[0] = ch;
73     temp[1] = '\0';
74     if (mbtowc(&convert, temp, 1) >= 0)
75         result = convert;
76     else
77         result = WEOF;
78 #endif
79     return result;
80 }