1 /**
2 * @file local.cpp
3 * @brief Implementation of the local class.
4 * @author DEVISER
5 *
6 * <!--------------------------------------------------------------------------
7 * This file is part of libSEDML. Please visit http://sed-ml.org for more
8 * information about SED-ML. The latest version of libSEDML can be found on
9 * github: https://github.com/fbergmann/libSEDML/
10 *
11
12 * Copyright (c) 2013-2019, Frank T. Bergmann
13 * All rights reserved.
14 *
15
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are met:
18 *
19
20 * 1. Redistributions of source code must retain the above copyright notice,
21 * this
22 * list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright notice,
24 * this list of conditions and the following disclaimer in the documentation
25 * and/or other materials provided with the distribution.
26 *
27 * This library is free software; you can redistribute it and/or modify it
28 * under the terms of the GNU Lesser General Public License as published by the
29 * Free Software Foundation. A copy of the license agreement is provided in the
30 * file named "LICENSE.txt" included with this software distribution and also
31 * available online as http://sbml.org/software/libsbml/license.html
32 * ------------------------------------------------------------------------ -->
33 */
34
35
36
37
38 //--------------------------------------------------------------------------------
39 // (Currently (2008-07-25), this file is used only for Windows)
40 //
41 // Utility functions for converting encodings between Unicode (wide char),
42 // UTF8 and ANSI CP (multibyte char)
43 //
44 // 1) char* convertUnicodeToUTF8(const wchar_t* src_wstr)
45 // 2) char* convertUnicodeToACP(const wchar_t* src_wstr)
46 // 3) wchar_t* convertUTF8ToUnicode(const char* src_str)
47 // 4) char* convertACPToUTF8(const char* src_str)
48 // 5) char* convertUTF8ToACP(const char* src_str)
49 //
50 //--------------------------------------------------------------------------------
51
52 #ifdef WIN32
53
54 // do not include MFC
55 #define WIN32_LEAN_AND_MEAN
56 // do not include GDI by default
57 #define NOGDI
58
59 #include <windows.h>
60 #include <winnls.h>
61
62 //
63 // convert Unicode -> UTF8 (for Windows)
64 //
convertUnicodeToUTF8(const wchar_t * src_wstr)65 char* convertUnicodeToUTF8(const wchar_t* src_wstr)
66 {
67 int length;
68 int clength;
69 char* sbuf;
70
71 //
72 // Unicode -> UTF8
73 //
74
75 //
76 // Check wbuf length
77 //
78 length = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)src_wstr, -1, NULL, 0, NULL, NULL);
79
80 if(length == 0){
81 return NULL;
82 }
83
84 sbuf = new char[length+1];
85
86 //
87 // Convert
88 //
89 clength = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)src_wstr, -1, sbuf, length, NULL, NULL);
90 sbuf[clength] = 0;
91
92 if(clength == 0){
93 delete [] sbuf;
94 return NULL;
95 }
96
97 return sbuf;
98 }
99
100
101 //
102 // convert Unicode -> ANSI CP (for Windows)
103 //
convertUnicodeToACP(const wchar_t * src_wstr)104 char* convertUnicodeToACP(const wchar_t* src_wstr)
105 {
106 int length;
107 int clength;
108 char* sbuf;
109
110 //
111 // Unicode -> ANSI CP
112 //
113
114 //
115 // Check wbuf length
116 //
117 length = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src_wstr, -1, NULL, 0, NULL, NULL);
118
119 if(length == 0){
120 return NULL;
121 }
122
123 sbuf = new char[length+1];
124
125 //
126 // Convert
127 //
128 clength = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src_wstr, -1, sbuf, length, NULL, NULL);
129 sbuf[clength] = 0;
130
131 if(clength == 0){
132 delete [] sbuf;
133 return NULL;
134 }
135
136 return sbuf;
137 }
138
139
140 //
141 // convert UTF8 -> Unicode (for Windows)
142 //
convertUTF8ToUnicode(const char * src_str)143 wchar_t* convertUTF8ToUnicode(const char* src_str)
144 {
145 int length;
146 int c_length;
147 wchar_t* wbuf;
148
149 //
150 // UTF8 -> Unicode
151 //
152
153 // Check src_str length
154 length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)src_str, -1, NULL, 0);
155 if(length == 0){
156 return NULL;
157 }
158
159 wbuf = new wchar_t[length+1];
160
161 // Convert
162 c_length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)src_str, -1, wbuf, length);
163 wbuf[c_length] = 0;
164 if(c_length == 0) {
165 delete [] wbuf;
166 return NULL;
167 }
168
169 return wbuf;
170
171 }
172
173
174 //
175 // convert ANSI CP -> UTF8 for Windows
176 //
convertACPToUTF8(const char * src_str)177 char* convertACPToUTF8(const char* src_str)
178 {
179 int length;
180 int c_length;
181 wchar_t* wbuf;
182 char* ubuf;
183
184 //
185 // ANSI CP -> Unicode
186 //
187
188 // Check src_str length
189 length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src_str, -1, NULL, 0);
190 if(length == 0){
191 return NULL;
192 }
193
194 wbuf = new wchar_t[length+1];
195
196 // Convert
197 c_length = MultiByteToWideChar(CP_ACP, 0,(LPCSTR)src_str,-1,wbuf,length);
198 wbuf[c_length] = 0;
199
200 if(c_length == 0) {
201 delete [] wbuf;
202 return NULL;
203 }
204
205 //
206 // Unicode -> UTF8
207 //
208
209 // Check wbuf length
210 length = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wbuf,-1,NULL,0,NULL,NULL);
211
212 if(length == 0){
213 delete [] wbuf;
214 return NULL;
215 }
216
217 ubuf = new char[length+1];
218
219 // Convert
220 c_length = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wbuf,-1,ubuf,length,NULL,NULL);
221 ubuf[c_length] = 0;
222
223 if(c_length == 0){
224 delete [] wbuf;
225 delete [] ubuf;
226 return NULL;
227 }
228
229 delete [] wbuf;
230 return ubuf;
231
232 }
233
234
235 //
236 // convert UTF8 -> ANSI CP for Windows
237 //
convertUTF8ToACP(const char * src_str)238 char* convertUTF8ToACP(const char* src_str)
239 {
240 int length;
241 int c_length;
242 wchar_t* wbuf;
243 char* ubuf;
244
245 //
246 // UTF8 -> Unicode
247 //
248
249 // Check src_str length
250 length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)src_str, -1, NULL, 0);
251 if(length == 0){
252 return NULL;
253 }
254
255 wbuf = new wchar_t[length+1];
256
257 // Convert
258 c_length = MultiByteToWideChar(CP_UTF8, 0,(LPCSTR)src_str,-1,wbuf,length);
259 wbuf[c_length] = 0;
260 if(c_length == 0) {
261 delete [] wbuf;
262 return NULL;
263 }
264
265 //
266 // Unicode -> ANSI CP
267 //
268
269 // Check wbuf length
270 length = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wbuf,-1,NULL,0,NULL,NULL);
271
272 if(length == 0){
273 delete [] wbuf;
274 return NULL;
275 }
276
277 ubuf = new char[length+1];
278
279 // Convert
280 c_length = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wbuf,-1,ubuf,length,NULL,NULL);
281 ubuf[c_length] = 0;
282
283 if(c_length == 0){
284 delete [] wbuf;
285 delete [] ubuf;
286 return NULL;
287 }
288
289 delete [] wbuf;
290 return ubuf;
291
292 }
293
294 #endif // WIN32
295