]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/widechar/widechars.c
c86b8a0660597fa037edbc4088700e8fd312f387
[ncurses.git] / ncurses / widechar / widechars.c
1 /****************************************************************************
2  * Copyright (c) 2012 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 #include <curses.priv.h>
30
31 #if USE_WIDEC_SUPPORT
32
33 MODULE_ID("$Id: widechars.c,v 1.4 2012/12/02 01:50:59 tom Exp $")
34
35 #if defined(__MINGW32__)
36 /*
37  * MinGW has wide-character functions, but they do not work correctly.
38  */
39
40 int
41 _nc_mbtowc(wchar_t *pwc, const char *s, size_t n)
42 {
43     int result;
44     int count;
45     int try;
46
47     if (s != 0 && n != 0) {
48         /*
49          * MultiByteToWideChar() can decide to return more than one wide-character.
50          * We want only one. Ignore any trailing null, both in the initial count
51          * and in the conversion.
52          */
53         count = 0;
54         for (try = 1; try <= (int) n; ++try) {
55             count = MultiByteToWideChar(CP_UTF8,
56                                         MB_ERR_INVALID_CHARS,
57                                         s,
58                                         try,
59                                         pwc,
60                                         0);
61             TR(TRACE_BITS, ("...try %d:%d", try, count));
62             if (count > 0) {
63                 break;
64             }
65         }
66         if (count < 1 || count > 2) {
67             result = -1;
68         } else {
69             wchar_t actual[2];
70             count = MultiByteToWideChar(CP_UTF8,
71                                         MB_ERR_INVALID_CHARS,
72                                         s,
73                                         try,
74                                         actual,
75                                         2);
76             TR(TRACE_BITS, ("\twin32 ->%#x, %#x", actual[0], actual[1]));
77             *pwc = actual[0];
78             if (actual[1] != 0)
79                 result = -1;
80             else
81                 result = try;
82         }
83     } else {
84         result = 0;
85     }
86
87     return result;
88 }
89
90 int
91 _nc_mblen(const char *s, size_t n)
92 {
93     int result = -1;
94     int count;
95     wchar_t temp;
96
97     if (s != 0 && n != 0) {
98         count = _nc_mbtowc(&temp, s, n);
99         if (count == 1) {
100             int check = WideCharToMultiByte(CP_UTF8,
101                                             0,
102                                             &temp,
103                                             1,
104                                             NULL,
105                                             0,  /* compute length only */
106                                             NULL,
107                                             NULL);
108             TR(TRACE_BITS, ("\tcheck ->%d\n", check));
109             if (check > 0 && (size_t) check <= n) {
110                 result = check;
111             }
112         }
113     } else {
114         result = 0;
115     }
116
117     return result;
118 }
119
120 int __MINGW_NOTHROW
121 _nc_wctomb(char *s, wchar_t wc)
122 {
123     int result;
124     int check;
125
126     check = WideCharToMultiByte(CP_UTF8,
127                                 0,
128                                 &wc,
129                                 1,
130                                 NULL,
131                                 0,      /* compute length only */
132                                 NULL,
133                                 NULL);
134     if (check > 0) {
135         result = WideCharToMultiByte(CP_UTF8,
136                                      0,
137                                      &wc,
138                                      1,
139                                      s,
140                                      check + 1,
141                                      NULL,
142                                      NULL);
143     } else {
144         result = -1;
145     }
146     return result;
147 }
148
149 #endif /* __MINGW32__ */
150
151 #endif /* USE_WIDEC_SUPPORT */