xref: /trunk/main/connectivity/source/commontools/dbcharset.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include <connectivity/dbcharset.hxx>
27cdf0e10cSrcweir #include "diagnose_ex.h"
28cdf0e10cSrcweir #include <osl/diagnose.h>
29cdf0e10cSrcweir #include <rtl/tencinfo.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir //.........................................................................
32cdf0e10cSrcweir namespace dbtools
33cdf0e10cSrcweir {
34cdf0e10cSrcweir //.........................................................................
35cdf0e10cSrcweir 
36cdf0e10cSrcweir     //=========================================================================
37cdf0e10cSrcweir     //= OCharsetMap
38cdf0e10cSrcweir     //=========================================================================
39cdf0e10cSrcweir     //-------------------------------------------------------------------------
OCharsetMap()40cdf0e10cSrcweir     OCharsetMap::OCharsetMap()
41cdf0e10cSrcweir     {
42cdf0e10cSrcweir     }
43cdf0e10cSrcweir 
44cdf0e10cSrcweir     //-------------------------------------------------------------------------
lateConstruct()45cdf0e10cSrcweir     void OCharsetMap::lateConstruct()
46cdf0e10cSrcweir     {
47cdf0e10cSrcweir         const rtl_TextEncoding eFirstEncoding = RTL_TEXTENCODING_DONTKNOW;
48cdf0e10cSrcweir         const rtl_TextEncoding eLastEncoding = 100;     // TODO: a define in rtl/textenc.h would be fine here ...
49cdf0e10cSrcweir         OSL_ENSURE( 0 == eFirstEncoding, "OCharsetMap::OCharsetMap: somebody changed the numbers!" );
50cdf0e10cSrcweir 
51cdf0e10cSrcweir         rtl_TextEncodingInfo aInfo; aInfo.StructSize = sizeof( rtl_TextEncodingInfo );
52cdf0e10cSrcweir         for ( rtl_TextEncoding eEncoding = eFirstEncoding; eEncoding < eLastEncoding; ++eEncoding )
53cdf0e10cSrcweir         {
54cdf0e10cSrcweir             if  (   ( RTL_TEXTENCODING_DONTKNOW == eEncoding )  // this is always allowed - it has the special meaning "system encoding"
55cdf0e10cSrcweir                 ||  (   rtl_getTextEncodingInfo( eEncoding, &aInfo )
56cdf0e10cSrcweir                     &&  approveEncoding( eEncoding, aInfo )
57cdf0e10cSrcweir                     )
58cdf0e10cSrcweir                 )
59cdf0e10cSrcweir             {
60cdf0e10cSrcweir                 m_aEncodings.insert( eEncoding );
61cdf0e10cSrcweir             }
62cdf0e10cSrcweir         }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_MS_1252 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding ANSI!" );
65cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_APPLE_ROMAN ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding macintosh!" );
66cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_437 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM437!" );
67cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_850) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM850!" );
68cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_860 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM860!" );
69cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_861 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM861!" );
70cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_863 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM863!" );
71cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_865 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM865!" );
72cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_866 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM866!" );
73cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_DONTKNOW ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding SYSTEM!" );
74cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_UTF8 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding UTF-8!" );
75cdf0e10cSrcweir         OSL_ENSURE( find( RTL_TEXTENCODING_BIG5_HKSCS ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding Big5-HKSCS!" );
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     //-------------------------------------------------------------------------
approveEncoding(const rtl_TextEncoding _eEncoding,const rtl_TextEncodingInfo & _rInfo) const79cdf0e10cSrcweir     sal_Bool OCharsetMap::approveEncoding( const rtl_TextEncoding _eEncoding, const rtl_TextEncodingInfo& _rInfo ) const
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         sal_Bool bIsMimeEncoding = 0 != ( _rInfo.Flags & RTL_TEXTENCODING_INFO_MIME );
82cdf0e10cSrcweir         OSL_ENSURE( !bIsMimeEncoding || rtl_getMimeCharsetFromTextEncoding( _eEncoding ),
83cdf0e10cSrcweir                 "OCharsetMap::OCharsetMap: inconsistence in rtl!" );
84cdf0e10cSrcweir         OSL_UNUSED( _eEncoding );
85cdf0e10cSrcweir         return bIsMimeEncoding;
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     //-------------------------------------------------------------------------
~OCharsetMap()89cdf0e10cSrcweir     OCharsetMap::~OCharsetMap()
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     //-------------------------------------------------------------------------
begin() const94cdf0e10cSrcweir     OCharsetMap::CharsetIterator OCharsetMap::begin() const
95cdf0e10cSrcweir     {
96cdf0e10cSrcweir         ensureConstructed( );
97cdf0e10cSrcweir         return CharsetIterator(this, m_aEncodings.begin() );
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     //-------------------------------------------------------------------------
find(const rtl_TextEncoding _eEncoding) const101cdf0e10cSrcweir     OCharsetMap::CharsetIterator    OCharsetMap::find(const rtl_TextEncoding _eEncoding) const
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         ensureConstructed( );
104cdf0e10cSrcweir         return CharsetIterator( this, m_aEncodings.find( _eEncoding ) );
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     //-------------------------------------------------------------------------
find(const::rtl::OUString & _rIanaName,const IANA &) const108cdf0e10cSrcweir     OCharsetMap::CharsetIterator    OCharsetMap::find(const ::rtl::OUString& _rIanaName, const IANA&) const
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         ensureConstructed( );
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
113cdf0e10cSrcweir         if ( _rIanaName.getLength() )
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             // byte string conversion
116cdf0e10cSrcweir             ::rtl::OString sMimeByteString( _rIanaName.getStr(), _rIanaName.getLength(), RTL_TEXTENCODING_ASCII_US );
117cdf0e10cSrcweir             // look up
118cdf0e10cSrcweir             eEncoding = rtl_getTextEncodingFromMimeCharset( sMimeByteString.getStr() );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             if ( RTL_TEXTENCODING_DONTKNOW == eEncoding )
121cdf0e10cSrcweir             {   // if we're here, the name is not empty, but unknown -> this is an invalid name
122cdf0e10cSrcweir                 return end();
123cdf0e10cSrcweir             }
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         return find( eEncoding );
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     //-------------------------------------------------------------------------
end() const130cdf0e10cSrcweir     OCharsetMap::CharsetIterator OCharsetMap::end() const
131cdf0e10cSrcweir     {
132cdf0e10cSrcweir         ensureConstructed( );
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         return CharsetIterator( this, m_aEncodings.end() );
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     //=========================================================================
138cdf0e10cSrcweir     //= CharsetIteratorDerefHelper
139cdf0e10cSrcweir     //=========================================================================
140cdf0e10cSrcweir     //-------------------------------------------------------------------------
CharsetIteratorDerefHelper(const CharsetIteratorDerefHelper & _rSource)141cdf0e10cSrcweir     CharsetIteratorDerefHelper::CharsetIteratorDerefHelper( const CharsetIteratorDerefHelper& _rSource )
142cdf0e10cSrcweir         :m_eEncoding( _rSource.m_eEncoding )
143cdf0e10cSrcweir         ,m_aIanaName( _rSource.m_aIanaName )
144cdf0e10cSrcweir     {
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     //-------------------------------------------------------------------------
CharsetIteratorDerefHelper(const rtl_TextEncoding _eEncoding,const::rtl::OUString & _rIanaName)148cdf0e10cSrcweir     CharsetIteratorDerefHelper:: CharsetIteratorDerefHelper(const rtl_TextEncoding _eEncoding, const ::rtl::OUString& _rIanaName )
149cdf0e10cSrcweir         :m_eEncoding( _eEncoding )
150cdf0e10cSrcweir         ,m_aIanaName( _rIanaName )
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     //-------------------------------------------------------------------------
CharsetIteratorDerefHelper()155cdf0e10cSrcweir     CharsetIteratorDerefHelper::CharsetIteratorDerefHelper()
156cdf0e10cSrcweir         :m_eEncoding(RTL_TEXTENCODING_DONTKNOW)
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     //=========================================================================
161cdf0e10cSrcweir     //= OCharsetMap::CharsetIterator
162cdf0e10cSrcweir     //=========================================================================
163cdf0e10cSrcweir     //-------------------------------------------------------------------------
CharsetIterator(const OCharsetMap * _pContainer,OCharsetMap::TextEncBag::const_iterator _aPos)164cdf0e10cSrcweir     OCharsetMap::CharsetIterator::CharsetIterator(const OCharsetMap* _pContainer, OCharsetMap::TextEncBag::const_iterator _aPos )
165cdf0e10cSrcweir         :m_pContainer( _pContainer )
166cdf0e10cSrcweir         ,m_aPos( _aPos )
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir         OSL_ENSURE( m_pContainer, "OCharsetMap::CharsetIterator::CharsetIterator : invalid container!" );
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     //-------------------------------------------------------------------------
CharsetIterator(const CharsetIterator & _rSource)172cdf0e10cSrcweir     OCharsetMap::CharsetIterator::CharsetIterator(const CharsetIterator& _rSource)
173cdf0e10cSrcweir         :m_pContainer( _rSource.m_pContainer )
174cdf0e10cSrcweir         ,m_aPos( _rSource.m_aPos )
175cdf0e10cSrcweir     {
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     //-------------------------------------------------------------------------
~CharsetIterator()179cdf0e10cSrcweir     OCharsetMap::CharsetIterator::~CharsetIterator()
180cdf0e10cSrcweir     {
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     //-------------------------------------------------------------------------
operator *() const184cdf0e10cSrcweir     CharsetIteratorDerefHelper OCharsetMap::CharsetIterator::operator*() const
185cdf0e10cSrcweir     {
186cdf0e10cSrcweir         OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.end(), "OCharsetMap::CharsetIterator::operator*: invalid position!");
187cdf0e10cSrcweir 
188cdf0e10cSrcweir         rtl_TextEncoding eEncoding = *m_aPos;
189cdf0e10cSrcweir         ::rtl::OUString sIanaName;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir         if ( RTL_TEXTENCODING_DONTKNOW != eEncoding )
192cdf0e10cSrcweir         {   // it's not the virtual "system charset"
193cdf0e10cSrcweir             const char* pIanaName = rtl_getMimeCharsetFromTextEncoding( eEncoding );
194cdf0e10cSrcweir             OSL_ENSURE( pIanaName, "OCharsetMap::CharsetIterator: invalid mime name!" );
195cdf0e10cSrcweir             if ( pIanaName )
196cdf0e10cSrcweir                 sIanaName = ::rtl::OUString::createFromAscii( pIanaName );
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir         return CharsetIteratorDerefHelper( eEncoding, sIanaName );
199cdf0e10cSrcweir     }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     //-------------------------------------------------------------------------
operator ++()202cdf0e10cSrcweir     const OCharsetMap::CharsetIterator& OCharsetMap::CharsetIterator::operator++()
203cdf0e10cSrcweir     {
204cdf0e10cSrcweir         OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.end(), "OCharsetMap::CharsetIterator::operator++ : invalid position!" );
205cdf0e10cSrcweir         if ( m_aPos != m_pContainer->m_aEncodings.end())
206cdf0e10cSrcweir             ++m_aPos;
207cdf0e10cSrcweir         return *this;
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     //-------------------------------------------------------------------------
operator --()211cdf0e10cSrcweir     const OCharsetMap::CharsetIterator& OCharsetMap::CharsetIterator::operator--()
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.begin(), "OCharsetMap::CharsetIterator::operator-- : invalid position!" );
214cdf0e10cSrcweir         if ( m_aPos != m_pContainer->m_aEncodings.begin() )
215cdf0e10cSrcweir             --m_aPos;
216cdf0e10cSrcweir         return *this;
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     //-------------------------------------------------------------------------
operator ==(const OCharsetMap::CharsetIterator & lhs,const OCharsetMap::CharsetIterator & rhs)220cdf0e10cSrcweir     bool operator==(const OCharsetMap::CharsetIterator& lhs, const OCharsetMap::CharsetIterator& rhs)
221cdf0e10cSrcweir     {
222cdf0e10cSrcweir         return ( lhs.m_pContainer == rhs.m_pContainer ) && ( lhs.m_aPos == rhs.m_aPos );
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir //.........................................................................
226cdf0e10cSrcweir }   // namespace dbtools
227cdf0e10cSrcweir //.........................................................................
228