1*c7ef0cfcSnicm /* $OpenBSD: charable.c,v 1.2 2023/10/17 09:52:09 nicm Exp $ */
26bc6570dSnicm
36bc6570dSnicm /****************************************************************************
4*c7ef0cfcSnicm * Copyright 2018,2020 Thomas E. Dickey *
5*c7ef0cfcSnicm * Copyright 2003-2005,2008 Free Software Foundation, Inc. *
66bc6570dSnicm * *
76bc6570dSnicm * Permission is hereby granted, free of charge, to any person obtaining a *
86bc6570dSnicm * copy of this software and associated documentation files (the *
96bc6570dSnicm * "Software"), to deal in the Software without restriction, including *
106bc6570dSnicm * without limitation the rights to use, copy, modify, merge, publish, *
116bc6570dSnicm * distribute, distribute with modifications, sublicense, and/or sell *
126bc6570dSnicm * copies of the Software, and to permit persons to whom the Software is *
136bc6570dSnicm * furnished to do so, subject to the following conditions: *
146bc6570dSnicm * *
156bc6570dSnicm * The above copyright notice and this permission notice shall be included *
166bc6570dSnicm * in all copies or substantial portions of the Software. *
176bc6570dSnicm * *
186bc6570dSnicm * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
196bc6570dSnicm * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
206bc6570dSnicm * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
216bc6570dSnicm * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
226bc6570dSnicm * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
236bc6570dSnicm * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
246bc6570dSnicm * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
256bc6570dSnicm * *
266bc6570dSnicm * Except as contained in this notice, the name(s) of the above copyright *
276bc6570dSnicm * holders shall not be used in advertising or otherwise to promote the *
286bc6570dSnicm * sale, use or other dealings in this Software without prior written *
296bc6570dSnicm * authorization. *
306bc6570dSnicm ****************************************************************************/
316bc6570dSnicm
326bc6570dSnicm /*
336bc6570dSnicm ** Support functions for wide/narrow conversion.
346bc6570dSnicm */
356bc6570dSnicm
366bc6570dSnicm #include <curses.priv.h>
376bc6570dSnicm
38*c7ef0cfcSnicm MODULE_ID("$Id: charable.c,v 1.2 2023/10/17 09:52:09 nicm Exp $")
396bc6570dSnicm
NCURSES_EXPORT(bool)406bc6570dSnicm NCURSES_EXPORT(bool) _nc_is_charable(wchar_t ch)
416bc6570dSnicm {
426bc6570dSnicm bool result;
436bc6570dSnicm #if HAVE_WCTOB
446bc6570dSnicm result = (wctob((wint_t) ch) == (int) ch);
456bc6570dSnicm #else
466bc6570dSnicm result = (_nc_to_char(ch) >= 0);
476bc6570dSnicm #endif
486bc6570dSnicm return result;
496bc6570dSnicm }
506bc6570dSnicm
_nc_to_char(wint_t ch)516bc6570dSnicm NCURSES_EXPORT(int) _nc_to_char(wint_t ch)
526bc6570dSnicm {
536bc6570dSnicm int result;
546bc6570dSnicm #if HAVE_WCTOB
556bc6570dSnicm result = wctob(ch);
566bc6570dSnicm #elif HAVE_WCTOMB
576bc6570dSnicm char temp[MB_LEN_MAX];
586bc6570dSnicm result = wctomb(temp, ch);
596bc6570dSnicm if (strlen(temp) == 1)
606bc6570dSnicm result = UChar(temp[0]);
616bc6570dSnicm else
626bc6570dSnicm result = -1;
63*c7ef0cfcSnicm #else
64*c7ef0cfcSnicm #error expected either wctob/wctomb
656bc6570dSnicm #endif
666bc6570dSnicm return result;
676bc6570dSnicm }
686bc6570dSnicm
_nc_to_widechar(int ch)696bc6570dSnicm NCURSES_EXPORT(wint_t) _nc_to_widechar(int ch)
706bc6570dSnicm {
716bc6570dSnicm wint_t result;
726bc6570dSnicm #if HAVE_BTOWC
736bc6570dSnicm result = btowc(ch);
746bc6570dSnicm #elif HAVE_MBTOWC
756bc6570dSnicm wchar_t convert;
766bc6570dSnicm char temp[2];
776bc6570dSnicm temp[0] = ch;
786bc6570dSnicm temp[1] = '\0';
796bc6570dSnicm if (mbtowc(&convert, temp, 1) >= 0)
806bc6570dSnicm result = convert;
816bc6570dSnicm else
826bc6570dSnicm result = WEOF;
83*c7ef0cfcSnicm #else
84*c7ef0cfcSnicm #error expected either btowc/mbtowc
856bc6570dSnicm #endif
866bc6570dSnicm return result;
876bc6570dSnicm }
88