xref: /aoo42x/main/tools/source/string/strucvt.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 
InitStringRes(const char * pUTF8Str,sal_Int32 nLen)28 void UniString::InitStringRes( const char* pUTF8Str, sal_Int32 nLen )
29 {
30 	DBG_CTOR( UniString, DbgCheckUniString );
31     OSL_ENSURE(nLen <= STRING_MAXLEN, "Overflowing UniString");
32 
33 	mpData = NULL;
34 	rtl_string2UString( (rtl_uString **)(&mpData),
35 						pUTF8Str, nLen,
36 						RTL_TEXTENCODING_UTF8,
37 						RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE |
38 						RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT |
39 						RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT );
40 }
41 
42 // =======================================================================
43 
UniString(const ByteString & rByteStr,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)44 UniString::UniString( const ByteString& rByteStr, rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
45 {
46 	DBG_CTOR( UniString, DbgCheckUniString );
47 	DBG_CHKOBJ( &rByteStr, ByteString, DbgCheckByteString );
48 
49 	mpData = NULL;
50 	rtl_string2UString( (rtl_uString **)(&mpData),
51 						rByteStr.mpData->maStr, rByteStr.mpData->mnLen,
52 						eTextEncoding, nCvtFlags );
53 }
54 
55 // -----------------------------------------------------------------------
56 
UniString(const ByteString & rByteStr,xub_StrLen nPos,xub_StrLen nLen,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)57 UniString::UniString( const ByteString& rByteStr, xub_StrLen nPos, xub_StrLen nLen,
58 					  rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
59 {
60 	DBG_CTOR( UniString, DbgCheckUniString );
61 	DBG_CHKOBJ( &rByteStr, ByteString, DbgCheckByteString );
62 
63 	// Stringlaenge ermitteln
64 	if ( nPos > rByteStr.mpData->mnLen )
65 		nLen = 0;
66 	else
67 	{
68 		// Laenge korrigieren, wenn noetig
69 		sal_Int32 nMaxLen = rByteStr.mpData->mnLen-nPos;
70 		if ( nLen > nMaxLen )
71 			nLen = static_cast< xub_StrLen >(nMaxLen);
72 	}
73 
74 	mpData = NULL;
75 	rtl_string2UString( (rtl_uString **)(&mpData),
76 						rByteStr.mpData->maStr+nPos, nLen,
77 						eTextEncoding, nCvtFlags );
78 }
79 
80 // -----------------------------------------------------------------------
81 
UniString(const char * pByteStr,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)82 UniString::UniString( const char* pByteStr,
83 					  rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
84 {
85 	DBG_CTOR( UniString, DbgCheckUniString );
86 	DBG_ASSERT( pByteStr, "UniString::UniString() - pByteStr is NULL" );
87 
88 	mpData = NULL;
89 	rtl_string2UString( (rtl_uString **)(&mpData),
90 						pByteStr, ImplStringLen( pByteStr ),
91 						eTextEncoding, nCvtFlags );
92 }
93 
94 // -----------------------------------------------------------------------
95 
UniString(const char * pByteStr,xub_StrLen nLen,rtl_TextEncoding eTextEncoding,sal_uInt32 nCvtFlags)96 UniString::UniString( const char* pByteStr, xub_StrLen nLen,
97 					  rtl_TextEncoding eTextEncoding, sal_uInt32 nCvtFlags )
98 {
99 	DBG_CTOR( UniString, DbgCheckUniString );
100 	DBG_ASSERT( pByteStr, "UniString::UniString() - pByteStr is NULL" );
101 
102 	if ( nLen == STRING_LEN )
103 		nLen = ImplStringLen( pByteStr );
104 
105 	mpData = NULL;
106 	rtl_string2UString( (rtl_uString **)(&mpData),
107 						pByteStr, nLen,
108 						eTextEncoding, nCvtFlags );
109 }
110 
111 // =======================================================================
112 
UniString(const rtl::OUString & rStr)113 UniString::UniString( const rtl::OUString& rStr )
114 	: mpData(NULL)
115 {
116 	DBG_CTOR( UniString, DbgCheckUniString );
117 
118     OSL_ENSURE(rStr.pData->length < STRING_MAXLEN,
119                "Overflowing rtl::OUString -> UniString cut to zero length");
120 
121 
122 	if (rStr.pData->length < STRING_MAXLEN)
123 	{
124 	    mpData = reinterpret_cast< UniStringData * >(const_cast< rtl::OUString & >(rStr).pData);
125 	    STRING_ACQUIRE((STRING_TYPE *)mpData);
126 	}
127 	else
128 	{
129 		STRING_NEW((STRING_TYPE **)&mpData);
130 	}
131 }
132 
133 // -----------------------------------------------------------------------
134 
Assign(const rtl::OUString & rStr)135 UniString& UniString::Assign( const rtl::OUString& rStr )
136 {
137 	DBG_CHKTHIS( UniString, DbgCheckUniString );
138 
139     OSL_ENSURE(rStr.pData->length < STRING_MAXLEN,
140                "Overflowing rtl::OUString -> UniString cut to zero length");
141 
142 
143 	if (rStr.pData->length < STRING_MAXLEN)
144 	{
145 	    STRING_RELEASE((STRING_TYPE *)mpData);
146 	    mpData = reinterpret_cast< UniStringData * >(const_cast< rtl::OUString & >(rStr).pData);
147 	    STRING_ACQUIRE((STRING_TYPE *)mpData);
148 	}
149 	else
150 	{
151 		STRING_NEW((STRING_TYPE **)&mpData);
152 	}
153 
154 	return *this;
155 }
156 
intern() const157 UniString UniString::intern() const
158 {
159     UniString aStr;
160 
161     rtl_uString_intern( reinterpret_cast<rtl_uString **>(&aStr.mpData),
162                         (rtl_uString *)(mpData) );
163 
164     return aStr;
165 }
166 
167 // =======================================================================
168 
169 #include <tools/rc.hxx>
170 #include <tools/rcid.h>
171 
UniString(const ResId & rResId)172 UniString::UniString( const ResId& rResId )
173 {
174     rResId.SetRT( RSC_STRING );
175     ResMgr* pResMgr = rResId.GetResMgr();
176     mpData = NULL;
177     if ( pResMgr && pResMgr->GetResource( rResId ) )
178     {
179         // String laden
180         RSHEADER_TYPE * pResHdr = (RSHEADER_TYPE*)pResMgr->GetClass();
181         //sal_uInt32 nLen = pResHdr->GetLocalOff() - sizeof( RSHEADER_TYPE );
182 
183         sal_Int32 nStringLen = rtl_str_getLength( (char*)(pResHdr+1) );
184         InitStringRes( (const char*)(pResHdr+1), nStringLen );
185 
186         sal_uInt32 nSize = sizeof( RSHEADER_TYPE )
187             + sal::static_int_cast< sal_uInt32 >(nStringLen) + 1;
188         nSize += nSize % 2;
189         pResMgr->Increment( nSize );
190     }
191     else
192     {
193 		STRING_NEW((STRING_TYPE **)&mpData);
194 
195 #if OSL_DEBUG_LEVEL > 0
196         *this = UniString::CreateFromAscii( "<resource id " );
197         Append( UniString::CreateFromInt32( rResId.GetId() ) );
198         AppendAscii( " not found>" );
199 #endif
200         if( pResMgr )
201             pResMgr->PopContext();
202     }
203 
204 
205     ResHookProc pImplResHookProc = ResMgr::GetReadStringHook();
206     if ( pImplResHookProc )
207         pImplResHookProc( *this );
208 }
209 
210