1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: strucvt.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // no include "precompiled_tools.hxx" because this is included in other cxx files.
33 // =======================================================================
35 void UniString::InitStringRes( const char* pUTF8Str
, sal_Int32 nLen
)
37 DBG_CTOR( UniString
, DbgCheckUniString
);
38 OSL_ENSURE(nLen
<= STRING_MAXLEN
, "Overflowing UniString");
41 rtl_string2UString( (rtl_uString
**)(&mpData
),
43 RTL_TEXTENCODING_UTF8
,
44 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE
|
45 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT
|
46 RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT
);
49 // =======================================================================
51 UniString::UniString( const ByteString
& rByteStr
, rtl_TextEncoding eTextEncoding
, sal_uInt32 nCvtFlags
)
53 DBG_CTOR( UniString
, DbgCheckUniString
);
54 DBG_CHKOBJ( &rByteStr
, ByteString
, DbgCheckByteString
);
57 rtl_string2UString( (rtl_uString
**)(&mpData
),
58 rByteStr
.mpData
->maStr
, rByteStr
.mpData
->mnLen
,
59 eTextEncoding
, nCvtFlags
);
62 // -----------------------------------------------------------------------
64 UniString::UniString( const ByteString
& rByteStr
, xub_StrLen nPos
, xub_StrLen nLen
,
65 rtl_TextEncoding eTextEncoding
, sal_uInt32 nCvtFlags
)
67 DBG_CTOR( UniString
, DbgCheckUniString
);
68 DBG_CHKOBJ( &rByteStr
, ByteString
, DbgCheckByteString
);
70 // Stringlaenge ermitteln
71 if ( nPos
> rByteStr
.mpData
->mnLen
)
75 // Laenge korrigieren, wenn noetig
76 sal_Int32 nMaxLen
= rByteStr
.mpData
->mnLen
-nPos
;
78 nLen
= static_cast< xub_StrLen
>(nMaxLen
);
82 rtl_string2UString( (rtl_uString
**)(&mpData
),
83 rByteStr
.mpData
->maStr
+nPos
, nLen
,
84 eTextEncoding
, nCvtFlags
);
87 // -----------------------------------------------------------------------
89 UniString::UniString( const char* pByteStr
,
90 rtl_TextEncoding eTextEncoding
, sal_uInt32 nCvtFlags
)
92 DBG_CTOR( UniString
, DbgCheckUniString
);
93 DBG_ASSERT( pByteStr
, "UniString::UniString() - pByteStr is NULL" );
96 rtl_string2UString( (rtl_uString
**)(&mpData
),
97 pByteStr
, ImplStringLen( pByteStr
),
98 eTextEncoding
, nCvtFlags
);
101 // -----------------------------------------------------------------------
103 UniString::UniString( const char* pByteStr
, xub_StrLen nLen
,
104 rtl_TextEncoding eTextEncoding
, sal_uInt32 nCvtFlags
)
106 DBG_CTOR( UniString
, DbgCheckUniString
);
107 DBG_ASSERT( pByteStr
, "UniString::UniString() - pByteStr is NULL" );
109 if ( nLen
== STRING_LEN
)
110 nLen
= ImplStringLen( pByteStr
);
113 rtl_string2UString( (rtl_uString
**)(&mpData
),
115 eTextEncoding
, nCvtFlags
);
118 // =======================================================================
120 UniString::UniString( const rtl::OUString
& rStr
)
123 DBG_CTOR( UniString
, DbgCheckUniString
);
125 OSL_ENSURE(rStr
.pData
->length
< STRING_MAXLEN
,
126 "Overflowing rtl::OUString -> UniString cut to zero length");
129 if (rStr
.pData
->length
< STRING_MAXLEN
)
131 mpData
= reinterpret_cast< UniStringData
* >(const_cast< rtl::OUString
& >(rStr
).pData
);
132 STRING_ACQUIRE((STRING_TYPE
*)mpData
);
136 STRING_NEW((STRING_TYPE
**)&mpData
);
140 // -----------------------------------------------------------------------
142 UniString
& UniString::Assign( const rtl::OUString
& rStr
)
144 DBG_CHKTHIS( UniString
, DbgCheckUniString
);
146 OSL_ENSURE(rStr
.pData
->length
< STRING_MAXLEN
,
147 "Overflowing rtl::OUString -> UniString cut to zero length");
150 if (rStr
.pData
->length
< STRING_MAXLEN
)
152 STRING_RELEASE((STRING_TYPE
*)mpData
);
153 mpData
= reinterpret_cast< UniStringData
* >(const_cast< rtl::OUString
& >(rStr
).pData
);
154 STRING_ACQUIRE((STRING_TYPE
*)mpData
);
158 STRING_NEW((STRING_TYPE
**)&mpData
);
164 UniString
UniString::intern() const
168 rtl_uString_intern( reinterpret_cast<rtl_uString
**>(&aStr
.mpData
),
169 (rtl_uString
*)(mpData
) );
174 // =======================================================================
176 #include <tools/rc.hxx>
177 #include <tools/rcid.h>
179 UniString::UniString( const ResId
& rResId
)
181 rResId
.SetRT( RSC_STRING
);
182 ResMgr
* pResMgr
= rResId
.GetResMgr();
184 if ( pResMgr
&& pResMgr
->GetResource( rResId
) )
187 RSHEADER_TYPE
* pResHdr
= (RSHEADER_TYPE
*)pResMgr
->GetClass();
188 //sal_uInt32 nLen = pResHdr->GetLocalOff() - sizeof( RSHEADER_TYPE );
190 sal_Int32 nStringLen
= rtl_str_getLength( (char*)(pResHdr
+1) );
191 InitStringRes( (const char*)(pResHdr
+1), nStringLen
);
193 sal_uInt32 nSize
= sizeof( RSHEADER_TYPE
)
194 + sal::static_int_cast
< sal_uInt32
>(nStringLen
) + 1;
196 pResMgr
->Increment( nSize
);
200 STRING_NEW((STRING_TYPE
**)&mpData
);
202 #if OSL_DEBUG_LEVEL > 0
203 *this = UniString::CreateFromAscii( "<resource id " );
204 Append( UniString::CreateFromInt32( rResId
.GetId() ) );
205 AppendAscii( " not found>" );
210 ResHookProc pImplResHookProc
= ResMgr::GetReadStringHook();
211 if ( pImplResHookProc
)
212 pImplResHookProc( *this );