xref: /aoo42x/main/tools/source/string/strucvt.cxx (revision 89b56da7)
1*89b56da7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*89b56da7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*89b56da7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*89b56da7SAndrew Rist  * distributed with this work for additional information
6*89b56da7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*89b56da7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*89b56da7SAndrew Rist  * "License"); you may not use this file except in compliance
9*89b56da7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*89b56da7SAndrew Rist  *
11*89b56da7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*89b56da7SAndrew Rist  *
13*89b56da7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*89b56da7SAndrew Rist  * software distributed under the License is distributed on an
15*89b56da7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*89b56da7SAndrew Rist  * KIND, either express or implied.  See the License for the
17*89b56da7SAndrew Rist  * specific language governing permissions and limitations
18*89b56da7SAndrew Rist  * under the License.
19*89b56da7SAndrew Rist  *
20*89b56da7SAndrew Rist  *************************************************************/
21*89b56da7SAndrew Rist 
22*89b56da7SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // no include "precompiled_tools.hxx" because this is included in other cxx files.
25cdf0e10cSrcweir 
26cdf0e10cSrcweir // =======================================================================
27cdf0e10cSrcweir 
InitStringRes(const char * pUTF8Str,sal_Int32 nLen)28cdf0e10cSrcweir void UniString::InitStringRes( const char* pUTF8Str, sal_Int32 nLen )
29cdf0e10cSrcweir {
30cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
31cdf0e10cSrcweir     OSL_ENSURE(nLen <= STRING_MAXLEN, "Overflowing UniString");
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 	mpData = NULL;
34cdf0e10cSrcweir 	rtl_string2UString( (rtl_uString **)(&mpData),
35cdf0e10cSrcweir 						pUTF8Str, nLen,
36cdf0e10cSrcweir 						RTL_TEXTENCODING_UTF8,
37cdf0e10cSrcweir 						RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE |
38cdf0e10cSrcweir 						RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT |
39cdf0e10cSrcweir 						RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT );
40cdf0e10cSrcweir }
41cdf0e10cSrcweir 
42cdf0e10cSrcweir // =======================================================================
43cdf0e10cSrcweir 
UniString(const ByteString & rByteStr,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)44cdf0e10cSrcweir UniString::UniString( const ByteString& rByteStr, rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
47cdf0e10cSrcweir 	DBG_CHKOBJ( &rByteStr, ByteString, DbgCheckByteString );
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 	mpData = NULL;
50cdf0e10cSrcweir 	rtl_string2UString( (rtl_uString **)(&mpData),
51cdf0e10cSrcweir 						rByteStr.mpData->maStr, rByteStr.mpData->mnLen,
52cdf0e10cSrcweir 						eTextEncoding, nCvtFlags );
53cdf0e10cSrcweir }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir // -----------------------------------------------------------------------
56cdf0e10cSrcweir 
UniString(const ByteString & rByteStr,xub_StrLen nPos,xub_StrLen nLen,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)57cdf0e10cSrcweir UniString::UniString( const ByteString& rByteStr, xub_StrLen nPos, xub_StrLen nLen,
58cdf0e10cSrcweir 					  rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
61cdf0e10cSrcweir 	DBG_CHKOBJ( &rByteStr, ByteString, DbgCheckByteString );
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	// Stringlaenge ermitteln
64cdf0e10cSrcweir 	if ( nPos > rByteStr.mpData->mnLen )
65cdf0e10cSrcweir 		nLen = 0;
66cdf0e10cSrcweir 	else
67cdf0e10cSrcweir 	{
68cdf0e10cSrcweir 		// Laenge korrigieren, wenn noetig
69cdf0e10cSrcweir 		sal_Int32 nMaxLen = rByteStr.mpData->mnLen-nPos;
70cdf0e10cSrcweir 		if ( nLen > nMaxLen )
71cdf0e10cSrcweir 			nLen = static_cast< xub_StrLen >(nMaxLen);
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	mpData = NULL;
75cdf0e10cSrcweir 	rtl_string2UString( (rtl_uString **)(&mpData),
76cdf0e10cSrcweir 						rByteStr.mpData->maStr+nPos, nLen,
77cdf0e10cSrcweir 						eTextEncoding, nCvtFlags );
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir // -----------------------------------------------------------------------
81cdf0e10cSrcweir 
UniString(const char * pByteStr,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)82cdf0e10cSrcweir UniString::UniString( const char* pByteStr,
83cdf0e10cSrcweir 					  rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
86cdf0e10cSrcweir 	DBG_ASSERT( pByteStr, "UniString::UniString() - pByteStr is NULL" );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	mpData = NULL;
89cdf0e10cSrcweir 	rtl_string2UString( (rtl_uString **)(&mpData),
90cdf0e10cSrcweir 						pByteStr, ImplStringLen( pByteStr ),
91cdf0e10cSrcweir 						eTextEncoding, nCvtFlags );
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir // -----------------------------------------------------------------------
95cdf0e10cSrcweir 
UniString(const char * pByteStr,xub_StrLen nLen,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)96cdf0e10cSrcweir UniString::UniString( const char* pByteStr, xub_StrLen nLen,
97cdf0e10cSrcweir 					  rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
98cdf0e10cSrcweir {
99cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
100cdf0e10cSrcweir 	DBG_ASSERT( pByteStr, "UniString::UniString() - pByteStr is NULL" );
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	if ( nLen == STRING_LEN )
103cdf0e10cSrcweir 		nLen = ImplStringLen( pByteStr );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	mpData = NULL;
106cdf0e10cSrcweir 	rtl_string2UString( (rtl_uString **)(&mpData),
107cdf0e10cSrcweir 						pByteStr, nLen,
108cdf0e10cSrcweir 						eTextEncoding, nCvtFlags );
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir // =======================================================================
112cdf0e10cSrcweir 
UniString(const rtl::OUString & rStr)113cdf0e10cSrcweir UniString::UniString( const rtl::OUString& rStr )
114cdf0e10cSrcweir 	: mpData(NULL)
115cdf0e10cSrcweir {
116cdf0e10cSrcweir 	DBG_CTOR( UniString, DbgCheckUniString );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     OSL_ENSURE(rStr.pData->length < STRING_MAXLEN,
119cdf0e10cSrcweir                "Overflowing rtl::OUString -> UniString cut to zero length");
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	if (rStr.pData->length < STRING_MAXLEN)
123cdf0e10cSrcweir 	{
124cdf0e10cSrcweir 	    mpData = reinterpret_cast< UniStringData * >(const_cast< rtl::OUString & >(rStr).pData);
125cdf0e10cSrcweir 	    STRING_ACQUIRE((STRING_TYPE *)mpData);
126cdf0e10cSrcweir 	}
127cdf0e10cSrcweir 	else
128cdf0e10cSrcweir 	{
129cdf0e10cSrcweir 		STRING_NEW((STRING_TYPE **)&mpData);
130cdf0e10cSrcweir 	}
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir // -----------------------------------------------------------------------
134cdf0e10cSrcweir 
Assign(const rtl::OUString & rStr)135cdf0e10cSrcweir UniString& UniString::Assign( const rtl::OUString& rStr )
136cdf0e10cSrcweir {
137cdf0e10cSrcweir 	DBG_CHKTHIS( UniString, DbgCheckUniString );
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     OSL_ENSURE(rStr.pData->length < STRING_MAXLEN,
140cdf0e10cSrcweir                "Overflowing rtl::OUString -> UniString cut to zero length");
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	if (rStr.pData->length < STRING_MAXLEN)
144cdf0e10cSrcweir 	{
145cdf0e10cSrcweir 	    STRING_RELEASE((STRING_TYPE *)mpData);
146cdf0e10cSrcweir 	    mpData = reinterpret_cast< UniStringData * >(const_cast< rtl::OUString & >(rStr).pData);
147cdf0e10cSrcweir 	    STRING_ACQUIRE((STRING_TYPE *)mpData);
148cdf0e10cSrcweir 	}
149cdf0e10cSrcweir 	else
150cdf0e10cSrcweir 	{
151cdf0e10cSrcweir 		STRING_NEW((STRING_TYPE **)&mpData);
152cdf0e10cSrcweir 	}
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	return *this;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
intern() const157cdf0e10cSrcweir UniString UniString::intern() const
158cdf0e10cSrcweir {
159cdf0e10cSrcweir     UniString aStr;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     rtl_uString_intern( reinterpret_cast<rtl_uString **>(&aStr.mpData),
162cdf0e10cSrcweir                         (rtl_uString *)(mpData) );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     return aStr;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir // =======================================================================
168cdf0e10cSrcweir 
169cdf0e10cSrcweir #include <tools/rc.hxx>
170cdf0e10cSrcweir #include <tools/rcid.h>
171cdf0e10cSrcweir 
UniString(const ResId & rResId)172cdf0e10cSrcweir UniString::UniString( const ResId& rResId )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     rResId.SetRT( RSC_STRING );
175cdf0e10cSrcweir     ResMgr* pResMgr = rResId.GetResMgr();
176cdf0e10cSrcweir     mpData = NULL;
177cdf0e10cSrcweir     if ( pResMgr && pResMgr->GetResource( rResId ) )
178cdf0e10cSrcweir     {
179cdf0e10cSrcweir         // String laden
180cdf0e10cSrcweir         RSHEADER_TYPE * pResHdr = (RSHEADER_TYPE*)pResMgr->GetClass();
181cdf0e10cSrcweir         //sal_uInt32 nLen = pResHdr->GetLocalOff() - sizeof( RSHEADER_TYPE );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         sal_Int32 nStringLen = rtl_str_getLength( (char*)(pResHdr+1) );
184cdf0e10cSrcweir         InitStringRes( (const char*)(pResHdr+1), nStringLen );
185cdf0e10cSrcweir 
186cdf0e10cSrcweir         sal_uInt32 nSize = sizeof( RSHEADER_TYPE )
187cdf0e10cSrcweir             + sal::static_int_cast< sal_uInt32 >(nStringLen) + 1;
188cdf0e10cSrcweir         nSize += nSize % 2;
189cdf0e10cSrcweir         pResMgr->Increment( nSize );
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir     else
192cdf0e10cSrcweir     {
193cdf0e10cSrcweir 		STRING_NEW((STRING_TYPE **)&mpData);
194cdf0e10cSrcweir 
195cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
196cdf0e10cSrcweir         *this = UniString::CreateFromAscii( "<resource id " );
197cdf0e10cSrcweir         Append( UniString::CreateFromInt32( rResId.GetId() ) );
198cdf0e10cSrcweir         AppendAscii( " not found>" );
199cdf0e10cSrcweir #endif
200cdf0e10cSrcweir         if( pResMgr )
201cdf0e10cSrcweir             pResMgr->PopContext();
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     ResHookProc pImplResHookProc = ResMgr::GetReadStringHook();
206cdf0e10cSrcweir     if ( pImplResHookProc )
207cdf0e10cSrcweir         pImplResHookProc( *this );
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210