1*c7ef0cfcSnicm /****************************************************************************
2*c7ef0cfcSnicm * Copyright 2018,2020 Thomas E. Dickey *
3*c7ef0cfcSnicm * Copyright 2012,2013 Free Software Foundation, Inc. *
4*c7ef0cfcSnicm * *
5*c7ef0cfcSnicm * Permission is hereby granted, free of charge, to any person obtaining a *
6*c7ef0cfcSnicm * copy of this software and associated documentation files (the *
7*c7ef0cfcSnicm * "Software"), to deal in the Software without restriction, including *
8*c7ef0cfcSnicm * without limitation the rights to use, copy, modify, merge, publish, *
9*c7ef0cfcSnicm * distribute, distribute with modifications, sublicense, and/or sell *
10*c7ef0cfcSnicm * copies of the Software, and to permit persons to whom the Software is *
11*c7ef0cfcSnicm * furnished to do so, subject to the following conditions: *
12*c7ef0cfcSnicm * *
13*c7ef0cfcSnicm * The above copyright notice and this permission notice shall be included *
14*c7ef0cfcSnicm * in all copies or substantial portions of the Software. *
15*c7ef0cfcSnicm * *
16*c7ef0cfcSnicm * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17*c7ef0cfcSnicm * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18*c7ef0cfcSnicm * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19*c7ef0cfcSnicm * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20*c7ef0cfcSnicm * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21*c7ef0cfcSnicm * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22*c7ef0cfcSnicm * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23*c7ef0cfcSnicm * *
24*c7ef0cfcSnicm * Except as contained in this notice, the name(s) of the above copyright *
25*c7ef0cfcSnicm * holders shall not be used in advertising or otherwise to promote the *
26*c7ef0cfcSnicm * sale, use or other dealings in this Software without prior written *
27*c7ef0cfcSnicm * authorization. *
28*c7ef0cfcSnicm ****************************************************************************/
29*c7ef0cfcSnicm
30*c7ef0cfcSnicm #include <curses.priv.h>
31*c7ef0cfcSnicm
32*c7ef0cfcSnicm #if USE_WIDEC_SUPPORT
33*c7ef0cfcSnicm
34*c7ef0cfcSnicm MODULE_ID("$Id: widechars.c,v 1.1 2023/10/17 09:52:09 nicm Exp $")
35*c7ef0cfcSnicm
36*c7ef0cfcSnicm #if (defined(_NC_WINDOWS)) && !defined(_NC_MSC)
37*c7ef0cfcSnicm /*
38*c7ef0cfcSnicm * MinGW has wide-character functions, but they do not work correctly.
39*c7ef0cfcSnicm */
40*c7ef0cfcSnicm
41*c7ef0cfcSnicm int
_nc_mbtowc(wchar_t * pwc,const char * s,size_t n)42*c7ef0cfcSnicm _nc_mbtowc(wchar_t *pwc, const char *s, size_t n)
43*c7ef0cfcSnicm {
44*c7ef0cfcSnicm int result;
45*c7ef0cfcSnicm int count;
46*c7ef0cfcSnicm int try;
47*c7ef0cfcSnicm
48*c7ef0cfcSnicm if (s != 0 && n != 0) {
49*c7ef0cfcSnicm /*
50*c7ef0cfcSnicm * MultiByteToWideChar() can decide to return more than one
51*c7ef0cfcSnicm * wide-character. We want only one. Ignore any trailing null, both
52*c7ef0cfcSnicm * in the initial count and in the conversion.
53*c7ef0cfcSnicm */
54*c7ef0cfcSnicm count = 0;
55*c7ef0cfcSnicm for (try = 1; try <= (int) n; ++try) {
56*c7ef0cfcSnicm count = MultiByteToWideChar(CP_UTF8,
57*c7ef0cfcSnicm MB_ERR_INVALID_CHARS,
58*c7ef0cfcSnicm s,
59*c7ef0cfcSnicm try,
60*c7ef0cfcSnicm pwc,
61*c7ef0cfcSnicm 0);
62*c7ef0cfcSnicm TR(TRACE_BITS, ("...try %d:%d", try, count));
63*c7ef0cfcSnicm if (count > 0) {
64*c7ef0cfcSnicm break;
65*c7ef0cfcSnicm }
66*c7ef0cfcSnicm }
67*c7ef0cfcSnicm if (count < 1 || count > 2) {
68*c7ef0cfcSnicm result = -1;
69*c7ef0cfcSnicm } else {
70*c7ef0cfcSnicm wchar_t actual[2];
71*c7ef0cfcSnicm memset(&actual, 0, sizeof(actual));
72*c7ef0cfcSnicm count = MultiByteToWideChar(CP_UTF8,
73*c7ef0cfcSnicm MB_ERR_INVALID_CHARS,
74*c7ef0cfcSnicm s,
75*c7ef0cfcSnicm try,
76*c7ef0cfcSnicm actual,
77*c7ef0cfcSnicm 2);
78*c7ef0cfcSnicm TR(TRACE_BITS, ("\twin32 ->%#x, %#x", actual[0], actual[1]));
79*c7ef0cfcSnicm *pwc = actual[0];
80*c7ef0cfcSnicm if (actual[1] != 0)
81*c7ef0cfcSnicm result = -1;
82*c7ef0cfcSnicm else
83*c7ef0cfcSnicm result = try;
84*c7ef0cfcSnicm }
85*c7ef0cfcSnicm } else {
86*c7ef0cfcSnicm result = 0;
87*c7ef0cfcSnicm }
88*c7ef0cfcSnicm
89*c7ef0cfcSnicm return result;
90*c7ef0cfcSnicm }
91*c7ef0cfcSnicm
92*c7ef0cfcSnicm int
_nc_mblen(const char * s,size_t n)93*c7ef0cfcSnicm _nc_mblen(const char *s, size_t n)
94*c7ef0cfcSnicm {
95*c7ef0cfcSnicm int result = -1;
96*c7ef0cfcSnicm int count;
97*c7ef0cfcSnicm wchar_t temp;
98*c7ef0cfcSnicm
99*c7ef0cfcSnicm if (s != 0 && n != 0) {
100*c7ef0cfcSnicm count = _nc_mbtowc(&temp, s, n);
101*c7ef0cfcSnicm if (count == 1) {
102*c7ef0cfcSnicm int check = WideCharToMultiByte(CP_UTF8,
103*c7ef0cfcSnicm 0,
104*c7ef0cfcSnicm &temp,
105*c7ef0cfcSnicm 1,
106*c7ef0cfcSnicm NULL,
107*c7ef0cfcSnicm 0, /* compute length only */
108*c7ef0cfcSnicm NULL,
109*c7ef0cfcSnicm NULL);
110*c7ef0cfcSnicm TR(TRACE_BITS, ("\tcheck ->%d\n", check));
111*c7ef0cfcSnicm if (check > 0 && (size_t) check <= n) {
112*c7ef0cfcSnicm result = check;
113*c7ef0cfcSnicm }
114*c7ef0cfcSnicm }
115*c7ef0cfcSnicm } else {
116*c7ef0cfcSnicm result = 0;
117*c7ef0cfcSnicm }
118*c7ef0cfcSnicm
119*c7ef0cfcSnicm return result;
120*c7ef0cfcSnicm }
121*c7ef0cfcSnicm
122*c7ef0cfcSnicm int __MINGW_NOTHROW
_nc_wctomb(char * s,wchar_t wc)123*c7ef0cfcSnicm _nc_wctomb(char *s, wchar_t wc)
124*c7ef0cfcSnicm {
125*c7ef0cfcSnicm int result;
126*c7ef0cfcSnicm int check;
127*c7ef0cfcSnicm
128*c7ef0cfcSnicm check = WideCharToMultiByte(CP_UTF8,
129*c7ef0cfcSnicm 0,
130*c7ef0cfcSnicm &wc,
131*c7ef0cfcSnicm 1,
132*c7ef0cfcSnicm NULL,
133*c7ef0cfcSnicm 0, /* compute length only */
134*c7ef0cfcSnicm NULL,
135*c7ef0cfcSnicm NULL);
136*c7ef0cfcSnicm if (check > 0) {
137*c7ef0cfcSnicm result = WideCharToMultiByte(CP_UTF8,
138*c7ef0cfcSnicm 0,
139*c7ef0cfcSnicm &wc,
140*c7ef0cfcSnicm 1,
141*c7ef0cfcSnicm s,
142*c7ef0cfcSnicm check + 1,
143*c7ef0cfcSnicm NULL,
144*c7ef0cfcSnicm NULL);
145*c7ef0cfcSnicm } else {
146*c7ef0cfcSnicm result = -1;
147*c7ef0cfcSnicm }
148*c7ef0cfcSnicm return result;
149*c7ef0cfcSnicm }
150*c7ef0cfcSnicm
151*c7ef0cfcSnicm #endif /* _NC_WINDOWS */
152*c7ef0cfcSnicm
153*c7ef0cfcSnicm #endif /* USE_WIDEC_SUPPORT */
154