xref: /aoo41x/main/tools/source/string/strcvt.cxx (revision 89b56da7)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // no include "precompiled_tools.hxx" because this is included in other cxx files.
25 
26 // -----------------------------------------------------------------------
27 
ImplUpdateStringFromUniString(const sal_Unicode * pUniStr,sal_Size nUniLen,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)28 void ByteString::ImplUpdateStringFromUniString(
29     const sal_Unicode* pUniStr, sal_Size nUniLen,
30     rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
31 {
32 	ByteStringData* pNewStringData = NULL;
33 	rtl_uString2String( (rtl_String **)(&pNewStringData),
34 						pUniStr, nUniLen,
35 						eTextEncoding, nCvtFlags );
36 	STRING_RELEASE((STRING_TYPE *)mpData);
37 	mpData = pNewStringData;
38 }
39 
40 // =======================================================================
41 
ByteString(const UniString & rUniStr,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)42 ByteString::ByteString( const UniString& rUniStr, rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
43 {
44 	DBG_CTOR( ByteString, DbgCheckByteString );
45 	DBG_CHKOBJ( &rUniStr, UniString, DbgCheckUniString );
46 
47 	mpData = NULL;
48 	rtl_uString2String( (rtl_String **)(&mpData),
49 						rUniStr.mpData->maStr, rUniStr.mpData->mnLen,
50 						eTextEncoding, nCvtFlags );
51 }
52 
53 // -----------------------------------------------------------------------
54 
ByteString(const UniString & rUniStr,xub_StrLen nPos,xub_StrLen nLen,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)55 ByteString::ByteString( const UniString& rUniStr, xub_StrLen nPos, xub_StrLen nLen,
56 						rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
57 {
58 	DBG_CTOR( ByteString, DbgCheckByteString );
59 	DBG_CHKOBJ( &rUniStr, UniString, DbgCheckUniString );
60 
61 	// Stringlaenge ermitteln
62 	if ( nPos > rUniStr.mpData->mnLen )
63 		nLen = 0;
64 	else
65 	{
66 		// Laenge korrigieren, wenn noetig
67 		sal_Int32 nMaxLen = rUniStr.mpData->mnLen-nPos;
68 		if ( nLen > nMaxLen )
69 			nLen = static_cast< xub_StrLen >(nMaxLen);
70 	}
71 
72 	mpData = NULL;
73 	rtl_uString2String( (rtl_String **)(&mpData),
74 						rUniStr.mpData->maStr+nPos, nLen,
75 						eTextEncoding, nCvtFlags );
76 }
77 
78 // -----------------------------------------------------------------------
79 
ByteString(const sal_Unicode * pUniStr,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)80 ByteString::ByteString( const sal_Unicode* pUniStr,
81 						rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
82 {
83 	DBG_CTOR( ByteString, DbgCheckByteString );
84 	DBG_ASSERT( pUniStr, "ByteString::ByteString() - pUniStr is NULL" );
85 
86 	mpData = NULL;
87 	rtl_uString2String( (rtl_String **)(&mpData),
88 						pUniStr, ImplStringLen( pUniStr ),
89 						eTextEncoding, nCvtFlags );
90 }
91 
92 // -----------------------------------------------------------------------
93 
ByteString(const sal_Unicode * pUniStr,xub_StrLen nLen,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)94 ByteString::ByteString( const sal_Unicode* pUniStr, xub_StrLen nLen,
95 						rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
96 {
97 	DBG_CTOR( ByteString, DbgCheckByteString );
98 	DBG_ASSERT( pUniStr, "ByteString::ByteString() - pUniStr is NULL" );
99 
100 	if ( nLen == STRING_LEN )
101 		nLen = ImplStringLen( pUniStr );
102 
103 	mpData = NULL;
104 	rtl_uString2String( (rtl_String **)(&mpData),
105 						pUniStr, nLen,
106 						eTextEncoding, nCvtFlags );
107 }
108 
109 // =======================================================================
110 
111 static sal_uChar aImplByteTab[256] =
112 {
113 	0,	 1,   2,   3,	4,	 5,   6,   7,
114 	8,	 9,  10,  11,  12,	13,  14,  15,
115    16,	17,  18,  19,  20,	21,  22,  23,
116    24,	25,  26,  27,  28,	29,  30,  31,
117    32,	33,  34,  35,  36,	37,  38,  39,
118    40,	41,  42,  43,  44,	45,  46,  47,
119    48,	49,  50,  51,  52,	53,  54,  55,
120    56,	57,  58,  59,  60,	61,  62,  63,
121    64,	65,  66,  67,  68,	69,  70,  71,
122    72,	73,  74,  75,  76,	77,  78,  79,
123    80,	81,  82,  83,  84,	85,  86,  87,
124    88,	89,  90,  91,  92,	93,  94,  95,
125    96,	97,  98,  99, 100, 101, 102, 103,
126   104, 105, 106, 107, 108, 109, 110, 111,
127   112, 113, 114, 115, 116, 117, 118, 119,
128   120, 121, 122, 123, 124, 125, 126, 127,
129   128, 129, 130, 131, 132, 133, 134, 135,
130   136, 137, 138, 139, 140, 141, 142, 143,
131   144, 145, 146, 147, 148, 149, 150, 151,
132   152, 153, 154, 155, 156, 157, 158, 159,
133   160, 161, 162, 163, 164, 165, 166, 167,
134   168, 169, 170, 171, 172, 173, 174, 175,
135   176, 177, 178, 179, 180, 181, 182, 183,
136   184, 185, 186, 187, 188, 189, 190, 191,
137   192, 193, 194, 195, 196, 197, 198, 199,
138   200, 201, 202, 203, 204, 205, 206, 207,
139   208, 209, 210, 211, 212, 213, 214, 215,
140   216, 217, 218, 219, 220, 221, 222, 223,
141   224, 225, 226, 227, 228, 229, 230, 231,
142   232, 233, 234, 235, 236, 237, 238, 239,
143   240, 241, 242, 243, 244, 245, 246, 247,
144   248, 249, 250, 251, 252, 253, 254, 255
145 };
146 
147 // =======================================================================
148 
149 struct Impl1ByteUnicodeTabData
150 {
151 	rtl_TextEncoding			meTextEncoding;
152 	sal_Unicode 				maUniTab[256];
153 	Impl1ByteUnicodeTabData*	mpNext;
154 };
155 
156 // -----------------------------------------------------------------------
157 
158 struct Impl1ByteConvertTabData
159 {
160 	rtl_TextEncoding			meSrcTextEncoding;
161 	rtl_TextEncoding			meDestTextEncoding;
162 	sal_uChar					maConvertTab[256];
163 	sal_uChar					maRepConvertTab[256];
164 	Impl1ByteConvertTabData*	mpNext;
165 };
166 
167 // =======================================================================
168 
ImplGet1ByteUnicodeTab(rtl_TextEncoding eTextEncoding)169 sal_Unicode* ImplGet1ByteUnicodeTab( rtl_TextEncoding eTextEncoding )
170 {
171 #ifndef BOOTSTRAP
172 	TOOLSINDATA*				pToolsData = ImplGetToolsInData();
173 #else
174 	TOOLSINDATA*				pToolsData = 0x0;
175 #endif
176 	Impl1ByteUnicodeTabData*	pTab = pToolsData->mpFirstUniTabData;
177 
178 	while ( pTab )
179 	{
180 		if ( pTab->meTextEncoding == eTextEncoding )
181 			return pTab->maUniTab;
182 		pTab = pTab->mpNext;
183 	}
184 
185 	// get TextEncodingInfo
186 	rtl_TextEncodingInfo aTextEncInfo;
187 	aTextEncInfo.StructSize = sizeof( aTextEncInfo );
188 	rtl_getTextEncodingInfo( eTextEncoding, &aTextEncInfo );
189 
190 	if ( aTextEncInfo.MaximumCharSize == 1 )
191 	{
192 		pTab = new Impl1ByteUnicodeTabData;
193 		pTab->meTextEncoding = eTextEncoding;
194 		pTab->mpNext = pToolsData->mpFirstUniTabData;
195 
196 		rtl_TextToUnicodeConverter	hConverter;
197 		sal_uInt32					nInfo;
198 		sal_Size					nSrcBytes;
199 		sal_Size					nDestChars;
200 		hConverter = rtl_createTextToUnicodeConverter( eTextEncoding );
201 		nDestChars = rtl_convertTextToUnicode( hConverter, 0,
202 											   (const sal_Char*)aImplByteTab, 256,
203 											   pTab->maUniTab, 256,
204 											   RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE |
205 											   RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT |
206 											   RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT,
207 											   &nInfo, &nSrcBytes );
208 		rtl_destroyTextToUnicodeConverter( hConverter );
209 
210 		if ( (nSrcBytes != 256) || (nDestChars != 256) )
211 			delete pTab;
212 		else
213 		{
214 			pToolsData->mpFirstUniTabData = pTab;
215 			return pTab->maUniTab;
216 		}
217 	}
218 
219 	return NULL;
220 }
221 
222 // -----------------------------------------------------------------------
223 
ImplGet1ByteConvertTab(rtl_TextEncoding eSrcTextEncoding,rtl_TextEncoding eDestTextEncoding,sal_Bool bReplace)224 static sal_uChar* ImplGet1ByteConvertTab( rtl_TextEncoding eSrcTextEncoding,
225 										  rtl_TextEncoding eDestTextEncoding,
226 										  sal_Bool bReplace )
227 {
228 #ifndef BOOTSTRAP
229 	TOOLSINDATA*				pToolsData = ImplGetToolsInData();
230 #else
231 	TOOLSINDATA*				pToolsData = 0x0;
232 #endif
233 	Impl1ByteConvertTabData*	pTab = pToolsData->mpFirstConvertTabData;
234 
235 	while ( pTab )
236 	{
237 		if ( (pTab->meSrcTextEncoding == eSrcTextEncoding) &&
238 			 (pTab->meDestTextEncoding == eDestTextEncoding) )
239 		{
240 			if ( bReplace )
241 				return pTab->maRepConvertTab;
242 			else
243 				return pTab->maConvertTab;
244 		}
245 		pTab = pTab->mpNext;
246 	}
247 
248 	// get TextEncodingInfo
249 	rtl_TextEncodingInfo aTextEncInfo1;
250 	aTextEncInfo1.StructSize = sizeof( aTextEncInfo1 );
251 	rtl_getTextEncodingInfo( eSrcTextEncoding, &aTextEncInfo1 );
252 	rtl_TextEncodingInfo aTextEncInfo2;
253 	aTextEncInfo2.StructSize = sizeof( aTextEncInfo2 );
254 	rtl_getTextEncodingInfo( eDestTextEncoding, &aTextEncInfo2 );
255 
256 	if ( (aTextEncInfo1.MaximumCharSize == 1) &&
257 		 (aTextEncInfo2.MaximumCharSize == 1) )
258 	{
259 		pTab = new Impl1ByteConvertTabData;
260 		pTab->meSrcTextEncoding = eSrcTextEncoding;
261 		pTab->meDestTextEncoding = eDestTextEncoding;
262 		pTab->mpNext = pToolsData->mpFirstConvertTabData;
263 
264 		rtl_TextToUnicodeConverter	hConverter;
265 		rtl_UnicodeToTextConverter	hConverter2;
266 		sal_uInt32					nInfo;
267 		sal_Size					nSrcBytes;
268 		sal_Size					nDestChars;
269 		sal_Size					nSrcChars;
270 		sal_Size					nDestBytes;
271 		sal_Unicode 				aTempBuf[256];
272 		hConverter = rtl_createTextToUnicodeConverter( eSrcTextEncoding );
273 		nDestChars = rtl_convertTextToUnicode( hConverter, 0,
274 											   (const sal_Char*)aImplByteTab, 256,
275 											   aTempBuf, 256,
276 											   RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT |
277 											   RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT |
278 											   RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT,
279 											   &nInfo, &nSrcBytes );
280 		rtl_destroyTextToUnicodeConverter( hConverter );
281 		if ( (nSrcBytes != 256) || (nDestChars != 256) )
282 			delete pTab;
283 		else
284 		{
285 			hConverter2 = rtl_createUnicodeToTextConverter( eDestTextEncoding );
286 			nDestBytes = rtl_convertUnicodeToText( hConverter2, 0,
287 												   aTempBuf, 256,
288 												   (sal_Char*)pTab->maConvertTab, 256,
289 												   RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0 |
290 												   RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT,
291 												   &nInfo, &nSrcChars );
292 			if ( (nDestBytes == 256) || (nSrcChars == 256) )
293 			{
294 				nDestBytes = rtl_convertUnicodeToText( hConverter2, 0,
295 													   aTempBuf, 256,
296 													   (sal_Char*)pTab->maRepConvertTab, 256,
297 													   RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT |
298 													   RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT |
299 													   RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE,
300 													   &nInfo, &nSrcChars );
301 			}
302 			rtl_destroyUnicodeToTextConverter( hConverter2 );
303 			if ( (nDestBytes != 256) || (nSrcChars != 256) )
304 				delete pTab;
305 			else
306 			{
307 				pToolsData->mpFirstConvertTabData = pTab;
308 				if ( bReplace )
309 					return pTab->maRepConvertTab;
310 				else
311 					return pTab->maConvertTab;
312 			}
313 		}
314 	}
315 
316 	return NULL;
317 }
318 
319 // =======================================================================
320 
ImplDeleteCharTabData()321 void ImplDeleteCharTabData()
322 {
323 #ifndef BOOTSTRAP
324 	TOOLSINDATA*				pToolsData = ImplGetToolsInData();
325 #else
326 	TOOLSINDATA*				pToolsData = 0x0;
327 #endif
328 	Impl1ByteUnicodeTabData*	pTempUniTab;
329 	Impl1ByteUnicodeTabData*	pUniTab = pToolsData->mpFirstUniTabData;
330 	while ( pUniTab )
331 	{
332 		pTempUniTab = pUniTab->mpNext;
333 		delete pUniTab;
334 		pUniTab = pTempUniTab;
335 	}
336 	pToolsData->mpFirstUniTabData = NULL;
337 
338 	Impl1ByteConvertTabData*	pTempConvertTab;
339 	Impl1ByteConvertTabData*	pConvertTab = pToolsData->mpFirstConvertTabData;
340 	while ( pConvertTab )
341 	{
342 		pTempConvertTab = pConvertTab->mpNext;
343 		delete pConvertTab;
344 		pConvertTab = pTempConvertTab;
345 	}
346 	pToolsData->mpFirstConvertTabData = NULL;
347 }
348 
349 // =======================================================================
350 
ImplStringConvert(rtl_TextEncoding eSource,rtl_TextEncoding eTarget,sal_Bool bReplace)351 void ByteString::ImplStringConvert(
352     rtl_TextEncoding eSource, rtl_TextEncoding eTarget, sal_Bool bReplace )
353 {
354 	sal_uChar* pConvertTab = ImplGet1ByteConvertTab( eSource, eTarget, bReplace );
355 	if ( pConvertTab )
356 	{
357 		char* pStr = mpData->maStr;
358 		while ( *pStr )
359 		{
360 			sal_uChar c = (sal_uChar)*pStr;
361 			sal_uChar cConv = pConvertTab[c];
362 			if ( c != cConv )
363 			{
364 				pStr = ImplCopyStringData( pStr );
365 				*pStr = (char)cConv;
366 			}
367 
368 			pStr++;
369 		}
370 	}
371 	else
372 	{
373 		rtl_UnicodeToTextConverter	hSrcConverter = rtl_createTextToUnicodeConverter( eSource );
374 		sal_uInt32					nInfo;
375 		sal_Size					nSrcBytes;
376 		sal_Size					nDestChars;
377 		sal_Size					nTempLen;
378 		sal_Unicode*				pTempBuf;
379 		nTempLen = mpData->mnLen;
380 		pTempBuf = new sal_Unicode[nTempLen];
381 		nDestChars = rtl_convertTextToUnicode( hSrcConverter, 0,
382 											   mpData->maStr, mpData->mnLen,
383 											   pTempBuf, nTempLen,
384 											   RTL_TEXTTOUNICODE_FLAGS_FLUSH |
385 											   RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE |
386 											   RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT |
387 											   RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT,
388 											   &nInfo, &nSrcBytes );
389 		rtl_destroyTextToUnicodeConverter( hSrcConverter );
390 		// Hier werten wir bReplace nicht aus, da fuer MultiByte-Textencodings
391 		// sowieso keine Ersatzdarstellung moeglich ist. Da sich der String
392 		// sowieso in der Laenge aendern kann, nehmen wir auch sonst keine
393 		// Ruecksicht darauf, das die Laenge erhalten bleibt.
394 		ImplUpdateStringFromUniString( pTempBuf, nDestChars, eTarget,
395 									   RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT |
396 									   RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT |
397 									   RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE |
398 									   RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACESTR |
399 									   RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0 |
400 									   RTL_UNICODETOTEXT_FLAGS_NONSPACING_IGNORE |
401 									   RTL_UNICODETOTEXT_FLAGS_CONTROL_IGNORE );
402 		delete [] pTempBuf;
403 	}
404 }
405 
406 // =======================================================================
407 
Convert(rtl_TextEncoding eSource,rtl_TextEncoding eTarget,sal_Bool bReplace)408 ByteString& ByteString::Convert( rtl_TextEncoding eSource, rtl_TextEncoding eTarget, sal_Bool bReplace )
409 {
410 	DBG_CHKTHIS( ByteString, DbgCheckByteString );
411 
412 	// rtl_TextEncoding Dontknow kann nicht konvertiert werden
413 	if ( (eSource == RTL_TEXTENCODING_DONTKNOW) || (eTarget == RTL_TEXTENCODING_DONTKNOW) )
414 		return *this;
415 
416 	// Wenn Source und Target gleich sind, muss nicht konvertiert werden
417 	if ( eSource == eTarget )
418 		return *this;
419 
420 	// rtl_TextEncoding Symbol nur nach Unicode oder von Unicode wandeln, ansonsten
421 	// wollen wir die Zeichencodes beibehalten
422 	if ( (eSource == RTL_TEXTENCODING_SYMBOL) &&
423 		 (eTarget != RTL_TEXTENCODING_UTF7) &&
424 		 (eTarget != RTL_TEXTENCODING_UTF8) )
425 		return *this;
426 	if ( (eTarget == RTL_TEXTENCODING_SYMBOL) &&
427 		 (eSource != RTL_TEXTENCODING_UTF7) &&
428 		 (eSource != RTL_TEXTENCODING_UTF8) )
429 		return *this;
430 
431 	// Zeichensatz umwandeln
432 	ImplStringConvert( eSource, eTarget, bReplace );
433 
434 	return *this;
435 }
436 
437 // =======================================================================
438 
Convert(char c,rtl_TextEncoding eSource,rtl_TextEncoding eTarget,sal_Bool bReplace)439 char ByteString::Convert( char c,
440 						  rtl_TextEncoding eSource, rtl_TextEncoding eTarget,
441 						  sal_Bool bReplace )
442 {
443 	// TextEncoding Dontknow kann nicht konvertiert werden
444 	if ( (eSource == RTL_TEXTENCODING_DONTKNOW) || (eTarget == RTL_TEXTENCODING_DONTKNOW) )
445 		return '\0';
446 
447 	// Wenn Source und Target gleich sind, muss nicht konvertiert werden
448 	if ( eSource == eTarget )
449 		return c;
450 
451 	// TextEncoding Symbol nur nach Unicode oder von Unicode wandeln, ansonsten
452 	// wollen wir die Zeichencodes beibehalten
453 	if ( (eSource == RTL_TEXTENCODING_SYMBOL) &&
454 		 (eTarget != RTL_TEXTENCODING_UTF7) &&
455 		 (eTarget != RTL_TEXTENCODING_UTF8) )
456 		return '\0';
457 	if ( (eTarget == RTL_TEXTENCODING_SYMBOL) &&
458 		 (eSource != RTL_TEXTENCODING_UTF7) &&
459 		 (eSource != RTL_TEXTENCODING_UTF8) )
460 		return '\0';
461 
462 	sal_uChar* pConvertTab = ImplGet1ByteConvertTab( eSource, eTarget, bReplace );
463 	if ( pConvertTab )
464 		return (char)pConvertTab[(sal_uChar)c];
465 	else
466 		return '\0';
467 }
468 
469 // =======================================================================
470 
ConvertToUnicode(char c,rtl_TextEncoding eTextEncoding)471 sal_Unicode ByteString::ConvertToUnicode( char c, rtl_TextEncoding eTextEncoding )
472 {
473 	sal_Size nLen = 1;
474 	return ConvertToUnicode( &c, &nLen, eTextEncoding );
475 }
476 
477 // -----------------------------------------------------------------------
478 
ConvertFromUnicode(sal_Unicode c,rtl_TextEncoding eTextEncoding,sal_Bool bReplace)479 char ByteString::ConvertFromUnicode( sal_Unicode c, rtl_TextEncoding eTextEncoding, sal_Bool bReplace )
480 {
481 	sal_Size	nLen;
482 	char		aBuf[30];
483 	nLen = ConvertFromUnicode( c, aBuf, sizeof( aBuf ), eTextEncoding, bReplace );
484 	if ( nLen == 1 )
485 		return aBuf[0];
486 	else
487 		return 0;
488 }
489 
490 // -----------------------------------------------------------------------
491 
ConvertToUnicode(const char * pChar,sal_Size * pLen,rtl_TextEncoding eTextEncoding)492 sal_Unicode ByteString::ConvertToUnicode( const char* pChar, sal_Size* pLen, rtl_TextEncoding eTextEncoding )
493 {
494 	// TextEncoding Dontknow wird nicht konvertiert
495 	if ( eTextEncoding == RTL_TEXTENCODING_DONTKNOW )
496 		return 0;
497 
498 	rtl_TextToUnicodeConverter	hConverter;
499 	sal_uInt32					nInfo;
500 	sal_Size					nSrcBytes;
501 	sal_Size					nDestChars;
502 	sal_Unicode 				nConvChar;
503 	hConverter = rtl_createTextToUnicodeConverter( eTextEncoding );
504 	nDestChars = rtl_convertTextToUnicode( hConverter, 0,
505 										   (const sal_Char*)pChar, *pLen,
506 										   &nConvChar, 1,
507 										   RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT |
508 										   RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT |
509 										   RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT |
510 										   RTL_TEXTTOUNICODE_FLAGS_FLUSH,
511 										   &nInfo, &nSrcBytes );
512 	rtl_destroyTextToUnicodeConverter( hConverter );
513 
514 	if ( nDestChars == 1 )
515 	{
516 		*pLen = nSrcBytes;
517 		return nConvChar;
518 	}
519 	else
520 	{
521 		*pLen = 0;
522 		return 0;
523 	}
524 }
525 
526 // -----------------------------------------------------------------------
527 
ConvertFromUnicode(sal_Unicode c,char * pBuf,sal_Size nBufLen,rtl_TextEncoding eTextEncoding,sal_Bool bReplace)528 sal_Size ByteString::ConvertFromUnicode( sal_Unicode c, char* pBuf, sal_Size nBufLen, rtl_TextEncoding eTextEncoding,
529 										 sal_Bool bReplace )
530 {
531 	// TextEncoding Dontknow wird nicht konvertiert
532 	if ( eTextEncoding == RTL_TEXTENCODING_DONTKNOW )
533 		return '\0';
534 
535 	rtl_UnicodeToTextConverter	hConverter;
536 	sal_uInt32					nInfo;
537 	sal_Size					nSrcChars;
538 	sal_Size					nDestBytes;
539 	sal_Unicode 				cUni = c;
540 	sal_uInt32					nFlags = RTL_UNICODETOTEXT_FLAGS_NONSPACING_IGNORE |
541 										 RTL_UNICODETOTEXT_FLAGS_CONTROL_IGNORE |
542 										 RTL_UNICODETOTEXT_FLAGS_FLUSH;
543 	if ( bReplace )
544 	{
545 		nFlags |= RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT |
546 				  RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT;
547 		nFlags |= RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE;
548 		if ( nBufLen > 1 )
549 			nFlags |= RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACESTR;
550 	}
551 	else
552 	{
553 		nFlags |= RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0 |
554 				  RTL_UNICODETOTEXT_FLAGS_INVALID_0;
555 	}
556 
557 	hConverter = rtl_createUnicodeToTextConverter( eTextEncoding );
558 	nDestBytes = rtl_convertUnicodeToText( hConverter, 0,
559 										   &cUni, 1,
560 										   (sal_Char*)pBuf, nBufLen,
561 										   nFlags,
562 										   &nInfo, &nSrcChars );
563 	rtl_destroyUnicodeToTextConverter( hConverter );
564 	return nDestBytes;
565 }
566 
567 // =======================================================================
568 
ByteString(const rtl::OString & rStr)569 ByteString::ByteString( const rtl::OString& rStr )
570 	: mpData(NULL)
571 {
572 	DBG_CTOR( ByteString, DbgCheckByteString );
573 
574     OSL_ENSURE(rStr.pData->length < STRING_MAXLEN,
575                "Overflowing rtl::OString -> ByteString cut to zero length");
576 
577 	if (rStr.pData->length < STRING_MAXLEN)
578 	{
579 		mpData = reinterpret_cast< ByteStringData * >(const_cast< rtl::OString & >(rStr).pData);
580 	    STRING_ACQUIRE((STRING_TYPE *)mpData);
581 	}
582 	else
583 	{
584 		STRING_NEW((STRING_TYPE **)&mpData);
585 	}
586 }
587 
588 // -----------------------------------------------------------------------
589 
Assign(const rtl::OString & rStr)590 ByteString& ByteString::Assign( const rtl::OString& rStr )
591 {
592 	DBG_CHKTHIS( ByteString, DbgCheckByteString );
593 
594     OSL_ENSURE(rStr.pData->length < STRING_MAXLEN,
595                "Overflowing rtl::OString -> ByteString cut to zero length");
596 
597 	if (rStr.pData->length < STRING_MAXLEN)
598 	{
599 	    STRING_RELEASE((STRING_TYPE *)mpData);
600 		mpData = reinterpret_cast< ByteStringData * >(const_cast< rtl::OString & >(rStr).pData);
601 	    STRING_ACQUIRE((STRING_TYPE *)mpData);
602 	}
603 	else
604 	{
605 		STRING_NEW((STRING_TYPE **)&mpData);
606 	}
607 
608 	return *this;
609 }
610