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.
18 * 3535 Factoria Blvd, Ste. 550
19 * Bellevue, WA 98006, USA
20 * Phone: +1 425 653 1200
21 * http://www.tecplot.com/
27 #define TECPLOTENGINEMODULE
30 ******************************************************************
31 ******************************************************************
33 ****** (C) 1988-2008 Tecplot, Inc. *******
35 ******************************************************************
36 ******************************************************************
38 #define Q_UNICODEMODULE
43 #if !defined TECPLOTKERNEL
44 #include "TranslatedString.h"
48 #if defined TECPLOTKERNEL
49 /* CORE SOURCE CODE REMOVED */
54 #include "Q_UNICODE.h"
63 typedef std::map
<std::string
, char *> EnvStringPoolMap_t
;
64 static EnvStringPoolMap_t mapEnvStringPool
;
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");
79 wstring
StringToWString(string str
)
81 REQUIRE("str is any string");
83 wstring Result
= Utf8ToWideChar(str
.c_str());
85 ENSURE("Result is any string");
90 /************************************************
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
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
));
119 wstring
wString(WideString
);
122 if (wString
.length() > 0)
125 (size_t) ::WideCharToMultiByte(CodePage
,
135 char *pBuffer
= ALLOC_ARRAY(nLen
, char, "pBuffer");
137 VERIFY(::WideCharToMultiByte(CodePage
,
140 (int)(wString
.length() + 1),
147 FREE_ARRAY(pBuffer
, "pBuffer");
152 // this should never be an error
158 // output 'str' remains empty
161 ENSURE("strResult is a valid STL string");
167 wstring
MultiByteToWideChar(const char *UTF8String
,
168 unsigned int CodePage
)
170 REQUIRE(VALID_REF(UTF8String
));
171 REQUIRE(VALID_CODE_PAGE(CodePage
));
174 string
UTF8str(UTF8String
);
178 if (UTF8str
.length() > 0)
181 (size_t) ::MultiByteToWideChar(CodePage
,
189 wchar_t *wBuffer
= ALLOC_ARRAY(wLen
+ 1, wchar_t, "wBuffer");
190 VERIFY(::MultiByteToWideChar(CodePage
,
193 (int)(UTF8str
.length() + 1),
198 FREE_ARRAY(wBuffer
, "wBuffer");
203 CHECK(FALSE
); // We should never get an error here
208 // strResult is left empty
211 ENSURE("strResult is a valid CString");
213 wstring
strRet(strResult
);
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 */
230 UINT CodePage
= CP_ACP
;
234 #if defined TECPLOTKERNEL
235 /* CORE SOURCE CODE REMOVED */
238 Result
= WideCharToMultiByte(str
, CodePage
);
240 ENSURE("Result is any string");
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 */
252 UINT CodePage
= CP_ACP
;
255 #if defined TECPLOTKERNEL
256 /* CORE SOURCE CODE REMOVED */
259 Result
= MultiByteToWideChar(str
, CodePage
);
261 ENSURE("Result is any string");
267 Boolean_t
IsValidUtf8LeadByte(Byte_t uch
)
269 REQUIRE("uch is any byte");
272 (uch
>= 0xC0 && uch
<= 0xDF) ||
273 (uch
>= 0xE0 && uch
<= 0xEF) ||
274 (uch
>= 0xF0 && uch
<= 0xF4);
276 ENSURE(VALID_BOOLEAN(Result
));
280 Boolean_t
IsValidUtf8ContinuingByte(Byte_t uch
)
282 REQUIRE("uch is any char");
285 (uch
>= 0x80 && uch
<= 0xBF);
287 ENSURE(VALID_BOOLEAN(Result
));
291 Boolean_t
IsValidUtf8Byte(Byte_t uch
)
293 REQUIRE("uch is any char");
295 IsValidUtf8LeadByte(uch
) ||
296 IsValidUtf8ContinuingByte(uch
);
298 REQUIRE(VALID_BOOLEAN(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
));
316 Boolean_t
IsValidUtf8String(const char *str
)
318 Boolean_t IsValid
= TRUE
;
319 REQUIRE(VALID_REF(str
));
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
];
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 */
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;
390 ENSURE(VALID_BOOLEAN(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
));
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 */
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
);
440 ::OutputDebugStringA(buffer
);