Update ooo320-m1
[ooovba.git] / connectivity / source / commontools / dbcharset.cxx
blob70b4cc87588b76bbed71f69c5b29779fc77aafb5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dbcharset.cxx,v $
10 * $Revision: 1.12 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include <connectivity/dbcharset.hxx>
34 #include "diagnose_ex.h"
35 #include <osl/diagnose.h>
36 #include <rtl/tencinfo.h>
38 //.........................................................................
39 namespace dbtools
41 //.........................................................................
43 //=========================================================================
44 //= OCharsetMap
45 //=========================================================================
46 //-------------------------------------------------------------------------
47 OCharsetMap::OCharsetMap()
51 //-------------------------------------------------------------------------
52 void OCharsetMap::lateConstruct()
54 const rtl_TextEncoding eFirstEncoding = RTL_TEXTENCODING_DONTKNOW;
55 const rtl_TextEncoding eLastEncoding = 100; // TODO: a define in rtl/textenc.h would be fine here ...
56 OSL_ENSURE( 0 == eFirstEncoding, "OCharsetMap::OCharsetMap: somebody changed the numbers!" );
58 rtl_TextEncodingInfo aInfo; aInfo.StructSize = sizeof( rtl_TextEncodingInfo );
59 for ( rtl_TextEncoding eEncoding = eFirstEncoding; eEncoding < eLastEncoding; ++eEncoding )
61 if ( ( RTL_TEXTENCODING_DONTKNOW == eEncoding ) // this is always allowed - it has the special meaning "system encoding"
62 || ( rtl_getTextEncodingInfo( eEncoding, &aInfo )
63 && approveEncoding( eEncoding, aInfo )
67 m_aEncodings.insert( eEncoding );
71 OSL_ENSURE( find( RTL_TEXTENCODING_MS_1252 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding ANSI!" );
72 OSL_ENSURE( find( RTL_TEXTENCODING_APPLE_ROMAN ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding macintosh!" );
73 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_437 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM437!" );
74 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_850) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM850!" );
75 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_860 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM860!" );
76 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_861 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM861!" );
77 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_863 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM863!" );
78 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_865 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM865!" );
79 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_866 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM866!" );
80 OSL_ENSURE( find( RTL_TEXTENCODING_DONTKNOW ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding SYSTEM!" );
81 OSL_ENSURE( find( RTL_TEXTENCODING_UTF8 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding UTF-8!" );
82 OSL_ENSURE( find( RTL_TEXTENCODING_BIG5_HKSCS ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding Big5-HKSCS!" );
85 //-------------------------------------------------------------------------
86 sal_Bool OCharsetMap::approveEncoding( const rtl_TextEncoding _eEncoding, const rtl_TextEncodingInfo& _rInfo ) const
88 sal_Bool bIsMimeEncoding = 0 != ( _rInfo.Flags & RTL_TEXTENCODING_INFO_MIME );
89 OSL_ENSURE( !bIsMimeEncoding || rtl_getMimeCharsetFromTextEncoding( _eEncoding ),
90 "OCharsetMap::OCharsetMap: inconsistence in rtl!" );
91 OSL_UNUSED( _eEncoding );
92 return bIsMimeEncoding;
95 //-------------------------------------------------------------------------
96 OCharsetMap::~OCharsetMap()
100 //-------------------------------------------------------------------------
101 OCharsetMap::CharsetIterator OCharsetMap::begin() const
103 ensureConstructed( );
104 return CharsetIterator(this, m_aEncodings.begin() );
107 //-------------------------------------------------------------------------
108 OCharsetMap::CharsetIterator OCharsetMap::find(const rtl_TextEncoding _eEncoding) const
110 ensureConstructed( );
111 return CharsetIterator( this, m_aEncodings.find( _eEncoding ) );
114 //-------------------------------------------------------------------------
115 OCharsetMap::CharsetIterator OCharsetMap::find(const ::rtl::OUString& _rIanaName, const IANA&) const
117 ensureConstructed( );
119 rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
120 if ( _rIanaName.getLength() )
122 // byte string conversion
123 ::rtl::OString sMimeByteString( _rIanaName.getStr(), _rIanaName.getLength(), RTL_TEXTENCODING_ASCII_US );
124 // look up
125 eEncoding = rtl_getTextEncodingFromMimeCharset( sMimeByteString.getStr() );
127 if ( RTL_TEXTENCODING_DONTKNOW == eEncoding )
128 { // if we're here, the name is not empty, but unknown -> this is an invalid name
129 return end();
133 return find( eEncoding );
136 //-------------------------------------------------------------------------
137 OCharsetMap::CharsetIterator OCharsetMap::end() const
139 ensureConstructed( );
141 return CharsetIterator( this, m_aEncodings.end() );
144 //=========================================================================
145 //= CharsetIteratorDerefHelper
146 //=========================================================================
147 //-------------------------------------------------------------------------
148 CharsetIteratorDerefHelper::CharsetIteratorDerefHelper( const CharsetIteratorDerefHelper& _rSource )
149 :m_eEncoding( _rSource.m_eEncoding )
150 ,m_aIanaName( _rSource.m_aIanaName )
154 //-------------------------------------------------------------------------
155 CharsetIteratorDerefHelper:: CharsetIteratorDerefHelper(const rtl_TextEncoding _eEncoding, const ::rtl::OUString& _rIanaName )
156 :m_eEncoding( _eEncoding )
157 ,m_aIanaName( _rIanaName )
161 //-------------------------------------------------------------------------
162 CharsetIteratorDerefHelper::CharsetIteratorDerefHelper()
163 :m_eEncoding(RTL_TEXTENCODING_DONTKNOW)
167 //=========================================================================
168 //= OCharsetMap::CharsetIterator
169 //=========================================================================
170 //-------------------------------------------------------------------------
171 OCharsetMap::CharsetIterator::CharsetIterator(const OCharsetMap* _pContainer, OCharsetMap::TextEncBag::const_iterator _aPos )
172 :m_pContainer( _pContainer )
173 ,m_aPos( _aPos )
175 OSL_ENSURE( m_pContainer, "OCharsetMap::CharsetIterator::CharsetIterator : invalid container!" );
178 //-------------------------------------------------------------------------
179 OCharsetMap::CharsetIterator::CharsetIterator(const CharsetIterator& _rSource)
180 :m_pContainer( _rSource.m_pContainer )
181 ,m_aPos( _rSource.m_aPos )
185 //-------------------------------------------------------------------------
186 OCharsetMap::CharsetIterator::~CharsetIterator()
190 //-------------------------------------------------------------------------
191 CharsetIteratorDerefHelper OCharsetMap::CharsetIterator::operator*() const
193 OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.end(), "OCharsetMap::CharsetIterator::operator*: invalid position!");
195 rtl_TextEncoding eEncoding = *m_aPos;
196 ::rtl::OUString sIanaName;
198 if ( RTL_TEXTENCODING_DONTKNOW != eEncoding )
199 { // it's not the virtual "system charset"
200 const char* pIanaName = rtl_getMimeCharsetFromTextEncoding( eEncoding );
201 OSL_ENSURE( pIanaName, "OCharsetMap::CharsetIterator: invalid mime name!" );
202 if ( pIanaName )
203 sIanaName = ::rtl::OUString::createFromAscii( pIanaName );
205 return CharsetIteratorDerefHelper( eEncoding, sIanaName );
208 //-------------------------------------------------------------------------
209 const OCharsetMap::CharsetIterator& OCharsetMap::CharsetIterator::operator++()
211 OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.end(), "OCharsetMap::CharsetIterator::operator++ : invalid position!" );
212 if ( m_aPos != m_pContainer->m_aEncodings.end())
213 ++m_aPos;
214 return *this;
217 //-------------------------------------------------------------------------
218 const OCharsetMap::CharsetIterator& OCharsetMap::CharsetIterator::operator--()
220 OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.begin(), "OCharsetMap::CharsetIterator::operator-- : invalid position!" );
221 if ( m_aPos != m_pContainer->m_aEncodings.begin() )
222 --m_aPos;
223 return *this;
226 //-------------------------------------------------------------------------
227 bool operator==(const OCharsetMap::CharsetIterator& lhs, const OCharsetMap::CharsetIterator& rhs)
229 return ( lhs.m_pContainer == rhs.m_pContainer ) && ( lhs.m_aPos == rhs.m_aPos );
232 //.........................................................................
233 } // namespace dbtools
234 //.........................................................................