1 /****************************************************************************
2 * Copyright 2018,2020 Thomas E. Dickey *
3 * Copyright 2012,2013 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 #include <curses.priv.h>
34 MODULE_ID("$Id: widechars.c,v 1.8 2020/07/11 20:48:42 anonymous.maarten Exp $")
36 #if defined(_WIN32) && !defined(_MSC_VER)
38 * MinGW has wide-character functions, but they do not work correctly.
42 _nc_mbtowc(wchar_t *pwc, const char *s, size_t n)
48 if (s != 0 && n != 0) {
50 * MultiByteToWideChar() can decide to return more than one
51 * wide-character. We want only one. Ignore any trailing null, both
52 * in the initial count and in the conversion.
55 for (try = 1; try <= (int) n; ++try) {
56 count = MultiByteToWideChar(CP_UTF8,
62 TR(TRACE_BITS, ("...try %d:%d", try, count));
67 if (count < 1 || count > 2) {
71 memset(&actual, 0, sizeof(actual));
72 count = MultiByteToWideChar(CP_UTF8,
78 TR(TRACE_BITS, ("\twin32 ->%#x, %#x", actual[0], actual[1]));
93 _nc_mblen(const char *s, size_t n)
99 if (s != 0 && n != 0) {
100 count = _nc_mbtowc(&temp, s, n);
102 int check = WideCharToMultiByte(CP_UTF8,
107 0, /* compute length only */
110 TR(TRACE_BITS, ("\tcheck ->%d\n", check));
111 if (check > 0 && (size_t) check <= n) {
123 _nc_wctomb(char *s, wchar_t wc)
128 check = WideCharToMultiByte(CP_UTF8,
133 0, /* compute length only */
137 result = WideCharToMultiByte(CP_UTF8,
153 #endif /* USE_WIDEC_SUPPORT */