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  ****************************************************************************/
28 
29 #include <curses.priv.h>
30 
31 #if USE_WIDEC_SUPPORT
32 
33 MODULE_ID("$Id: widechars.c,v 1.5 2013/03/02 18:55:51 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
50 	 * wide-character.  We want only one.  Ignore any trailing null, both
51 	 * in the initial count 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 	    memset(&actual, 0, sizeof(actual));
71 	    count = MultiByteToWideChar(CP_UTF8,
72 					MB_ERR_INVALID_CHARS,
73 					s,
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;
83 	}
84     } else {
85 	result = 0;
86     }
87 
88     return result;
89 }
90 
91 int
92 _nc_mblen(const char *s, size_t n)
93 {
94     int result = -1;
95     int count;
96     wchar_t temp;
97 
98     if (s != 0 && n != 0) {
99 	count = _nc_mbtowc(&temp, s, n);
100 	if (count == 1) {
101 	    int check = WideCharToMultiByte(CP_UTF8,
102 					    0,
103 					    &temp,
104 					    1,
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;
112 	    }
113 	}
114     } else {
115 	result = 0;
116     }
117 
118     return result;
119 }
120 
121 int __MINGW_NOTHROW
122 _nc_wctomb(char *s, wchar_t wc)
123 {
124     int result;
125     int check;
126 
127     check = WideCharToMultiByte(CP_UTF8,
128 				0,
129 				&wc,
130 				1,
131 				NULL,
132 				0,	/* compute length only */
133 				NULL,
134 				NULL);
135     if (check > 0) {
136 	result = WideCharToMultiByte(CP_UTF8,
137 				     0,
138 				     &wc,
139 				     1,
140 				     s,
141 				     check + 1,
142 				     NULL,
143 				     NULL);
144     } else {
145 	result = -1;
146     }
147     return result;
148 }
149 
150 #endif /* __MINGW32__ */
151 
152 #endif /* USE_WIDEC_SUPPORT */
153