xref: /AOO41X/main/tools/source/string/strucvt.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // no include "precompiled_tools.hxx" because this is included in other cxx files.
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir // =======================================================================
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir void UniString::InitStringRes( const char* pUTF8Str, sal_Int32 nLen )
33*cdf0e10cSrcweir {
34*cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
35*cdf0e10cSrcweir     OSL_ENSURE(nLen <= STRING_MAXLEN, "Overflowing UniString");
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir 	mpData = NULL;
38*cdf0e10cSrcweir 	rtl_string2UString( (rtl_uString **)(&mpData),
39*cdf0e10cSrcweir 						pUTF8Str, nLen,
40*cdf0e10cSrcweir 						RTL_TEXTENCODING_UTF8,
41*cdf0e10cSrcweir 						RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE |
42*cdf0e10cSrcweir 						RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT |
43*cdf0e10cSrcweir 						RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT );
44*cdf0e10cSrcweir }
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir // =======================================================================
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir UniString::UniString( const ByteString& rByteStr, rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
51*cdf0e10cSrcweir 	DBG_CHKOBJ( &rByteStr, ByteString, DbgCheckByteString );
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	mpData = NULL;
54*cdf0e10cSrcweir 	rtl_string2UString( (rtl_uString **)(&mpData),
55*cdf0e10cSrcweir 						rByteStr.mpData->maStr, rByteStr.mpData->mnLen,
56*cdf0e10cSrcweir 						eTextEncoding, nCvtFlags );
57*cdf0e10cSrcweir }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir // -----------------------------------------------------------------------
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir UniString::UniString( const ByteString& rByteStr, xub_StrLen nPos, xub_StrLen nLen,
62*cdf0e10cSrcweir 					  rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
65*cdf0e10cSrcweir 	DBG_CHKOBJ( &rByteStr, ByteString, DbgCheckByteString );
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 	// Stringlaenge ermitteln
68*cdf0e10cSrcweir 	if ( nPos > rByteStr.mpData->mnLen )
69*cdf0e10cSrcweir 		nLen = 0;
70*cdf0e10cSrcweir 	else
71*cdf0e10cSrcweir 	{
72*cdf0e10cSrcweir 		// Laenge korrigieren, wenn noetig
73*cdf0e10cSrcweir 		sal_Int32 nMaxLen = rByteStr.mpData->mnLen-nPos;
74*cdf0e10cSrcweir 		if ( nLen > nMaxLen )
75*cdf0e10cSrcweir 			nLen = static_cast< xub_StrLen >(nMaxLen);
76*cdf0e10cSrcweir 	}
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	mpData = NULL;
79*cdf0e10cSrcweir 	rtl_string2UString( (rtl_uString **)(&mpData),
80*cdf0e10cSrcweir 						rByteStr.mpData->maStr+nPos, nLen,
81*cdf0e10cSrcweir 						eTextEncoding, nCvtFlags );
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir // -----------------------------------------------------------------------
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir UniString::UniString( const char* pByteStr,
87*cdf0e10cSrcweir 					  rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
90*cdf0e10cSrcweir 	DBG_ASSERT( pByteStr, "UniString::UniString() - pByteStr is NULL" );
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	mpData = NULL;
93*cdf0e10cSrcweir 	rtl_string2UString( (rtl_uString **)(&mpData),
94*cdf0e10cSrcweir 						pByteStr, ImplStringLen( pByteStr ),
95*cdf0e10cSrcweir 						eTextEncoding, nCvtFlags );
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir // -----------------------------------------------------------------------
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir UniString::UniString( const char* pByteStr, xub_StrLen nLen,
101*cdf0e10cSrcweir 					  rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
104*cdf0e10cSrcweir 	DBG_ASSERT( pByteStr, "UniString::UniString() - pByteStr is NULL" );
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 	if ( nLen == STRING_LEN )
107*cdf0e10cSrcweir 		nLen = ImplStringLen( pByteStr );
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	mpData = NULL;
110*cdf0e10cSrcweir 	rtl_string2UString( (rtl_uString **)(&mpData),
111*cdf0e10cSrcweir 						pByteStr, nLen,
112*cdf0e10cSrcweir 						eTextEncoding, nCvtFlags );
113*cdf0e10cSrcweir }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir // =======================================================================
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir UniString::UniString( const rtl::OUString& rStr )
118*cdf0e10cSrcweir 	: mpData(NULL)
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     OSL_ENSURE(rStr.pData->length < STRING_MAXLEN,
123*cdf0e10cSrcweir                "Overflowing rtl::OUString -> UniString cut to zero length");
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	if (rStr.pData->length < STRING_MAXLEN)
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 	    mpData = reinterpret_cast< UniStringData * >(const_cast< rtl::OUString & >(rStr).pData);
129*cdf0e10cSrcweir 	    STRING_ACQUIRE((STRING_TYPE *)mpData);
130*cdf0e10cSrcweir 	}
131*cdf0e10cSrcweir 	else
132*cdf0e10cSrcweir 	{
133*cdf0e10cSrcweir 		STRING_NEW((STRING_TYPE **)&mpData);
134*cdf0e10cSrcweir 	}
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir // -----------------------------------------------------------------------
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir UniString& UniString::Assign( const rtl::OUString& rStr )
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir 	DBG_CHKTHIS( UniString, DbgCheckUniString );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     OSL_ENSURE(rStr.pData->length < STRING_MAXLEN,
144*cdf0e10cSrcweir                "Overflowing rtl::OUString -> UniString cut to zero length");
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	if (rStr.pData->length < STRING_MAXLEN)
148*cdf0e10cSrcweir 	{
149*cdf0e10cSrcweir 	    STRING_RELEASE((STRING_TYPE *)mpData);
150*cdf0e10cSrcweir 	    mpData = reinterpret_cast< UniStringData * >(const_cast< rtl::OUString & >(rStr).pData);
151*cdf0e10cSrcweir 	    STRING_ACQUIRE((STRING_TYPE *)mpData);
152*cdf0e10cSrcweir 	}
153*cdf0e10cSrcweir 	else
154*cdf0e10cSrcweir 	{
155*cdf0e10cSrcweir 		STRING_NEW((STRING_TYPE **)&mpData);
156*cdf0e10cSrcweir 	}
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 	return *this;
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir UniString UniString::intern() const
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir     UniString aStr;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir     rtl_uString_intern( reinterpret_cast<rtl_uString **>(&aStr.mpData),
166*cdf0e10cSrcweir                         (rtl_uString *)(mpData) );
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     return aStr;
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir // =======================================================================
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir #include <tools/rc.hxx>
174*cdf0e10cSrcweir #include <tools/rcid.h>
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir UniString::UniString( const ResId& rResId )
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir     rResId.SetRT( RSC_STRING );
179*cdf0e10cSrcweir     ResMgr* pResMgr = rResId.GetResMgr();
180*cdf0e10cSrcweir     mpData = NULL;
181*cdf0e10cSrcweir     if ( pResMgr && pResMgr->GetResource( rResId ) )
182*cdf0e10cSrcweir     {
183*cdf0e10cSrcweir         // String laden
184*cdf0e10cSrcweir         RSHEADER_TYPE * pResHdr = (RSHEADER_TYPE*)pResMgr->GetClass();
185*cdf0e10cSrcweir         //sal_uInt32 nLen = pResHdr->GetLocalOff() - sizeof( RSHEADER_TYPE );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir         sal_Int32 nStringLen = rtl_str_getLength( (char*)(pResHdr+1) );
188*cdf0e10cSrcweir         InitStringRes( (const char*)(pResHdr+1), nStringLen );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir         sal_uInt32 nSize = sizeof( RSHEADER_TYPE )
191*cdf0e10cSrcweir             + sal::static_int_cast< sal_uInt32 >(nStringLen) + 1;
192*cdf0e10cSrcweir         nSize += nSize % 2;
193*cdf0e10cSrcweir         pResMgr->Increment( nSize );
194*cdf0e10cSrcweir     }
195*cdf0e10cSrcweir     else
196*cdf0e10cSrcweir     {
197*cdf0e10cSrcweir 		STRING_NEW((STRING_TYPE **)&mpData);
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
200*cdf0e10cSrcweir         *this = UniString::CreateFromAscii( "<resource id " );
201*cdf0e10cSrcweir         Append( UniString::CreateFromInt32( rResId.GetId() ) );
202*cdf0e10cSrcweir         AppendAscii( " not found>" );
203*cdf0e10cSrcweir #endif
204*cdf0e10cSrcweir         if( pResMgr )
205*cdf0e10cSrcweir             pResMgr->PopContext();
206*cdf0e10cSrcweir     }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir     ResHookProc pImplResHookProc = ResMgr::GetReadStringHook();
210*cdf0e10cSrcweir     if ( pImplResHookProc )
211*cdf0e10cSrcweir         pImplResHookProc( *this );
212*cdf0e10cSrcweir }
213*cdf0e10cSrcweir 
214