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