LCOV - code coverage report
Current view: top level - xmlhelp/source/cxxhelp/provider - db.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 0 125 0.0 %
Date: 2014-04-11 Functions: 0 8 0.0 %
Legend: Lines: hit not hit

          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             : 
      21             : #include "db.hxx"
      22             : 
      23             : #include <rtl/alloc.h>
      24             : #include <cstring>
      25             : 
      26             : #include <com/sun/star/io/XSeekable.hpp>
      27             : 
      28             : using namespace com::sun::star::uno;
      29             : using namespace com::sun::star::io;
      30             : 
      31             : namespace helpdatafileproxy {
      32             : 
      33           0 : void HDFData::copyToBuffer( const char* pSrcData, int nSize )
      34             : {
      35           0 :     m_nSize = nSize;
      36           0 :     delete [] m_pBuffer;
      37           0 :     m_pBuffer = new char[m_nSize+1];
      38           0 :     memcpy( m_pBuffer, pSrcData, m_nSize );
      39           0 :     m_pBuffer[m_nSize] = 0;
      40           0 : }
      41             : 
      42             : 
      43             : // Hdf
      44             : 
      45           0 : bool Hdf::implReadLenAndData( const char* pData, int& riPos, HDFData& rValue )
      46             : {
      47           0 :     bool bSuccess = false;
      48             : 
      49             :     // Read key len
      50           0 :     const char* pStartPtr = pData + riPos;
      51             :     char* pEndPtr;
      52           0 :     sal_Int32 nKeyLen = strtol( pStartPtr, &pEndPtr, 16 );
      53           0 :     if( pEndPtr == pStartPtr )
      54           0 :         return bSuccess;
      55           0 :     riPos += (pEndPtr - pStartPtr) + 1;
      56             : 
      57           0 :     const char* pKeySrc = pData + riPos;
      58           0 :     rValue.copyToBuffer( pKeySrc, nKeyLen );
      59           0 :     riPos += nKeyLen + 1;
      60             : 
      61           0 :     bSuccess = true;
      62           0 :     return bSuccess;
      63             : }
      64             : 
      65           0 : void Hdf::createHashMap( bool bOptimizeForPerformance )
      66             : {
      67           0 :     releaseHashMap();
      68           0 :     if( bOptimizeForPerformance )
      69             :     {
      70           0 :         if( m_pStringToDataMap != NULL )
      71           0 :             return;
      72           0 :         m_pStringToDataMap = new StringToDataMap();
      73             :     }
      74             :     else
      75             :     {
      76           0 :         if( m_pStringToValPosMap != NULL )
      77           0 :             return;
      78           0 :         m_pStringToValPosMap = new StringToValPosMap();
      79             :     }
      80             : 
      81           0 :     Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
      82           0 :     if( xIn.is() )
      83             :     {
      84           0 :         Sequence< sal_Int8 > aData;
      85           0 :         sal_Int32 nSize = m_xSFA->getSize( m_aFileURL );
      86           0 :         sal_Int32 nRead = xIn->readBytes( aData, nSize );
      87             : 
      88           0 :         const char* pData = (const char*)aData.getConstArray();
      89           0 :         int iPos = 0;
      90           0 :         while( iPos < nRead )
      91             :         {
      92           0 :             HDFData aDBKey;
      93           0 :             if( !implReadLenAndData( pData, iPos, aDBKey ) )
      94           0 :                 break;
      95             : 
      96           0 :             OString aOKeyStr = aDBKey.getData();
      97             : 
      98             :             // Read val len
      99           0 :             const char* pStartPtr = pData + iPos;
     100             :             char* pEndPtr;
     101           0 :             sal_Int32 nValLen = strtol( pStartPtr, &pEndPtr, 16 );
     102           0 :             if( pEndPtr == pStartPtr )
     103           0 :                 break;
     104             : 
     105           0 :             iPos += (pEndPtr - pStartPtr) + 1;
     106             : 
     107           0 :             if( bOptimizeForPerformance )
     108             :             {
     109           0 :                 const char* pValSrc = pData + iPos;
     110           0 :                 OString aValStr( pValSrc, nValLen );
     111           0 :                 (*m_pStringToDataMap)[aOKeyStr] = aValStr;
     112             :             }
     113             :             else
     114             :             {
     115             :                 // store value start position
     116           0 :                 (*m_pStringToValPosMap)[aOKeyStr] = std::pair<int,int>( iPos, nValLen );
     117             :             }
     118           0 :             iPos += nValLen + 1;
     119           0 :         }
     120             : 
     121           0 :         xIn->closeInput();
     122           0 :     }
     123             : }
     124             : 
     125           0 : void Hdf::releaseHashMap( void )
     126             : {
     127           0 :     if( m_pStringToDataMap != NULL )
     128             :     {
     129           0 :         delete m_pStringToDataMap;
     130           0 :         m_pStringToDataMap = NULL;
     131             :     }
     132           0 :     if( m_pStringToValPosMap != NULL )
     133             :     {
     134           0 :         delete m_pStringToValPosMap;
     135           0 :         m_pStringToValPosMap = NULL;
     136             :     }
     137           0 : }
     138             : 
     139             : 
     140           0 : bool Hdf::getValueForKey( const OString& rKey, HDFData& rValue )
     141             : {
     142           0 :     bool bSuccess = false;
     143           0 :     if( !m_xSFA.is() )
     144           0 :         return bSuccess;
     145             : 
     146             :     try
     147             :     {
     148             : 
     149           0 :     if( m_pStringToDataMap == NULL && m_pStringToValPosMap == NULL )
     150             :     {
     151           0 :         bool bOptimizeForPerformance = false;
     152           0 :         createHashMap( bOptimizeForPerformance );
     153             :     }
     154             : 
     155           0 :     if( m_pStringToValPosMap != NULL )
     156             :     {
     157           0 :         StringToValPosMap::const_iterator it = m_pStringToValPosMap->find( rKey );
     158           0 :         if( it != m_pStringToValPosMap->end() )
     159             :         {
     160           0 :             const std::pair<int,int>& rValPair = it->second;
     161           0 :             int iValuePos = rValPair.first;
     162           0 :             int nValueLen = rValPair.second;
     163             : 
     164           0 :             Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
     165           0 :             if( xIn.is() )
     166             :             {
     167           0 :                 Reference< XSeekable > xXSeekable( xIn, UNO_QUERY );
     168           0 :                 if( xXSeekable.is() )
     169             :                 {
     170           0 :                     xXSeekable->seek( iValuePos );
     171             : 
     172           0 :                     Sequence< sal_Int8 > aData;
     173           0 :                     sal_Int32 nRead = xIn->readBytes( aData, nValueLen );
     174           0 :                     if( nRead == nValueLen )
     175             :                     {
     176           0 :                         const char* pData = (const sal_Char*)aData.getConstArray();
     177           0 :                         rValue.copyToBuffer( pData, nValueLen );
     178           0 :                         bSuccess = true;
     179           0 :                     }
     180             :                 }
     181           0 :                 xIn->closeInput();
     182           0 :             }
     183             :         }
     184             :     }
     185             : 
     186           0 :     else if( m_pStringToDataMap != NULL )
     187             :     {
     188           0 :         StringToDataMap::const_iterator it = m_pStringToDataMap->find( rKey );
     189           0 :         if( it != m_pStringToDataMap->end() )
     190             :         {
     191           0 :             const OString& rValueStr = it->second;
     192           0 :             int nValueLen = rValueStr.getLength();
     193           0 :             const char* pData = rValueStr.getStr();
     194           0 :             rValue.copyToBuffer( pData, nValueLen );
     195           0 :             bSuccess = true;
     196             :         }
     197             :     }
     198             : 
     199             :     }
     200           0 :     catch( Exception & )
     201             :     {
     202           0 :         bSuccess = false;
     203             :     }
     204             : 
     205           0 :     return bSuccess;
     206             : }
     207             : 
     208           0 : bool Hdf::startIteration( void )
     209             : {
     210           0 :     bool bSuccess = false;
     211             : 
     212           0 :     sal_Int32 nSize = m_xSFA->getSize( m_aFileURL );
     213             : 
     214           0 :     Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
     215           0 :     if( xIn.is() )
     216             :     {
     217           0 :         m_nItRead = xIn->readBytes( m_aItData, nSize );
     218           0 :         if( m_nItRead == nSize )
     219             :         {
     220           0 :             bSuccess = true;
     221           0 :             m_pItData = (const char*)m_aItData.getConstArray();
     222           0 :             m_iItPos = 0;
     223             :         }
     224             :         else
     225             :         {
     226           0 :             stopIteration();
     227             :         }
     228             :     }
     229             : 
     230           0 :     return bSuccess;
     231             : }
     232             : 
     233           0 : bool Hdf::getNextKeyAndValue( HDFData& rKey, HDFData& rValue )
     234             : {
     235           0 :     bool bSuccess = false;
     236             : 
     237           0 :     if( m_iItPos < m_nItRead )
     238             :     {
     239           0 :         if( implReadLenAndData( m_pItData, m_iItPos, rKey ) )
     240             :         {
     241           0 :             if( implReadLenAndData( m_pItData, m_iItPos, rValue ) )
     242           0 :                 bSuccess = true;
     243             :         }
     244             :     }
     245             : 
     246           0 :     return bSuccess;
     247             : }
     248             : 
     249           0 : void Hdf::stopIteration( void )
     250             : {
     251           0 :     m_aItData = Sequence<sal_Int8>();
     252           0 :     m_pItData = NULL;
     253           0 :     m_nItRead = -1;
     254           0 :     m_iItPos = -1;
     255           0 : }
     256             : 
     257             : } // end of namespace helpdatafileproxy
     258             : 
     259             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10