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