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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include <connectivity/dbcharset.hxx>
27 #include "diagnose_ex.h"
28 #include <osl/diagnose.h>
29 #include <rtl/tencinfo.h>
30 
31 //.........................................................................
32 namespace dbtools
33 {
34 //.........................................................................
35 
36 	//=========================================================================
37 	//= OCharsetMap
38 	//=========================================================================
39 	//-------------------------------------------------------------------------
OCharsetMap()40 	OCharsetMap::OCharsetMap()
41 	{
42 	}
43 
44 	//-------------------------------------------------------------------------
lateConstruct()45 	void OCharsetMap::lateConstruct()
46 	{
47 		const rtl_TextEncoding eFirstEncoding = RTL_TEXTENCODING_DONTKNOW;
48 		const rtl_TextEncoding eLastEncoding = 100;		// TODO: a define in rtl/textenc.h would be fine here ...
49 		OSL_ENSURE( 0 == eFirstEncoding, "OCharsetMap::OCharsetMap: somebody changed the numbers!" );
50 
51 		rtl_TextEncodingInfo aInfo; aInfo.StructSize = sizeof( rtl_TextEncodingInfo );
52 		for ( rtl_TextEncoding eEncoding = eFirstEncoding; eEncoding < eLastEncoding; ++eEncoding )
53 		{
54 			if	(	( RTL_TEXTENCODING_DONTKNOW == eEncoding )	// this is always allowed - it has the special meaning "system encoding"
55 				||	(	rtl_getTextEncodingInfo( eEncoding, &aInfo )
56 					&&	approveEncoding( eEncoding, aInfo )
57 					)
58 				)
59 			{
60 				m_aEncodings.insert( eEncoding );
61 			}
62 		}
63 
64 		OSL_ENSURE( find( RTL_TEXTENCODING_MS_1252 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding ANSI!" );
65 		OSL_ENSURE( find( RTL_TEXTENCODING_APPLE_ROMAN ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding macintosh!" );
66 		OSL_ENSURE( find( RTL_TEXTENCODING_IBM_437 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM437!" );
67 		OSL_ENSURE( find( RTL_TEXTENCODING_IBM_850) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM850!" );
68 		OSL_ENSURE( find( RTL_TEXTENCODING_IBM_860 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM860!" );
69 		OSL_ENSURE( find( RTL_TEXTENCODING_IBM_861 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM861!" );
70 		OSL_ENSURE( find( RTL_TEXTENCODING_IBM_863 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM863!" );
71 		OSL_ENSURE( find( RTL_TEXTENCODING_IBM_865 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM865!" );
72 		OSL_ENSURE( find( RTL_TEXTENCODING_IBM_866 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM866!" );
73 		OSL_ENSURE( find( RTL_TEXTENCODING_DONTKNOW ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding SYSTEM!" );
74 		OSL_ENSURE( find( RTL_TEXTENCODING_UTF8 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding UTF-8!" );
75 		OSL_ENSURE( find( RTL_TEXTENCODING_BIG5_HKSCS ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding Big5-HKSCS!" );
76 	}
77 
78 	//-------------------------------------------------------------------------
approveEncoding(const rtl_TextEncoding _eEncoding,const rtl_TextEncodingInfo & _rInfo) const79 	sal_Bool OCharsetMap::approveEncoding( const rtl_TextEncoding _eEncoding, const rtl_TextEncodingInfo& _rInfo ) const
80 	{
81 		sal_Bool bIsMimeEncoding = 0 != ( _rInfo.Flags & RTL_TEXTENCODING_INFO_MIME );
82 		OSL_ENSURE( !bIsMimeEncoding || rtl_getMimeCharsetFromTextEncoding( _eEncoding ),
83 				"OCharsetMap::OCharsetMap: inconsistence in rtl!" );
84         OSL_UNUSED( _eEncoding );
85 		return bIsMimeEncoding;
86 	}
87 
88 	//-------------------------------------------------------------------------
~OCharsetMap()89 	OCharsetMap::~OCharsetMap()
90 	{
91 	}
92 
93 	//-------------------------------------------------------------------------
begin() const94 	OCharsetMap::CharsetIterator OCharsetMap::begin() const
95 	{
96 		ensureConstructed( );
97 		return CharsetIterator(this, m_aEncodings.begin() );
98 	}
99 
100 	//-------------------------------------------------------------------------
find(const rtl_TextEncoding _eEncoding) const101 	OCharsetMap::CharsetIterator	OCharsetMap::find(const rtl_TextEncoding _eEncoding) const
102 	{
103 		ensureConstructed( );
104 		return CharsetIterator( this, m_aEncodings.find( _eEncoding ) );
105 	}
106 
107 	//-------------------------------------------------------------------------
find(const::rtl::OUString & _rIanaName,const IANA &) const108 	OCharsetMap::CharsetIterator	OCharsetMap::find(const ::rtl::OUString& _rIanaName, const IANA&) const
109 	{
110 		ensureConstructed( );
111 
112 		rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
113 		if ( _rIanaName.getLength() )
114 		{
115 			// byte string conversion
116 			::rtl::OString sMimeByteString( _rIanaName.getStr(), _rIanaName.getLength(), RTL_TEXTENCODING_ASCII_US );
117 			// look up
118 			eEncoding = rtl_getTextEncodingFromMimeCharset( sMimeByteString.getStr() );
119 
120 			if ( RTL_TEXTENCODING_DONTKNOW == eEncoding )
121 			{	// if we're here, the name is not empty, but unknown -> this is an invalid name
122 				return end();
123 			}
124 		}
125 
126 		return find( eEncoding );
127 	}
128 
129 	//-------------------------------------------------------------------------
end() const130 	OCharsetMap::CharsetIterator OCharsetMap::end() const
131 	{
132 		ensureConstructed( );
133 
134 		return CharsetIterator( this, m_aEncodings.end() );
135 	}
136 
137 	//=========================================================================
138 	//= CharsetIteratorDerefHelper
139 	//=========================================================================
140 	//-------------------------------------------------------------------------
CharsetIteratorDerefHelper(const CharsetIteratorDerefHelper & _rSource)141 	CharsetIteratorDerefHelper::CharsetIteratorDerefHelper( const CharsetIteratorDerefHelper& _rSource )
142 		:m_eEncoding( _rSource.m_eEncoding )
143 		,m_aIanaName( _rSource.m_aIanaName )
144 	{
145 	}
146 
147 	//-------------------------------------------------------------------------
CharsetIteratorDerefHelper(const rtl_TextEncoding _eEncoding,const::rtl::OUString & _rIanaName)148 	CharsetIteratorDerefHelper:: CharsetIteratorDerefHelper(const rtl_TextEncoding _eEncoding, const ::rtl::OUString& _rIanaName )
149 		:m_eEncoding( _eEncoding )
150 		,m_aIanaName( _rIanaName )
151 	{
152 	}
153 
154 	//-------------------------------------------------------------------------
CharsetIteratorDerefHelper()155 	CharsetIteratorDerefHelper::CharsetIteratorDerefHelper()
156 		:m_eEncoding(RTL_TEXTENCODING_DONTKNOW)
157 	{
158 	}
159 
160 	//=========================================================================
161 	//= OCharsetMap::CharsetIterator
162 	//=========================================================================
163 	//-------------------------------------------------------------------------
CharsetIterator(const OCharsetMap * _pContainer,OCharsetMap::TextEncBag::const_iterator _aPos)164 	OCharsetMap::CharsetIterator::CharsetIterator(const OCharsetMap* _pContainer, OCharsetMap::TextEncBag::const_iterator _aPos )
165 		:m_pContainer( _pContainer )
166 		,m_aPos( _aPos )
167 	{
168 		OSL_ENSURE( m_pContainer, "OCharsetMap::CharsetIterator::CharsetIterator : invalid container!" );
169 	}
170 
171 	//-------------------------------------------------------------------------
CharsetIterator(const CharsetIterator & _rSource)172 	OCharsetMap::CharsetIterator::CharsetIterator(const CharsetIterator& _rSource)
173 		:m_pContainer( _rSource.m_pContainer )
174 		,m_aPos( _rSource.m_aPos )
175 	{
176 	}
177 
178 	//-------------------------------------------------------------------------
~CharsetIterator()179 	OCharsetMap::CharsetIterator::~CharsetIterator()
180 	{
181 	}
182 
183 	//-------------------------------------------------------------------------
operator *() const184 	CharsetIteratorDerefHelper OCharsetMap::CharsetIterator::operator*() const
185 	{
186 		OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.end(), "OCharsetMap::CharsetIterator::operator*: invalid position!");
187 
188 		rtl_TextEncoding eEncoding = *m_aPos;
189 		::rtl::OUString sIanaName;
190 
191 		if ( RTL_TEXTENCODING_DONTKNOW != eEncoding )
192 		{	// it's not the virtual "system charset"
193 			const char* pIanaName = rtl_getMimeCharsetFromTextEncoding( eEncoding );
194 			OSL_ENSURE( pIanaName, "OCharsetMap::CharsetIterator: invalid mime name!" );
195 			if ( pIanaName )
196 				sIanaName = ::rtl::OUString::createFromAscii( pIanaName );
197 		}
198 		return CharsetIteratorDerefHelper( eEncoding, sIanaName );
199 	}
200 
201 	//-------------------------------------------------------------------------
operator ++()202 	const OCharsetMap::CharsetIterator&	OCharsetMap::CharsetIterator::operator++()
203 	{
204 		OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.end(), "OCharsetMap::CharsetIterator::operator++ : invalid position!" );
205 		if ( m_aPos != m_pContainer->m_aEncodings.end())
206 			++m_aPos;
207 		return *this;
208 	}
209 
210 	//-------------------------------------------------------------------------
operator --()211 	const OCharsetMap::CharsetIterator&	OCharsetMap::CharsetIterator::operator--()
212 	{
213 		OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.begin(), "OCharsetMap::CharsetIterator::operator-- : invalid position!" );
214 		if ( m_aPos != m_pContainer->m_aEncodings.begin() )
215 			--m_aPos;
216 		return *this;
217 	}
218 
219 	//-------------------------------------------------------------------------
operator ==(const OCharsetMap::CharsetIterator & lhs,const OCharsetMap::CharsetIterator & rhs)220 	bool operator==(const OCharsetMap::CharsetIterator& lhs, const OCharsetMap::CharsetIterator& rhs)
221 	{
222 		return ( lhs.m_pContainer == rhs.m_pContainer ) && ( lhs.m_aPos == rhs.m_aPos );
223 	}
224 
225 //.........................................................................
226 }	// namespace dbtools
227 //.........................................................................
228 
229