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