Forward compatibility: flex
[foam-extend-3.2.git] / applications / utilities / postProcessing / dataConversion / foamToTecplot360 / tecio / tecsrc / TranslatedString.cpp
blob5077bf2c9d80c04afe60f557c16ee003096523d9
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"
26 #define TECPLOTENGINEMODULE
28 *****************************************************************
29 *****************************************************************
30 ******* ********
31 ****** Copyright (C) 1988-2008 Tecplot, Inc. *******
32 ******* ********
33 *****************************************************************
34 *****************************************************************
37 #include "GLOBAL.h"
38 #include "TASSERT.h"
39 #include "Q_UNICODE.h"
42 using namespace std;
44 namespace tecplot
46 namespace strutil
49 #if defined MSWIN && !defined TECPLOTKERNEL
50 /**
51 * Stub function for non-TECPLOTKERNEL
53 string LookUpTranslation(string& str)
55 return string(str);
57 #endif
59 /**
60 * Convenience function for creating Utf8 string translations.
62 * @param str
63 * String to translate.
65 * @return
66 * A new Utf8 translated string.
68 static inline string* createUtf8StringTranslation(string& str)
70 #if defined MSWIN
71 string *result = new string(LookUpTranslation(str));
72 #else
73 string *result = new string(str);
74 #endif
75 ENSURE(VALID_REF(result));
76 return result;
79 #if defined MSWIN
80 /**
81 * Convenience function for creating wide string translations.
83 * @param str
84 * String to translate.
86 * @return
87 * A new wide translated string.
89 static inline wstring* createWideStringTranslation(string& str)
91 wstring *result = new wstring;
92 *result = StringToWString(LookUpTranslation(str));
94 ENSURE(VALID_REF(result));
95 return result;
97 #endif
99 #if defined MSWIN
101 * Convenience function for creating wide string with the given mode.
103 * @param mode
104 * Indicates if this string is to be translated or not.
105 * @param str
106 * String to translate.
108 * @return
109 * A new wide translated string.
111 static inline wstring* createWideString(TranslatedString::Mode mode,
112 string& str)
114 REQUIRE(mode == TranslatedString::DoTranslate || mode == TranslatedString::DontTranslate);
116 wstring* result;
117 if (mode == TranslatedString::DoTranslate)
118 result = createWideStringTranslation(str);
119 else
120 result = new wstring(StringToWString(str));
122 return result;
124 #endif
128 void TranslatedString::init(TranslatedString::Mode mode,
129 const char* str,
130 const char* translatorNotes)
132 REQUIRE(mode == DoTranslate || mode == DontTranslate);
133 REQUIRE(VALID_REF_OR_NULL(str));
134 REQUIRE(VALID_REF_OR_NULL(translatorNotes));
136 m_mode = mode;
137 m_isNull = (str == NULL);
138 if (!m_isNull)
139 m_string = str;
140 m_utf8String = NULL; // ...on demand resource
141 #if defined MSWIN
142 m_wideString = NULL; // ...on demand resource
143 #endif
148 TranslatedString::TranslatedString()
150 init(DontTranslate, (const char*)NULL, (const char*)NULL);
151 ENSURE(this->isValid());
156 TranslatedString TranslatedString::null()
158 return dontTranslate(NULL);
163 TranslatedString::TranslatedString(TranslatedString::Mode mode,
164 const char* str,
165 const char* translatorNotes)
168 REQUIRE(mode == DoTranslate || mode == DontTranslate);
169 REQUIRE(VALID_REF_OR_NULL(str));
170 REQUIRE(VALID_REF_OR_NULL(translatorNotes));
172 init(mode, str, translatorNotes);
173 ENSURE(this->isValid());
178 TranslatedString::~TranslatedString()
180 delete m_utf8String;
181 #if defined MSWIN
182 delete m_wideString;
183 #endif
186 #if !defined NO_ASSERTS
189 bool TranslatedString::isValid() const
191 CHECK(IMPLICATION(m_isNull, m_string.length() == 0));
192 #if 0
193 /* TODO(DTO/RMS/CAM): 11/2/2007
194 * Code currently exists in Tecplot where we call translate() on a
195 * variable. This seems wrong and at times (PleaseWait() in
196 * particular) the variable passed is a NULL pointer which causes
197 * this assertion to fail. There is not enough time before v11.2
198 * release to remove all translate() calls to non-literal strings so
199 * we'll have to do this as a post release cleanup. For now just
200 * deactivate this assertion.
202 CHECK(IMPLICATION(m_isNull, m_mode == DontTranslate));
203 #endif
205 return true;
207 #endif
211 bool TranslatedString::isNull() const
213 INVARIANT(this->isValid());
214 return m_isNull;
219 bool TranslatedString::isNullOrZeroLength() const
221 INVARIANT(this->isValid());
222 return m_isNull || m_string.length() == 0;
227 const char* TranslatedString::c_str()
229 INVARIANT(this->isValid());
231 const char* result = NULL;
232 if (!isNull())
234 if (m_mode == DoTranslate)
236 if (m_utf8String == NULL)
237 m_utf8String = createUtf8StringTranslation(m_string);
238 result = m_utf8String->c_str();
240 else // ...if we aren't translating don't bother creating another Utf8 copy just use m_string
241 result = m_string.c_str();
244 ENSURE(result == NULL || VALID_REF(result));
245 return result;
248 #if defined MSWIN
251 const wchar_t *TranslatedString::c_wstr()
253 INVARIANT(this->isValid());
255 const wchar_t *result = NULL;
256 if (!isNull())
258 if (m_wideString == NULL)
259 m_wideString = createWideString(m_mode, m_string);
260 result = m_wideString->c_str();
263 ENSURE(result == NULL || VALID_REF(result));
264 return result;
266 #endif
270 TranslatedString::operator string()
272 INVARIANT(this->isValid());
273 REQUIRE(!isNull());
275 string* result;
276 if (m_mode == DoTranslate)
278 if (m_utf8String == NULL)
279 m_utf8String = createUtf8StringTranslation(m_string);
280 result = m_utf8String;
282 else // ...if we aren't translating don't bother creating another Utf8 copy just use m_string
283 result = &m_string;
285 return *result;
288 #if defined MSWIN
291 TranslatedString::operator wstring()
293 INVARIANT(this->isValid());
294 REQUIRE(!isNull());
296 if (m_wideString == NULL)
297 m_wideString = createWideString(m_mode, m_string);
299 return *m_wideString;
301 #endif
305 TranslatedString& TranslatedString::operator =(const TranslatedString& other)
307 REQUIRE(other.isValid());
309 if (this != &other) // ...only perform if not self assignment
311 m_mode = other.m_mode;
312 m_isNull = other.m_isNull;
313 m_string = other.m_string;
314 m_utf8String = (other.m_utf8String != NULL ? new string(*other.m_utf8String) : NULL);
315 #if defined MSWIN
316 m_wideString = (other.m_wideString != NULL ? new wstring(*other.m_wideString) : NULL);
317 #endif
320 ENSURE(this->isValid());
321 return *this;
326 TranslatedString::TranslatedString(const TranslatedString& other)
328 REQUIRE(other.isValid());
330 m_mode = other.m_mode;
331 m_isNull = other.m_isNull;
332 m_string = other.m_string;
333 m_utf8String = (other.m_utf8String != NULL ? new string(*other.m_utf8String) : NULL);
334 #if defined MSWIN
335 m_wideString = (other.m_wideString != NULL ? new wstring(*other.m_wideString) : NULL);
336 #endif
338 ENSURE(this->isValid());
343 TranslatedString TranslatedString::translate(const char* str,
344 const char* translatorNotes)
346 REQUIRE(VALID_REF_OR_NULL(str));
347 REQUIRE(VALID_REF_OR_NULL(translatorNotes));
349 return TranslatedString(DoTranslate, str, translatorNotes);
354 TranslatedString TranslatedString::dontTranslate(const char* str)
356 REQUIRE(VALID_REF_OR_NULL(str));
358 return TranslatedString(DontTranslate, str, NULL);