foam to Tecplot360 converter
[OpenFOAM-1.6.x.git] / applications / utilities / postProcessing / dataConversion / foamToTecplot360 / tecio / tecsrc / q_unicode.cpp
blob4d4db61e1ddb9fd3fec146882ef268a9b4973ea1
1 /*
2 * NOTICE and LICENSE for Tecplot Input/Output Library (TecIO) - OpenFOAM
4 * Copyright (C) 1988-2009 Tecplot, Inc. All rights reserved worldwide.
6 * Tecplot hereby grants OpenCFD limited authority to distribute without
7 * alteration the source code to the Tecplot Input/Output library, known
8 * as TecIO, as part of its distribution of OpenFOAM and the
9 * OpenFOAM_to_Tecplot converter. Users of this converter are also hereby
10 * granted access to the TecIO source code, and may redistribute it for the
11 * purpose of maintaining the converter. However, no authority is granted
12 * to alter the TecIO source code in any form or manner.
14 * This limited grant of distribution does not supersede Tecplot, Inc.'s
15 * copyright in TecIO. Contact Tecplot, Inc. for further information.
17 * Tecplot, Inc.
18 * 3535 Factoria Blvd, Ste. 550
19 * Bellevue, WA 98006, USA
20 * Phone: +1 425 653 1200
21 * http://www.tecplot.com/
24 #include "stdafx.h"
25 #include "MASTER.h"
27 #define TECPLOTENGINEMODULE
30 ******************************************************************
31 ******************************************************************
32 ******* ********
33 ****** (C) 1988-2008 Tecplot, Inc. *******
34 ******* ********
35 ******************************************************************
36 ******************************************************************
38 #define Q_UNICODEMODULE
40 #include "GLOBAL.h"
41 #include "TASSERT.h"
43 #if !defined TECPLOTKERNEL
44 #include "TranslatedString.h"
45 #endif
48 #if defined TECPLOTKERNEL
49 /* CORE SOURCE CODE REMOVED */
50 #endif
52 #include "ALLOC.h"
54 #include "Q_UNICODE.h"
56 using namespace std;
58 namespace tecplot
60 namespace strutil
63 typedef std::map<std::string, char *> EnvStringPoolMap_t;
64 static EnvStringPoolMap_t mapEnvStringPool;
67 #if defined MSWIN
70 string WStringToString(wstring str)
72 REQUIRE("str is any wide string");
73 string Result = WideCharToUtf8(str.c_str());
75 ENSURE("Result is any string");
76 return Result;
79 wstring StringToWString(string str)
81 REQUIRE("str is any string");
83 wstring Result = Utf8ToWideChar(str.c_str());
85 ENSURE("Result is any string");
86 return Result;
88 #endif
90 /************************************************
91 * Utf8Api
92 ************************************************/
93 #define VALID_CODE_PAGE(cp) \
94 ( (cp) == 932 || (cp) == CP_UTF8 || (cp) == CP_ACP || (cp) == CP_OEMCP || (cp) == CP_THREAD_ACP )
97 #if defined TECPLOTKERNEL
98 /* CORE SOURCE CODE REMOVED */
99 #if defined MSWIN && !defined ENGINE
100 #endif
101 #if defined MSWIN
102 #endif
103 #endif
106 #if defined MSWIN
108 #if defined TECPLOTKERNEL
109 /* CORE SOURCE CODE REMOVED */
110 #endif /* TECPLOTKERNEL */
112 std::string WideCharToMultiByte(const wchar_t* WideString,
113 unsigned int CodePage)
115 REQUIRE(VALID_REF(WideString));
116 REQUIRE(VALID_CODE_PAGE(CodePage));
118 string strResult;
119 wstring wString(WideString);
122 if (wString.length() > 0)
124 size_t nLen =
125 (size_t) ::WideCharToMultiByte(CodePage,
127 wString.c_str(),
129 NULL,
131 NULL,
132 NULL);
133 if (nLen > 0)
135 char *pBuffer = ALLOC_ARRAY(nLen, char, "pBuffer");
137 VERIFY(::WideCharToMultiByte(CodePage,
139 WideString,
140 (int)(wString.length() + 1),
141 pBuffer,
142 (int)nLen,
143 NULL,
144 NULL) != 0);
146 strResult = pBuffer;
147 FREE_ARRAY(pBuffer, "pBuffer");
150 else
152 // this should never be an error
153 CHECK(FALSE);
156 else
158 // output 'str' remains empty
161 ENSURE("strResult is a valid STL string");
162 return strResult;
167 wstring MultiByteToWideChar(const char *UTF8String,
168 unsigned int CodePage)
170 REQUIRE(VALID_REF(UTF8String));
171 REQUIRE(VALID_CODE_PAGE(CodePage));
173 wstring strResult;
174 string UTF8str(UTF8String);
176 size_t wLen;
178 if (UTF8str.length() > 0)
180 wLen =
181 (size_t) ::MultiByteToWideChar(CodePage,
183 UTF8str.c_str(),
185 NULL,
187 if (wLen > 0)
189 wchar_t *wBuffer = ALLOC_ARRAY(wLen + 1, wchar_t, "wBuffer");
190 VERIFY(::MultiByteToWideChar(CodePage,
192 UTF8str.c_str(),
193 (int)(UTF8str.length() + 1),
194 wBuffer,
195 (int)wLen) != 0);
197 strResult = wBuffer;
198 FREE_ARRAY(wBuffer, "wBuffer");
201 else
203 CHECK(FALSE); // We should never get an error here
206 else
208 // strResult is left empty
211 ENSURE("strResult is a valid CString");
213 wstring strRet(strResult);
214 return strRet;
217 #endif
221 #if defined MSWIN
222 std::string WideCharToUtf8(const wchar_t *str)
224 REQUIRE(VALID_REF(str)); /* really cannot be NULL - 2007-10-22 CAM/DTO */
226 #if defined TECPLOTKERNEL
227 /* CORE SOURCE CODE REMOVED */
228 #endif
230 UINT CodePage = CP_ACP;
232 string Result = "";
234 #if defined TECPLOTKERNEL
235 /* CORE SOURCE CODE REMOVED */
236 #endif
238 Result = WideCharToMultiByte(str, CodePage);
240 ENSURE("Result is any string");
241 return Result;
244 wstring Utf8ToWideChar(const char *str)
246 REQUIRE(VALID_REF(str)); /* really cannot be NULL - 2007-10-22 CAM/DTO */
248 #if defined TECPLOTKERNEL
249 /* CORE SOURCE CODE REMOVED */
250 #endif
252 UINT CodePage = CP_ACP;
253 wstring Result;
255 #if defined TECPLOTKERNEL
256 /* CORE SOURCE CODE REMOVED */
257 #endif
259 Result = MultiByteToWideChar(str, CodePage);
261 ENSURE("Result is any string");
262 return Result;
264 #endif
267 Boolean_t IsValidUtf8LeadByte(Byte_t uch)
269 REQUIRE("uch is any byte");
270 Boolean_t Result =
271 uch <= 0x7F ||
272 (uch >= 0xC0 && uch <= 0xDF) ||
273 (uch >= 0xE0 && uch <= 0xEF) ||
274 (uch >= 0xF0 && uch <= 0xF4);
276 ENSURE(VALID_BOOLEAN(Result));
277 return Result;
280 Boolean_t IsValidUtf8ContinuingByte(Byte_t uch)
282 REQUIRE("uch is any char");
284 Boolean_t Result =
285 (uch >= 0x80 && uch <= 0xBF);
287 ENSURE(VALID_BOOLEAN(Result));
288 return Result;
291 Boolean_t IsValidUtf8Byte(Byte_t uch)
293 REQUIRE("uch is any char");
294 Boolean_t Result =
295 IsValidUtf8LeadByte(uch) ||
296 IsValidUtf8ContinuingByte(uch);
298 REQUIRE(VALID_BOOLEAN(Result));
299 return Result;
303 Boolean_t ShouldConvertWideStringToUtf8String(const wchar_t *str)
305 Boolean_t Result = FALSE;
307 #if defined MSWIN && defined TECPLOTKERNEL
308 /* CORE SOURCE CODE REMOVED */
309 #endif /* MSWIN and TECPLOTKERNEL */
311 ENSURE(VALID_BOOLEAN(Result));
312 return Result;
316 Boolean_t IsValidUtf8String(const char *str)
318 Boolean_t IsValid = TRUE;
319 REQUIRE(VALID_REF(str));
321 #if defined MSWIN
322 size_t len = strlen(str);
323 Boolean_t InUtf8Sequence = FALSE;
324 int Utf8SequenceCount = 0;
325 int MaxUtf8SequenceCount = 0;
327 /* we want to process the final \0, so go to <= len */
329 for (size_t ii = 0; IsValid && ii <= len; ii++)
331 Byte_t uch = (Byte_t)str[ii];
333 if (uch <= 0x7F)
335 /* This must be the end of a sequence,
336 so the sequence count must match
337 the max sequence count */
339 InUtf8Sequence = FALSE;
340 IsValid = (Utf8SequenceCount == MaxUtf8SequenceCount);
341 Utf8SequenceCount = 0;
342 MaxUtf8SequenceCount = 0;
344 else if (uch >= 0x80 && uch <= 0xBF)
346 /* Continuing byte in a multi byte sequence */
347 if (InUtf8Sequence)
349 Utf8SequenceCount++;
351 else
353 IsValid = FALSE;
357 else if (uch >= 0xC0 && uch <= 0xDF)
359 /* Lead byte of 000080-0007FF */
360 IsValid = (Utf8SequenceCount == MaxUtf8SequenceCount);
361 InUtf8Sequence = TRUE;
362 Utf8SequenceCount = 0;
363 MaxUtf8SequenceCount = 1;
365 else if (uch >= 0xE0 && uch <= 0xEF)
367 /* Lead byte of 000800-00FFFF */
368 IsValid = (Utf8SequenceCount == MaxUtf8SequenceCount);
369 InUtf8Sequence = TRUE;
370 Utf8SequenceCount = 0;
371 MaxUtf8SequenceCount = 2;
373 else if (uch >= 0xF0 && uch <= 0xF4)
375 /* Lead byte of 010000-10FFFF */
376 IsValid = (Utf8SequenceCount == MaxUtf8SequenceCount);
377 Utf8SequenceCount = 0;
378 InUtf8Sequence = TRUE;
379 MaxUtf8SequenceCount = 3;
382 else
384 /* Invalid Utf 8 */
385 IsValid = FALSE;
388 #endif
390 ENSURE(VALID_BOOLEAN(IsValid));
391 return IsValid;
397 Boolean_t IsNullOrZeroLengthString(const char *str)
399 REQUIRE(VALID_REF_OR_NULL(str));
401 Boolean_t Result = (str == NULL || strlen(str) == 0);
403 ENSURE(VALID_BOOLEAN(Result));
404 return Result;
409 Boolean_t IsNullOrZeroLengthString(TranslatedString TS)
411 REQUIRE(TS.isValid());
412 return TS.isNullOrZeroLength();
419 #if defined MSWIN && TECPLOTKERNEL && (!defined NO_ASSERTS || defined CHECKED_BUILD)
420 /* Keeping Trace out of the release builds
421 will verify for us that it has been optimized away.
423 See the definition of TRACE in MASTER.h for
424 more information... */
425 void MSWinTrace(const char *Format, ...)
427 REQUIRE(VALID_REF(Format));
429 const int BufferSize = 512; /* Only print the first 512 characers */
430 va_list Arguments;
432 /* Don't use ALLOC_ARRAY here */
433 char *buffer = new char[BufferSize];
434 memset(buffer, 0, BufferSize);
436 va_start(Arguments, Format);
437 _vsnprintf(buffer, BufferSize - 1, Format, Arguments);
438 va_end(Arguments);
440 ::OutputDebugStringA(buffer);
442 delete [] buffer;
447 #endif