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 #ifndef _DBHELPER_DBCHARSET_HXX_
25 #define _DBHELPER_DBCHARSET_HXX_
26 
27 #include <comphelper/stl_types.hxx>
28 #include <rtl/textenc.h>
29 #include <rtl/tencinfo.h>
30 #include <rtl/ustring.hxx>
31 #include "connectivity/dbtoolsdllapi.hxx"
32 
33 //.........................................................................
34 namespace dbtools
35 {
36 //.........................................................................
37 
38 	//=========================================================================
39 	//= OCharsetMap
40 	//=========================================================================
41 	/** is a class which translates between different charset representations.
42 
43 		<p>The set of recognized charsets is very limited: only the ones which are database relevant are
44 		implemented at the moment</p>
45 
46 		<p>Possible representations are:
47 		<ul>
48 			<li><b>IANA names.</b>
49 			Have a look at <A href="http://www.iana.org/assignments/character-sets">this document</A> for
50 			more details</li>
51 			<li><b>rtl_TextEncoding</b></li>
52 		</ul>
53 		</p>
54 	*/
55 	class OOO_DLLPUBLIC_DBTOOLS OCharsetMap
56 	{
57 	protected:
58 		DECLARE_STL_STDKEY_SET( rtl_TextEncoding, TextEncBag );
59 
60 		TextEncBag	m_aEncodings;
61 
62 	public:
63 		class CharsetIterator;
64 		friend class OCharsetMap::CharsetIterator;
65 		typedef CharsetIterator iterator;
66 		typedef CharsetIterator const_iterator;
67 
68 		OCharsetMap();
69 		virtual ~OCharsetMap();
70 
71 		struct IANA { };
72 
73 		/** find the given text encoding in the map.
74 			@return the <em>end</em> iterator if the encoding could not be found.
75 		*/
76 		CharsetIterator	find(const rtl_TextEncoding _eEncoding) const;
77 		/** find the given IANA name in the map.
78 			@return the <em>end</em> iterator if the IANA name could not be found.
79 		*/
80 		CharsetIterator	find(const ::rtl::OUString& _rIanaName, const IANA&) const;
81 
size() const82 		sal_Int32	size() const { ensureConstructed( ); return m_aEncodings.size(); }
83 
84 		/// get access to the first element of the charset collection
85 		CharsetIterator	begin() const;
86 		/// get access to the (last + 1st) element of the charset collection
87 		CharsetIterator	end() const;
88 
89 	protected:
90 		// needed because we want to call a virtual method during construction
91 				void lateConstruct();
ensureConstructed() const92 		inline	void ensureConstructed( ) const { if ( m_aEncodings.empty() ) const_cast< OCharsetMap* >( this )->lateConstruct(); }
93 
94 		virtual	sal_Bool approveEncoding( const rtl_TextEncoding _eEncoding, const rtl_TextEncodingInfo& _rInfo ) const;
95 	};
96 
97 	//-------------------------------------------------------------------------
98 	//- CharsetIteratorDerefHelper
99 	//-------------------------------------------------------------------------
100 	class OOO_DLLPUBLIC_DBTOOLS CharsetIteratorDerefHelper
101 	{
102 		friend class OCharsetMap::CharsetIterator;
103 
104 		rtl_TextEncoding	m_eEncoding;
105 		::rtl::OUString		m_aIanaName;
106 
107 	public:
108 		CharsetIteratorDerefHelper(const CharsetIteratorDerefHelper& _rSource);
109 
getEncoding() const110 		rtl_TextEncoding	getEncoding() const	{ return m_eEncoding; }
getIanaName() const111 		::rtl::OUString		getIanaName() const	{ return m_aIanaName; }
112 
113 	protected:
114 		CharsetIteratorDerefHelper();
115 		CharsetIteratorDerefHelper( const rtl_TextEncoding _eEncoding, const ::rtl::OUString& _rIanaName );
116 
117 	};
118 
119 
120 	//-------------------------------------------------------------------------
121 	//- OCharsetMap::CharsetIterator
122 	//-------------------------------------------------------------------------
123 	class OOO_DLLPUBLIC_DBTOOLS OCharsetMap::CharsetIterator
124 	{
125 		friend class OCharsetMap;
126 
127 		friend OOO_DLLPUBLIC_DBTOOLS bool operator==(const CharsetIterator& lhs, const CharsetIterator& rhs);
operator !=(const CharsetIterator & lhs,const CharsetIterator & rhs)128 		friend bool operator!=(const CharsetIterator& lhs, const CharsetIterator& rhs) { return !(lhs == rhs); }
129 
130 //		friend sal_Int32 operator-(const CharsetIterator& lhs, const CharsetIterator& rhs);
131 
132 	protected:
133 		const OCharsetMap*						m_pContainer;
134 		OCharsetMap::TextEncBag::const_iterator	m_aPos;
135 
136 	public:
137 		CharsetIterator(const CharsetIterator& _rSource);
138 		~CharsetIterator();
139 
140 		CharsetIteratorDerefHelper operator*() const;
141 		// no -> operator
142 		// this would require us to a) store CharsetIteratorDerefHelper instances ourself so that we
143 		// can return a pointer or b) introduce a -> operator on the CharsetIteratorDerefHelper, too.
144 
145 		/// prefix increment
146 		const CharsetIterator&	operator++();
147 		/// postfix increment
operator ++(int)148 		const CharsetIterator	operator++(int) { CharsetIterator hold(*this); ++*this; return hold; }
149 
150 		/// prefix decrement
151 		const CharsetIterator&	operator--();
152 		/// postfix decrement
operator --(int)153 		const CharsetIterator	operator--(int) { CharsetIterator hold(*this); --*this; return hold; }
154 
155 	protected:
156 		CharsetIterator(const OCharsetMap* _pContainer, OCharsetMap::TextEncBag::const_iterator _aPos );
157 	};
158 
159 //.........................................................................
160 }	// namespace dbtools
161 //.........................................................................
162 
163 #endif // _DBHELPER_DBCHARSET_HXX_
164 
165