LibreOffice Module connectivity (master) 1
dbcharset.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
21#include <utility>
22#include <osl/diagnose.h>
23#include <rtl/tencinfo.h>
24
25
26namespace dbtools
27{
28
30 {
31 }
32
33
35 {
36 const rtl_TextEncoding eFirstEncoding = RTL_TEXTENCODING_DONTKNOW;
37 const rtl_TextEncoding eLastEncoding = 100; // TODO: a define in rtl/textenc.h would be fine here ...
38 OSL_ENSURE( 0 == eFirstEncoding, "OCharsetMap::OCharsetMap: somebody changed the numbers!" );
39
40 rtl_TextEncodingInfo aInfo; aInfo.StructSize = sizeof( rtl_TextEncodingInfo );
41 for ( rtl_TextEncoding eEncoding = eFirstEncoding; eEncoding < eLastEncoding; ++eEncoding )
42 {
43 if ( ( RTL_TEXTENCODING_DONTKNOW == eEncoding ) // this is always allowed - it has the special meaning "system encoding"
44 || ( rtl_getTextEncodingInfo( eEncoding, &aInfo )
45 && approveEncoding( eEncoding, aInfo )
46 )
47 )
48 {
49 m_aEncodings.insert( eEncoding );
50 }
51 }
52
53 OSL_ENSURE( find( RTL_TEXTENCODING_MS_1252 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding ANSI!" );
54 OSL_ENSURE( find( RTL_TEXTENCODING_APPLE_ROMAN ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding macintosh!" );
55 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_437 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM437!" );
56 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_850) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM850!" );
57 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_860 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM860!" );
58 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_861 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM861!" );
59 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_863 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM863!" );
60 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_865 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM865!" );
61 OSL_ENSURE( find( RTL_TEXTENCODING_IBM_866 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM866!" );
62 OSL_ENSURE( find( RTL_TEXTENCODING_DONTKNOW ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding SYSTEM!" );
63 OSL_ENSURE( find( RTL_TEXTENCODING_UTF8 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding UTF-8!" );
64 OSL_ENSURE( find( RTL_TEXTENCODING_BIG5_HKSCS ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding Big5-HKSCS!" );
65 }
66
67
68 bool OCharsetMap::approveEncoding( const rtl_TextEncoding _eEncoding, const rtl_TextEncodingInfo& _rInfo ) const
69 {
70 bool bIsMimeEncoding = 0 != ( _rInfo.Flags & RTL_TEXTENCODING_INFO_MIME );
71 OSL_ENSURE( !bIsMimeEncoding || rtl_getMimeCharsetFromTextEncoding( _eEncoding ),
72 "OCharsetMap::OCharsetMap: inconsistence in rtl!" );
73 return bIsMimeEncoding;
74 }
75
76
78 {
79 }
80
81
83 {
85 return CharsetIterator(this, m_aEncodings.begin() );
86 }
87
88
89 OCharsetMap::CharsetIterator OCharsetMap::find(const rtl_TextEncoding _eEncoding) const
90 {
92 return CharsetIterator( this, m_aEncodings.find( _eEncoding ) );
93 }
94
95
96 OCharsetMap::CharsetIterator OCharsetMap::findIanaName(std::u16string_view _rIanaName) const
97 {
99
100 rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
101 if ( !_rIanaName.empty() )
102 {
103 // byte string conversion
104 OString sMimeByteString = OUStringToOString( _rIanaName, RTL_TEXTENCODING_ASCII_US );
105 // look up
106 eEncoding = rtl_getTextEncodingFromMimeCharset( sMimeByteString.getStr() );
107
108 if ( RTL_TEXTENCODING_DONTKNOW == eEncoding )
109 { // if we're here, the name is not empty, but unknown -> this is an invalid name
110 return end();
111 }
112 }
113
114 return find( eEncoding );
115 }
116
117
119 {
121
122 return CharsetIterator( this, m_aEncodings.end() );
123 }
124
125
127 :m_eEncoding( _rSource.m_eEncoding )
128 ,m_aIanaName( _rSource.m_aIanaName )
129 {
130 }
131
132
133 CharsetIteratorDerefHelper:: CharsetIteratorDerefHelper(const rtl_TextEncoding _eEncoding, OUString _aIanaName )
134 :m_eEncoding( _eEncoding )
135 ,m_aIanaName(std::move( _aIanaName ))
136 {
137 }
138
139 OCharsetMap::CharsetIterator::CharsetIterator(const OCharsetMap* _pContainer, OCharsetMap::TextEncBag::const_iterator _aPos )
140 :m_pContainer( _pContainer )
141 ,m_aPos(std::move( _aPos ))
142 {
143 OSL_ENSURE( m_pContainer, "OCharsetMap::CharsetIterator::CharsetIterator : invalid container!" );
144 }
145
146 CharsetIteratorDerefHelper OCharsetMap::CharsetIterator::operator*() const
147 {
148 OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.end(), "OCharsetMap::CharsetIterator::operator*: invalid position!");
149
150 rtl_TextEncoding eEncoding = *m_aPos;
151 OUString sIanaName;
152
153 if ( RTL_TEXTENCODING_DONTKNOW != eEncoding )
154 { // it's not the virtual "system charset"
155 const char* pIanaName = rtl_getMimeCharsetFromTextEncoding( eEncoding );
156 OSL_ENSURE( pIanaName, "OCharsetMap::CharsetIterator: invalid mime name!" );
157 if ( pIanaName )
158 sIanaName = OUString::createFromAscii( pIanaName );
159 }
160 return CharsetIteratorDerefHelper( eEncoding, sIanaName );
161 }
162
163
164 const OCharsetMap::CharsetIterator& OCharsetMap::CharsetIterator::operator++()
165 {
166 OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.end(), "OCharsetMap::CharsetIterator::operator++ : invalid position!" );
167 if ( m_aPos != m_pContainer->m_aEncodings.end())
168 ++m_aPos;
169 return *this;
170 }
171
172
173 const OCharsetMap::CharsetIterator& OCharsetMap::CharsetIterator::operator--()
174 {
175 OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.begin(), "OCharsetMap::CharsetIterator::operator-- : invalid position!" );
176 if ( m_aPos != m_pContainer->m_aEncodings.begin() )
177 --m_aPos;
178 return *this;
179 }
180
181
183 {
184 return ( lhs.m_pContainer == rhs.m_pContainer ) && ( lhs.m_aPos == rhs.m_aPos );
185 }
186
187
188} // namespace dbtools
189
190
191/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
CharsetIteratorDerefHelper(const CharsetIteratorDerefHelper &_rSource)
Definition: dbcharset.cxx:126
is a class which translates between different charset representations.
Definition: dbcharset.hxx:54
virtual bool approveEncoding(const rtl_TextEncoding _eEncoding, const rtl_TextEncodingInfo &_rInfo) const
Definition: dbcharset.cxx:68
CharsetIterator findIanaName(std::u16string_view _rIanaName) const
find the given IANA name in the map.
Definition: dbcharset.cxx:96
void ensureConstructed() const
Definition: dbcharset.hxx:86
CharsetIterator(const OCharsetMap *_pContainer, OCharsetMap::TextEncBag::const_iterator _aPos)
CharsetIterator find(const rtl_TextEncoding _eEncoding) const
find the given text encoding in the map.
Definition: dbcharset.cxx:89
friend class OCharsetMap
Definition: dbcharset.hxx:117
CharsetIterator end() const
get access to the (last + 1st) element of the charset collection
Definition: dbcharset.cxx:118
virtual ~OCharsetMap()
Definition: dbcharset.cxx:77
TextEncBag m_aEncodings
Definition: dbcharset.hxx:58
CharsetIterator begin() const
get access to the first element of the charset collection
Definition: dbcharset.cxx:82
bool operator==(const OCharsetMap::CharsetIterator &lhs, const OCharsetMap::CharsetIterator &rhs)
Definition: dbcharset.cxx:182
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
Definition: pq_tools.cxx:100