libncurses: updated to 6.0
[tomato.git] / release / src / router / libncurses / ncurses / widechar / widechars.c
blob6951e2863fa018b70401803983717befee1d033f
1 /****************************************************************************
2 * Copyright (c) 2012,2013 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 ****************************************************************************/
29 #include <curses.priv.h>
31 #if USE_WIDEC_SUPPORT
33 MODULE_ID("$Id: widechars.c,v 1.5 2013/03/02 18:55:51 tom Exp $")
35 #if defined(__MINGW32__)
37 * MinGW has wide-character functions, but they do not work correctly.
40 int
41 _nc_mbtowc(wchar_t *pwc, const char *s, size_t n)
43 int result;
44 int count;
45 int try;
47 if (s != 0 && n != 0) {
49 * MultiByteToWideChar() can decide to return more than one
50 * wide-character. We want only one. Ignore any trailing null, both
51 * in the initial count and in the conversion.
53 count = 0;
54 for (try = 1; try <= (int) n; ++try) {
55 count = MultiByteToWideChar(CP_UTF8,
56 MB_ERR_INVALID_CHARS,
58 try,
59 pwc,
60 0);
61 TR(TRACE_BITS, ("...try %d:%d", try, count));
62 if (count > 0) {
63 break;
66 if (count < 1 || count > 2) {
67 result = -1;
68 } else {
69 wchar_t actual[2];
70 memset(&actual, 0, sizeof(actual));
71 count = MultiByteToWideChar(CP_UTF8,
72 MB_ERR_INVALID_CHARS,
74 try,
75 actual,
76 2);
77 TR(TRACE_BITS, ("\twin32 ->%#x, %#x", actual[0], actual[1]));
78 *pwc = actual[0];
79 if (actual[1] != 0)
80 result = -1;
81 else
82 result = try;
84 } else {
85 result = 0;
88 return result;
91 int
92 _nc_mblen(const char *s, size_t n)
94 int result = -1;
95 int count;
96 wchar_t temp;
98 if (s != 0 && n != 0) {
99 count = _nc_mbtowc(&temp, s, n);
100 if (count == 1) {
101 int check = WideCharToMultiByte(CP_UTF8,
103 &temp,
105 NULL,
106 0, /* compute length only */
107 NULL,
108 NULL);
109 TR(TRACE_BITS, ("\tcheck ->%d\n", check));
110 if (check > 0 && (size_t) check <= n) {
111 result = check;
114 } else {
115 result = 0;
118 return result;
121 int __MINGW_NOTHROW
122 _nc_wctomb(char *s, wchar_t wc)
124 int result;
125 int check;
127 check = WideCharToMultiByte(CP_UTF8,
129 &wc,
131 NULL,
132 0, /* compute length only */
133 NULL,
134 NULL);
135 if (check > 0) {
136 result = WideCharToMultiByte(CP_UTF8,
138 &wc,
141 check + 1,
142 NULL,
143 NULL);
144 } else {
145 result = -1;
147 return result;
150 #endif /* __MINGW32__ */
152 #endif /* USE_WIDEC_SUPPORT */