diff options
author | Frank Schönheit <fs@openoffice.org> | 2000-11-29 21:21:42 +0000 |
---|---|---|
committer | Frank Schönheit <fs@openoffice.org> | 2000-11-29 21:21:42 +0000 |
commit | 21f752342c0cddf62d5015ca5769bfc1d2ef89bc (patch) | |
tree | fe1270bc72a07dedecbc0147c4aa80f1e4309084 /connectivity | |
parent | 5310a84c09bb9078694552f8b7ac4160fce848be (diff) |
initial checkin - helper class for translating charset representations
Diffstat (limited to 'connectivity')
-rw-r--r-- | connectivity/inc/connectivity/dbcharset.hxx | 223 | ||||
-rw-r--r-- | connectivity/source/commontools/dbcharset.cxx | 289 |
2 files changed, 512 insertions, 0 deletions
diff --git a/connectivity/inc/connectivity/dbcharset.hxx b/connectivity/inc/connectivity/dbcharset.hxx new file mode 100644 index 000000000000..52660cbb7803 --- /dev/null +++ b/connectivity/inc/connectivity/dbcharset.hxx @@ -0,0 +1,223 @@ +/************************************************************************* + * + * $RCSfile: dbcharset.hxx,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: fs $ $Date: 2000-11-29 22:21:00 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#ifndef _DBHELPER_DBCHARSET_HXX_ +#define _DBHELPER_DBCHARSET_HXX_ + +#ifndef _COMPHELPER_STLTYPES_HXX_ +#include <comphelper/stl_types.hxx> +#endif +#ifndef _RTL_TEXTENC_H +#include <rtl/textenc.h> +#endif +#include <rtl/ustring.hxx> + +//......................................................................... +namespace dbtools +{ +//......................................................................... + + //========================================================================= + //= OCharsetMap + //========================================================================= + /** is a class which translates between different charset representations. + + <p>The set of recognized charsets is very limited: only the ones which are database relevant are + implemented at the moment</p> + + <p>Possible representations are: + <ul> + <li><b>IANA names.</b> + Have a look at <A href="ftp://ftp.isi.edu/in-notes/iana/assignments/character-sets">this document</A> for + more details</li> + <li><b>rtl_TextEncoding</b></li> + <li><b>logical names</b> This are strings without a deeper meaning. They're introduced because different + logical names can be matched to the same IANA name, so the opposite direction is ambiguous. That's why if + you want to make a name persisten, you probably should use the logical name.</li> + </ul> + </p> + */ + class OCharsetMap + { + protected: + DECLARE_STL_VECTOR(rtl_TextEncoding, TextEncVector); + TextEncVector m_aEncodings; + DECLARE_STL_VECTOR(::rtl::OUString, StringVector); + StringVector m_aIanaNames; + // IANA names for the charsets + StringVector m_aNames; + // logical names + + + #ifdef DBG_UTIL + sal_Int32 m_nLivingIterators; /// just for debugging reasons, counts the living iterators + #endif + + public: + class CharsetIterator; + friend class OCharsetMap::CharsetIterator; + typedef CharsetIterator iterator; + typedef CharsetIterator const_iterator; + + OCharsetMap(); + ~OCharsetMap(); + + struct Logical { }; + struct IANA { }; + + /** find the given text encoding in the map. + @return the <em>end</em> iterator if the encoding could not be found. + */ + CharsetIterator find(const rtl_TextEncoding _eEncoding) const; + /** find the given IANA name in the map. + @return the <em>end</em> iterator if the IANA name could not be found. + */ + CharsetIterator find(const ::rtl::OUString& _rIanaName, const IANA&) const; + /** find the given logical name in the map. + @return the <em>end</em> iterator if the logical name could not be found. + */ + CharsetIterator find(const ::rtl::OUString& _rLogicalName, const Logical&) const; + + sal_Int32 size() const { return m_aNames.size(); } + + /// get access to the first element of the charset collection + CharsetIterator begin() const; + /// get access to the (last + 1st) element of the charset collection + CharsetIterator end() const; + }; + + //------------------------------------------------------------------------- + //- CharsetIteratorDerefHelper + //------------------------------------------------------------------------- + class CharsetIteratorDerefHelper + { + friend class OCharsetMap::CharsetIterator; + + rtl_TextEncoding m_eEncoding; + ::rtl::OUString m_aIanaName; + ::rtl::OUString m_aName; + + public: + CharsetIteratorDerefHelper(const CharsetIteratorDerefHelper& _rSource); + + rtl_TextEncoding getEncoding() const { return m_eEncoding; } + ::rtl::OUString getIanaName() const { return m_aIanaName; } + ::rtl::OUString getName() const { return m_aName; } + + protected: + CharsetIteratorDerefHelper(); + CharsetIteratorDerefHelper(const rtl_TextEncoding _eEncoding, const ::rtl::OUString& _rIanaName, const ::rtl::OUString& _rName); + + }; + + + //------------------------------------------------------------------------- + //- OCharsetMap::CharsetIterator + //------------------------------------------------------------------------- + class OCharsetMap::CharsetIterator + { + friend class OCharsetMap; + + friend bool operator==(const CharsetIterator& lhs, const CharsetIterator& rhs); + friend bool operator!=(const CharsetIterator& lhs, const CharsetIterator& rhs) { return !(lhs == rhs); } + + friend sal_Int32 operator-(const CharsetIterator& lhs, const CharsetIterator& rhs); + + protected: + const OCharsetMap* m_pContainer; + sal_Int32 m_nPosition; + + public: + CharsetIterator(const CharsetIterator& _rSource); + ~CharsetIterator(); + + CharsetIteratorDerefHelper operator*() const; + // no -> operator + // this would require us to a) store CharsetIteratorDerefHelper instances ourself so that we + // can return a pointer or b) introduce a -> operator on the CharsetIteratorDerefHelper, too. + + /// prefix increment + const CharsetIterator& operator++(); + /// postfix increment + const CharsetIterator operator++(int) { CharsetIterator hold(*this); ++*this; return hold; } + + /// prefix decrement + const CharsetIterator& operator--(); + /// postfix decrement + const CharsetIterator operator--(int) { CharsetIterator hold(*this); --*this; return hold; } + + protected: + CharsetIterator(const OCharsetMap* _pContainer, sal_Int32 _nInitialPos = 0); + }; + +//......................................................................... +} // namespace dbtools +//......................................................................... + +#endif // _DBHELPER_DBCHARSET_HXX_ + +/************************************************************************* + * history: + * $Log: not supported by cvs2svn $ + * + * Revision 1.0 29.11.00 18:29:26 fs + ************************************************************************/ + diff --git a/connectivity/source/commontools/dbcharset.cxx b/connectivity/source/commontools/dbcharset.cxx new file mode 100644 index 000000000000..2d03edaccfef --- /dev/null +++ b/connectivity/source/commontools/dbcharset.cxx @@ -0,0 +1,289 @@ +/************************************************************************* + * + * $RCSfile: dbcharset.cxx,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: fs $ $Date: 2000-11-29 22:21:42 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#ifndef _DBHELPER_DBCHARSET_HXX_ +#include <connectivity/dbcharset.hxx> +#endif +#ifndef _OSL_DIAGNOSE_H_ +#include <osl/diagnose.h> +#endif + +//......................................................................... +namespace dbtools +{ +//......................................................................... + + //------------------------------------------------------------------------- + template <class TYPE> + sal_Int32 getSequenceIndex( const ::std::vector< TYPE >& _rUnsortedContainer, const TYPE& _rLookupElement ) + { + for ( ::std::vector< TYPE >::const_iterator aSearch = _rUnsortedContainer.begin(); + aSearch != _rUnsortedContainer.end(); + ++aSearch + ) + if (*aSearch == _rLookupElement) + return aSearch - _rUnsortedContainer.begin(); + + return _rUnsortedContainer.size(); + } + + //========================================================================= + //= OCharsetMap + //========================================================================= + //------------------------------------------------------------------------- + OCharsetMap::OCharsetMap() + #ifdef DBG_UTIL + :m_nLivingIterators(0) + #endif + { + m_aEncodings.resize(11); + m_aEncodings[0] = RTL_TEXTENCODING_MS_1252; // ANSI + m_aEncodings[1] = RTL_TEXTENCODING_APPLE_ROMAN; // MAC + m_aEncodings[2] = RTL_TEXTENCODING_IBM_850; // DOS + m_aEncodings[3] = RTL_TEXTENCODING_IBM_850; // IBMPC + m_aEncodings[4] = RTL_TEXTENCODING_IBM_437; // IBMPC_437 + m_aEncodings[5] = RTL_TEXTENCODING_IBM_850; // IBMPC_850 + m_aEncodings[6] = RTL_TEXTENCODING_IBM_860; // IBMPC_860 + m_aEncodings[7] = RTL_TEXTENCODING_IBM_861; // IBMPC_861 + m_aEncodings[8] = RTL_TEXTENCODING_IBM_863; // IBMPC_863 + m_aEncodings[9] = RTL_TEXTENCODING_IBM_865; // IBMPC_865 + m_aEncodings[10] = RTL_TEXTENCODING_DONTKNOW; // SYSTEM + + m_aNames.resize(11); + m_aNames[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ANSI")); + m_aNames[1] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("MAC")); + m_aNames[2] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DOS")); + m_aNames[3] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBMPC")); + m_aNames[4] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBMPC_437")); + m_aNames[5] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBMPC_850")); + m_aNames[6] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBMPC_860")); + m_aNames[7] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBMPC_861")); + m_aNames[8] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBMPC_863")); + m_aNames[9] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBMPC_865")); + m_aNames[10] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SYSTEM")); + + OSL_ENSURE(m_aEncodings.size() == m_aNames.size(), + "OCharsetMap::OCharsetMap: inconsistentce(1)!"); + + // the IANA representations of the character sets which we know + m_aIanaNames.resize(11); + m_aIanaNames[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("windows-1252")); + m_aIanaNames[1] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("macintosh")); + m_aIanaNames[2] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBM850")); + m_aIanaNames[3] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBM850")); + m_aIanaNames[4] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBM437")); + m_aIanaNames[5] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBM850")); + m_aIanaNames[6] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBM860")); + m_aIanaNames[7] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBM861")); + m_aIanaNames[8] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBM863")); + m_aIanaNames[9] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IBM865")); + m_aIanaNames[10] = ::rtl::OUString(); + + OSL_ENSURE(m_aNames.size() == m_aIanaNames.size(), + "OCharsetMap::OCharsetMap: inconsistentce(2)!"); + } + + //------------------------------------------------------------------------- + OCharsetMap::~OCharsetMap() + { + OSL_ENSURE(0 == m_nLivingIterators, "OCharsetMap::~OCharsetMap : there are still living iterator objects!"); + } + + //------------------------------------------------------------------------- + OCharsetMap::CharsetIterator OCharsetMap::begin() const + { + return CharsetIterator(this, 0); + } + + //------------------------------------------------------------------------- + OCharsetMap::CharsetIterator OCharsetMap::find(const rtl_TextEncoding _eEncoding) const + { + return CharsetIterator(this, getSequenceIndex(m_aEncodings, _eEncoding)); + } + + //------------------------------------------------------------------------- + OCharsetMap::CharsetIterator OCharsetMap::find(const ::rtl::OUString& _rIanaName, const IANA&) const + { + return CharsetIterator(this, getSequenceIndex(m_aIanaNames, _rIanaName)); + } + + //------------------------------------------------------------------------- + OCharsetMap::CharsetIterator OCharsetMap::find(const ::rtl::OUString& _rLogicalName, const Logical&) const + { + return CharsetIterator(this, getSequenceIndex(m_aNames, _rLogicalName)); + } + + //------------------------------------------------------------------------- + OCharsetMap::CharsetIterator OCharsetMap::end() const + { + return CharsetIterator(this, m_aNames.size()); + } + + //========================================================================= + //= CharsetIteratorDerefHelper + //========================================================================= + //------------------------------------------------------------------------- + CharsetIteratorDerefHelper::CharsetIteratorDerefHelper(const CharsetIteratorDerefHelper& _rSource) + :m_eEncoding(_rSource.m_eEncoding) + ,m_aIanaName(_rSource.m_aIanaName) + ,m_aName(_rSource.m_aName) + { + } + + //------------------------------------------------------------------------- + CharsetIteratorDerefHelper::CharsetIteratorDerefHelper(const rtl_TextEncoding _eEncoding, const ::rtl::OUString& _rIanaName, const ::rtl::OUString& _rName) + :m_eEncoding(_eEncoding) + ,m_aIanaName(_rIanaName) + ,m_aName(_rName) + { + } + + //------------------------------------------------------------------------- + CharsetIteratorDerefHelper::CharsetIteratorDerefHelper() + :m_eEncoding(RTL_TEXTENCODING_DONTKNOW) + { + } + + //========================================================================= + //= OCharsetMap::CharsetIterator + //========================================================================= + //------------------------------------------------------------------------- + OCharsetMap::CharsetIterator::CharsetIterator(const OCharsetMap* _pContainer, sal_Int32 _nInitialPos) + :m_pContainer(_pContainer) + ,m_nPosition(_nInitialPos) + { + OSL_ENSURE(m_pContainer, "OCharsetMap::CharsetIterator::CharsetIterator : invalid container!"); + #ifdef DBG_UTIL + ++const_cast<OCharsetMap*>(m_pContainer)->m_nLivingIterators; + #endif + } + + //------------------------------------------------------------------------- + OCharsetMap::CharsetIterator::CharsetIterator(const CharsetIterator& _rSource) + :m_pContainer(_rSource.m_pContainer) + ,m_nPosition(_rSource.m_nPosition) + { + #ifdef DBG_UTIL + ++const_cast<OCharsetMap*>(m_pContainer)->m_nLivingIterators; + #endif + } + + //------------------------------------------------------------------------- + OCharsetMap::CharsetIterator::~CharsetIterator() + { + #ifdef DBG_UTIL + --const_cast<OCharsetMap*>(m_pContainer)->m_nLivingIterators; + #endif + } + + //------------------------------------------------------------------------- + CharsetIteratorDerefHelper OCharsetMap::CharsetIterator::operator*() const + { + OSL_ENSURE(m_nPosition < m_pContainer->m_aEncodings.size(), "OCharsetMap::CharsetIterator::operator*: invalid position!"); + OSL_ENSURE(m_nPosition < m_pContainer->m_aIanaNames.size(), "OCharsetMap::CharsetIterator::operator*: invalid position!"); + OSL_ENSURE(m_nPosition < m_pContainer->m_aNames.size(), "OCharsetMap::CharsetIterator::operator*: invalid position!"); + return CharsetIteratorDerefHelper( + m_pContainer->m_aEncodings[m_nPosition], + m_pContainer->m_aIanaNames[m_nPosition], + m_pContainer->m_aNames[m_nPosition]); + } + + //------------------------------------------------------------------------- + const OCharsetMap::CharsetIterator& OCharsetMap::CharsetIterator::operator++() + { + OSL_ENSURE(m_nPosition < m_pContainer->m_aNames.size(), "OCharsetMap::CharsetIterator::operator++ : invalid position!"); + if (m_nPosition < m_pContainer->m_aNames.size()) + ++m_nPosition; + return *this; + } + + //------------------------------------------------------------------------- + const OCharsetMap::CharsetIterator& OCharsetMap::CharsetIterator::operator--() + { + OSL_ENSURE(m_nPosition >= 0, "OCharsetMap::CharsetIterator::operator-- : invalid position!"); + if (m_nPosition >= 0) + --m_nPosition; + return *this; + } + + //------------------------------------------------------------------------- + sal_Int32 operator-(const OCharsetMap::CharsetIterator& lhs, const OCharsetMap::CharsetIterator& rhs) + { + return lhs.m_nPosition - rhs.m_nPosition; + } + + //------------------------------------------------------------------------- + bool operator==(const OCharsetMap::CharsetIterator& lhs, const OCharsetMap::CharsetIterator& rhs) + { + return (lhs.m_pContainer == rhs.m_pContainer) && (lhs.m_nPosition == rhs.m_nPosition); + } + +//......................................................................... +} // namespace dbtools +//......................................................................... + +/************************************************************************* + * history: + * $Log: not supported by cvs2svn $ + * + * Revision 1.0 29.11.00 18:42:55 fs + ************************************************************************/ + |