LCOV - code coverage report
Current view: top level - connectivity/source/commontools - dbcharset.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 51 68 75.0 %
Date: 2012-08-25 Functions: 12 18 66.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 28 52 53.8 %

           Branch data     Line data    Source code
       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                 :            : 
      20                 :            : #include <connectivity/dbcharset.hxx>
      21                 :            : #include "diagnose_ex.h"
      22                 :            : #include <osl/diagnose.h>
      23                 :            : #include <rtl/tencinfo.h>
      24                 :            : 
      25                 :            : //.........................................................................
      26                 :            : namespace dbtools
      27                 :            : {
      28                 :            : //.........................................................................
      29                 :            : 
      30                 :            :     //=========================================================================
      31                 :            :     //= OCharsetMap
      32                 :            :     //=========================================================================
      33                 :            :     //-------------------------------------------------------------------------
      34                 :         56 :     OCharsetMap::OCharsetMap()
      35                 :            :     {
      36                 :         56 :     }
      37                 :            : 
      38                 :            :     //-------------------------------------------------------------------------
      39                 :         56 :     void OCharsetMap::lateConstruct()
      40                 :            :     {
      41                 :         56 :         const rtl_TextEncoding eFirstEncoding = RTL_TEXTENCODING_DONTKNOW;
      42                 :         56 :         const rtl_TextEncoding eLastEncoding = 100;     // TODO: a define in rtl/textenc.h would be fine here ...
      43                 :            :         OSL_ENSURE( 0 == eFirstEncoding, "OCharsetMap::OCharsetMap: somebody changed the numbers!" );
      44                 :            : 
      45                 :         56 :         rtl_TextEncodingInfo aInfo; aInfo.StructSize = sizeof( rtl_TextEncodingInfo );
      46         [ +  + ]:       5656 :         for ( rtl_TextEncoding eEncoding = eFirstEncoding; eEncoding < eLastEncoding; ++eEncoding )
      47                 :            :         {
      48 [ +  + ][ +  + ]:      15904 :             if  (   ( RTL_TEXTENCODING_DONTKNOW == eEncoding )  // this is always allowed - it has the special meaning "system encoding"
         [ +  + ][ +  + ]
      49         [ +  - ]:       5544 :                 ||  (   rtl_getTextEncodingInfo( eEncoding, &aInfo )
      50         [ +  - ]:       4760 :                     &&  approveEncoding( eEncoding, aInfo )
      51                 :            :                     )
      52                 :            :                 )
      53                 :            :             {
      54         [ +  - ]:       3192 :                 m_aEncodings.insert( eEncoding );
      55                 :            :             }
      56                 :            :         }
      57                 :            : 
      58                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_MS_1252 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding ANSI!" );
      59                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_APPLE_ROMAN ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding macintosh!" );
      60                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_437 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM437!" );
      61                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_850) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM850!" );
      62                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_860 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM860!" );
      63                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_861 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM861!" );
      64                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_863 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM863!" );
      65                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_865 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM865!" );
      66                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_IBM_866 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding IBM866!" );
      67                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_DONTKNOW ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding SYSTEM!" );
      68                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_UTF8 ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding UTF-8!" );
      69                 :            :         OSL_ENSURE( find( RTL_TEXTENCODING_BIG5_HKSCS ) != end(), "OCharsetMap::lateConstruct: missing compatibility encoding Big5-HKSCS!" );
      70                 :         56 :     }
      71                 :            : 
      72                 :            :     //-------------------------------------------------------------------------
      73                 :       4760 :     sal_Bool OCharsetMap::approveEncoding( const rtl_TextEncoding _eEncoding, const rtl_TextEncodingInfo& _rInfo ) const
      74                 :            :     {
      75                 :       4760 :         sal_Bool bIsMimeEncoding = 0 != ( _rInfo.Flags & RTL_TEXTENCODING_INFO_MIME );
      76                 :            :         OSL_ENSURE( !bIsMimeEncoding || rtl_getMimeCharsetFromTextEncoding( _eEncoding ),
      77                 :            :                 "OCharsetMap::OCharsetMap: inconsistence in rtl!" );
      78                 :            :         OSL_UNUSED( _eEncoding );
      79                 :       4760 :         return bIsMimeEncoding;
      80                 :            :     }
      81                 :            : 
      82                 :            :     //-------------------------------------------------------------------------
      83                 :         56 :     OCharsetMap::~OCharsetMap()
      84                 :            :     {
      85         [ -  + ]:         56 :     }
      86                 :            : 
      87                 :            :     //-------------------------------------------------------------------------
      88                 :          0 :     OCharsetMap::CharsetIterator OCharsetMap::begin() const
      89                 :            :     {
      90                 :          0 :         ensureConstructed( );
      91                 :          0 :         return CharsetIterator(this, m_aEncodings.begin() );
      92                 :            :     }
      93                 :            : 
      94                 :            :     //-------------------------------------------------------------------------
      95                 :         56 :     OCharsetMap::CharsetIterator    OCharsetMap::find(const rtl_TextEncoding _eEncoding) const
      96                 :            :     {
      97                 :         56 :         ensureConstructed( );
      98                 :         56 :         return CharsetIterator( this, m_aEncodings.find( _eEncoding ) );
      99                 :            :     }
     100                 :            : 
     101                 :            :     //-------------------------------------------------------------------------
     102                 :         56 :     OCharsetMap::CharsetIterator    OCharsetMap::find(const ::rtl::OUString& _rIanaName, const IANA&) const
     103                 :            :     {
     104                 :         56 :         ensureConstructed( );
     105                 :            : 
     106                 :         56 :         rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
     107         [ +  + ]:         56 :         if ( !_rIanaName.isEmpty() )
     108                 :            :         {
     109                 :            :             // byte string conversion
     110         [ +  - ]:         12 :             ::rtl::OString sMimeByteString( _rIanaName.getStr(), _rIanaName.getLength(), RTL_TEXTENCODING_ASCII_US );
     111                 :            :             // look up
     112         [ +  - ]:         12 :             eEncoding = rtl_getTextEncodingFromMimeCharset( sMimeByteString.getStr() );
     113                 :            : 
     114         [ -  + ]:         12 :             if ( RTL_TEXTENCODING_DONTKNOW == eEncoding )
     115                 :            :             {   // if we're here, the name is not empty, but unknown -> this is an invalid name
     116         [ #  # ]:         12 :                 return end();
     117         [ +  - ]:         12 :             }
     118                 :            :         }
     119                 :            : 
     120                 :         56 :         return find( eEncoding );
     121                 :            :     }
     122                 :            : 
     123                 :            :     //-------------------------------------------------------------------------
     124                 :         56 :     OCharsetMap::CharsetIterator OCharsetMap::end() const
     125                 :            :     {
     126                 :         56 :         ensureConstructed( );
     127                 :            : 
     128                 :         56 :         return CharsetIterator( this, m_aEncodings.end() );
     129                 :            :     }
     130                 :            : 
     131                 :            :     //=========================================================================
     132                 :            :     //= CharsetIteratorDerefHelper
     133                 :            :     //=========================================================================
     134                 :            :     //-------------------------------------------------------------------------
     135                 :          0 :     CharsetIteratorDerefHelper::CharsetIteratorDerefHelper( const CharsetIteratorDerefHelper& _rSource )
     136                 :            :         :m_eEncoding( _rSource.m_eEncoding )
     137                 :          0 :         ,m_aIanaName( _rSource.m_aIanaName )
     138                 :            :     {
     139                 :          0 :     }
     140                 :            : 
     141                 :            :     //-------------------------------------------------------------------------
     142                 :         56 :     CharsetIteratorDerefHelper:: CharsetIteratorDerefHelper(const rtl_TextEncoding _eEncoding, const ::rtl::OUString& _rIanaName )
     143                 :            :         :m_eEncoding( _eEncoding )
     144                 :         56 :         ,m_aIanaName( _rIanaName )
     145                 :            :     {
     146                 :         56 :     }
     147                 :            : 
     148                 :            :     //=========================================================================
     149                 :            :     //= OCharsetMap::CharsetIterator
     150                 :            :     //=========================================================================
     151                 :            :     //-------------------------------------------------------------------------
     152                 :        112 :     OCharsetMap::CharsetIterator::CharsetIterator(const OCharsetMap* _pContainer, OCharsetMap::TextEncBag::const_iterator _aPos )
     153                 :            :         :m_pContainer( _pContainer )
     154                 :        112 :         ,m_aPos( _aPos )
     155                 :            :     {
     156                 :            :         OSL_ENSURE( m_pContainer, "OCharsetMap::CharsetIterator::CharsetIterator : invalid container!" );
     157                 :        112 :     }
     158                 :            : 
     159                 :            :     //-------------------------------------------------------------------------
     160                 :          0 :     OCharsetMap::CharsetIterator::CharsetIterator(const CharsetIterator& _rSource)
     161                 :            :         :m_pContainer( _rSource.m_pContainer )
     162                 :          0 :         ,m_aPos( _rSource.m_aPos )
     163                 :            :     {
     164                 :          0 :     }
     165                 :            : 
     166                 :            :     //-------------------------------------------------------------------------
     167                 :        112 :     OCharsetMap::CharsetIterator::~CharsetIterator()
     168                 :            :     {
     169                 :        112 :     }
     170                 :            : 
     171                 :            :     //-------------------------------------------------------------------------
     172                 :         56 :     CharsetIteratorDerefHelper OCharsetMap::CharsetIterator::operator*() const
     173                 :            :     {
     174                 :            :         OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.end(), "OCharsetMap::CharsetIterator::operator*: invalid position!");
     175                 :            : 
     176         [ +  - ]:         56 :         rtl_TextEncoding eEncoding = *m_aPos;
     177                 :         56 :         ::rtl::OUString sIanaName;
     178                 :            : 
     179         [ +  + ]:         56 :         if ( RTL_TEXTENCODING_DONTKNOW != eEncoding )
     180                 :            :         {   // it's not the virtual "system charset"
     181         [ +  - ]:         12 :             const char* pIanaName = rtl_getMimeCharsetFromTextEncoding( eEncoding );
     182                 :            :             OSL_ENSURE( pIanaName, "OCharsetMap::CharsetIterator: invalid mime name!" );
     183         [ +  - ]:         12 :             if ( pIanaName )
     184                 :         12 :                 sIanaName = ::rtl::OUString::createFromAscii( pIanaName );
     185                 :            :         }
     186         [ +  - ]:         56 :         return CharsetIteratorDerefHelper( eEncoding, sIanaName );
     187                 :            :     }
     188                 :            : 
     189                 :            :     //-------------------------------------------------------------------------
     190                 :          0 :     const OCharsetMap::CharsetIterator& OCharsetMap::CharsetIterator::operator++()
     191                 :            :     {
     192                 :            :         OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.end(), "OCharsetMap::CharsetIterator::operator++ : invalid position!" );
     193 [ #  # ][ #  # ]:          0 :         if ( m_aPos != m_pContainer->m_aEncodings.end())
     194                 :          0 :             ++m_aPos;
     195                 :          0 :         return *this;
     196                 :            :     }
     197                 :            : 
     198                 :            :     //-------------------------------------------------------------------------
     199                 :          0 :     const OCharsetMap::CharsetIterator& OCharsetMap::CharsetIterator::operator--()
     200                 :            :     {
     201                 :            :         OSL_ENSURE( m_aPos != m_pContainer->m_aEncodings.begin(), "OCharsetMap::CharsetIterator::operator-- : invalid position!" );
     202 [ #  # ][ #  # ]:          0 :         if ( m_aPos != m_pContainer->m_aEncodings.begin() )
     203                 :          0 :             --m_aPos;
     204                 :          0 :         return *this;
     205                 :            :     }
     206                 :            : 
     207                 :            :     //-------------------------------------------------------------------------
     208                 :         56 :     bool operator==(const OCharsetMap::CharsetIterator& lhs, const OCharsetMap::CharsetIterator& rhs)
     209                 :            :     {
     210 [ +  - ][ -  + ]:         56 :         return ( lhs.m_pContainer == rhs.m_pContainer ) && ( lhs.m_aPos == rhs.m_aPos );
     211                 :            :     }
     212                 :            : 
     213                 :            : //.........................................................................
     214                 :            : }   // namespace dbtools
     215                 :            : //.........................................................................
     216                 :            : 
     217                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10