bump product version to 5.0.4.1
[LibreOffice.git] / xmlhelp / source / cxxhelp / provider / db.cxx
blob93b0c65614a26fcda3ca46a3a058044e1e0a5b28
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "db.hxx"
23 #include <rtl/alloc.h>
24 #include <cstring>
26 #include <com/sun/star/io/XSeekable.hpp>
28 using namespace com::sun::star::uno;
29 using namespace com::sun::star::io;
31 namespace helpdatafileproxy {
33 void HDFData::copyToBuffer( const char* pSrcData, int nSize )
35 m_nSize = nSize;
36 delete [] m_pBuffer;
37 m_pBuffer = new char[m_nSize+1];
38 memcpy( m_pBuffer, pSrcData, m_nSize );
39 m_pBuffer[m_nSize] = 0;
43 // Hdf
45 bool Hdf::implReadLenAndData( const char* pData, int& riPos, HDFData& rValue )
47 bool bSuccess = false;
49 // Read key len
50 const char* pStartPtr = pData + riPos;
51 char* pEndPtr;
52 sal_Int32 nKeyLen = strtol( pStartPtr, &pEndPtr, 16 );
53 if( pEndPtr == pStartPtr )
54 return bSuccess;
55 riPos += (pEndPtr - pStartPtr) + 1;
57 const char* pKeySrc = pData + riPos;
58 rValue.copyToBuffer( pKeySrc, nKeyLen );
59 riPos += nKeyLen + 1;
61 bSuccess = true;
62 return bSuccess;
65 void Hdf::createHashMap( bool bOptimizeForPerformance )
67 releaseHashMap();
68 if( bOptimizeForPerformance )
70 if( m_pStringToDataMap != NULL )
71 return;
72 m_pStringToDataMap = new StringToDataMap();
74 else
76 if( m_pStringToValPosMap != NULL )
77 return;
78 m_pStringToValPosMap = new StringToValPosMap();
81 Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
82 if( xIn.is() )
84 Sequence< sal_Int8 > aData;
85 sal_Int32 nSize = m_xSFA->getSize( m_aFileURL );
86 sal_Int32 nRead = xIn->readBytes( aData, nSize );
88 const char* pData = reinterpret_cast<const char*>(aData.getConstArray());
89 int iPos = 0;
90 while( iPos < nRead )
92 HDFData aDBKey;
93 if( !implReadLenAndData( pData, iPos, aDBKey ) )
94 break;
96 OString aOKeyStr = aDBKey.getData();
98 // Read val len
99 const char* pStartPtr = pData + iPos;
100 char* pEndPtr;
101 sal_Int32 nValLen = strtol( pStartPtr, &pEndPtr, 16 );
102 if( pEndPtr == pStartPtr )
103 break;
105 iPos += (pEndPtr - pStartPtr) + 1;
107 if( bOptimizeForPerformance )
109 const char* pValSrc = pData + iPos;
110 OString aValStr( pValSrc, nValLen );
111 (*m_pStringToDataMap)[aOKeyStr] = aValStr;
113 else
115 // store value start position
116 (*m_pStringToValPosMap)[aOKeyStr] = std::pair<int,int>( iPos, nValLen );
118 iPos += nValLen + 1;
121 xIn->closeInput();
125 void Hdf::releaseHashMap()
127 if( m_pStringToDataMap != NULL )
129 delete m_pStringToDataMap;
130 m_pStringToDataMap = NULL;
132 if( m_pStringToValPosMap != NULL )
134 delete m_pStringToValPosMap;
135 m_pStringToValPosMap = NULL;
140 bool Hdf::getValueForKey( const OString& rKey, HDFData& rValue )
142 bool bSuccess = false;
143 if( !m_xSFA.is() )
144 return bSuccess;
149 if( m_pStringToDataMap == NULL && m_pStringToValPosMap == NULL )
151 bool bOptimizeForPerformance = false;
152 createHashMap( bOptimizeForPerformance );
155 if( m_pStringToValPosMap != NULL )
157 StringToValPosMap::const_iterator it = m_pStringToValPosMap->find( rKey );
158 if( it != m_pStringToValPosMap->end() )
160 const std::pair<int,int>& rValPair = it->second;
161 int iValuePos = rValPair.first;
162 int nValueLen = rValPair.second;
164 Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
165 if( xIn.is() )
167 Reference< XSeekable > xXSeekable( xIn, UNO_QUERY );
168 if( xXSeekable.is() )
170 xXSeekable->seek( iValuePos );
172 Sequence< sal_Int8 > aData;
173 sal_Int32 nRead = xIn->readBytes( aData, nValueLen );
174 if( nRead == nValueLen )
176 const char* pData = reinterpret_cast<const sal_Char*>(aData.getConstArray());
177 rValue.copyToBuffer( pData, nValueLen );
178 bSuccess = true;
181 xIn->closeInput();
186 else if( m_pStringToDataMap != NULL )
188 StringToDataMap::const_iterator it = m_pStringToDataMap->find( rKey );
189 if( it != m_pStringToDataMap->end() )
191 const OString& rValueStr = it->second;
192 int nValueLen = rValueStr.getLength();
193 const char* pData = rValueStr.getStr();
194 rValue.copyToBuffer( pData, nValueLen );
195 bSuccess = true;
200 catch( Exception & )
202 bSuccess = false;
205 return bSuccess;
208 bool Hdf::startIteration()
210 bool bSuccess = false;
212 sal_Int32 nSize = m_xSFA->getSize( m_aFileURL );
214 Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
215 if( xIn.is() )
217 m_nItRead = xIn->readBytes( m_aItData, nSize );
218 if( m_nItRead == nSize )
220 bSuccess = true;
221 m_pItData = reinterpret_cast<const char*>(m_aItData.getConstArray());
222 m_iItPos = 0;
224 else
226 stopIteration();
230 return bSuccess;
233 bool Hdf::getNextKeyAndValue( HDFData& rKey, HDFData& rValue )
235 bool bSuccess = false;
237 if( m_iItPos < m_nItRead )
239 if( implReadLenAndData( m_pItData, m_iItPos, rKey ) )
241 if( implReadLenAndData( m_pItData, m_iItPos, rValue ) )
242 bSuccess = true;
246 return bSuccess;
249 void Hdf::stopIteration()
251 m_aItData = Sequence<sal_Int8>();
252 m_pItData = NULL;
253 m_nItRead = -1;
254 m_iItPos = -1;
257 } // end of namespace helpdatafileproxy
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */