189dcb3daSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
389dcb3daSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
489dcb3daSAndrew Rist * or more contributor license agreements. See the NOTICE file
589dcb3daSAndrew Rist * distributed with this work for additional information
689dcb3daSAndrew Rist * regarding copyright ownership. The ASF licenses this file
789dcb3daSAndrew Rist * to you under the Apache License, Version 2.0 (the
889dcb3daSAndrew Rist * "License"); you may not use this file except in compliance
989dcb3daSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
1189dcb3daSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
1389dcb3daSAndrew Rist * Unless required by applicable law or agreed to in writing,
1489dcb3daSAndrew Rist * software distributed under the License is distributed on an
1589dcb3daSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1689dcb3daSAndrew Rist * KIND, either express or implied. See the License for the
1789dcb3daSAndrew Rist * specific language governing permissions and limitations
1889dcb3daSAndrew Rist * under the License.
19cdf0e10cSrcweir *
2089dcb3daSAndrew Rist *************************************************************/
2189dcb3daSAndrew Rist
2289dcb3daSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlhelp.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "db.hxx"
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <rtl/alloc.h>
30cdf0e10cSrcweir #include <cstring>
31cdf0e10cSrcweir
32cdf0e10cSrcweir #include "com/sun/star/io/XSeekable.hpp"
33cdf0e10cSrcweir
34cdf0e10cSrcweir using namespace com::sun::star::uno;
35cdf0e10cSrcweir using namespace com::sun::star::io;
36cdf0e10cSrcweir
37*70e197d9SHerbert Dürr namespace helpdatafileproxy {
38cdf0e10cSrcweir
39cdf0e10cSrcweir //----------------------------------------------------------------------------
copyToBuffer(const char * pSrcData,int nSize)40*70e197d9SHerbert Dürr void HDFData::copyToBuffer( const char* pSrcData, int nSize )
41cdf0e10cSrcweir {
42cdf0e10cSrcweir m_nSize = nSize;
43cdf0e10cSrcweir delete [] m_pBuffer;
44cdf0e10cSrcweir m_pBuffer = new char[m_nSize+1];
45cdf0e10cSrcweir memcpy( m_pBuffer, pSrcData, m_nSize );
46cdf0e10cSrcweir m_pBuffer[m_nSize] = 0;
47cdf0e10cSrcweir }
48cdf0e10cSrcweir
49cdf0e10cSrcweir
50*70e197d9SHerbert Dürr // Hdf
51cdf0e10cSrcweir
implReadLenAndData(const char * pData,int & riPos,HDFData & rValue)52*70e197d9SHerbert Dürr bool Hdf::implReadLenAndData( const char* pData, int& riPos, HDFData& rValue )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir bool bSuccess = false;
55cdf0e10cSrcweir
56cdf0e10cSrcweir // Read key len
57cdf0e10cSrcweir const char* pStartPtr = pData + riPos;
58cdf0e10cSrcweir char* pEndPtr;
59cdf0e10cSrcweir sal_Int32 nKeyLen = strtol( pStartPtr, &pEndPtr, 16 );
60cdf0e10cSrcweir if( pEndPtr == pStartPtr )
61cdf0e10cSrcweir return bSuccess;
62cdf0e10cSrcweir riPos += (pEndPtr - pStartPtr) + 1;
63cdf0e10cSrcweir
64cdf0e10cSrcweir const char* pKeySrc = pData + riPos;
65cdf0e10cSrcweir rValue.copyToBuffer( pKeySrc, nKeyLen );
66cdf0e10cSrcweir riPos += nKeyLen + 1;
67cdf0e10cSrcweir
68cdf0e10cSrcweir bSuccess = true;
69cdf0e10cSrcweir return bSuccess;
70cdf0e10cSrcweir }
71cdf0e10cSrcweir
createHashMap(bool bOptimizeForPerformance)72*70e197d9SHerbert Dürr void Hdf::createHashMap( bool bOptimizeForPerformance )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir releaseHashMap();
75cdf0e10cSrcweir if( bOptimizeForPerformance )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir if( m_pStringToDataMap != NULL )
78cdf0e10cSrcweir return;
79cdf0e10cSrcweir m_pStringToDataMap = new StringToDataMap();
80cdf0e10cSrcweir }
81cdf0e10cSrcweir else
82cdf0e10cSrcweir {
83cdf0e10cSrcweir if( m_pStringToValPosMap != NULL )
84cdf0e10cSrcweir return;
85cdf0e10cSrcweir m_pStringToValPosMap = new StringToValPosMap();
86cdf0e10cSrcweir }
87cdf0e10cSrcweir
88cdf0e10cSrcweir Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
89cdf0e10cSrcweir if( xIn.is() )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir Sequence< sal_Int8 > aData;
92cdf0e10cSrcweir sal_Int32 nSize = m_xSFA->getSize( m_aFileURL );
93cdf0e10cSrcweir sal_Int32 nRead = xIn->readBytes( aData, nSize );
94cdf0e10cSrcweir
95cdf0e10cSrcweir const char* pData = (const char*)aData.getConstArray();
96cdf0e10cSrcweir int iPos = 0;
97cdf0e10cSrcweir while( iPos < nRead )
98cdf0e10cSrcweir {
99*70e197d9SHerbert Dürr HDFData aDBKey;
100cdf0e10cSrcweir if( !implReadLenAndData( pData, iPos, aDBKey ) )
101cdf0e10cSrcweir break;
102cdf0e10cSrcweir
103cdf0e10cSrcweir rtl::OString aOKeyStr = aDBKey.getData();
104cdf0e10cSrcweir
105cdf0e10cSrcweir // Read val len
106cdf0e10cSrcweir const char* pStartPtr = pData + iPos;
107cdf0e10cSrcweir char* pEndPtr;
108cdf0e10cSrcweir sal_Int32 nValLen = strtol( pStartPtr, &pEndPtr, 16 );
109cdf0e10cSrcweir if( pEndPtr == pStartPtr )
110cdf0e10cSrcweir break;
111cdf0e10cSrcweir
112cdf0e10cSrcweir iPos += (pEndPtr - pStartPtr) + 1;
113cdf0e10cSrcweir
114cdf0e10cSrcweir if( bOptimizeForPerformance )
115cdf0e10cSrcweir {
116cdf0e10cSrcweir const char* pValSrc = pData + iPos;
117cdf0e10cSrcweir rtl::OString aValStr( pValSrc, nValLen );
118cdf0e10cSrcweir (*m_pStringToDataMap)[aOKeyStr] = aValStr;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir else
121cdf0e10cSrcweir {
122cdf0e10cSrcweir // store value start position
123cdf0e10cSrcweir (*m_pStringToValPosMap)[aOKeyStr] = std::pair<int,int>( iPos, nValLen );
124cdf0e10cSrcweir }
125cdf0e10cSrcweir iPos += nValLen + 1;
126cdf0e10cSrcweir }
127cdf0e10cSrcweir
128cdf0e10cSrcweir xIn->closeInput();
129cdf0e10cSrcweir }
130cdf0e10cSrcweir }
131cdf0e10cSrcweir
releaseHashMap(void)132*70e197d9SHerbert Dürr void Hdf::releaseHashMap( void )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir if( m_pStringToDataMap != NULL )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir delete m_pStringToDataMap;
137cdf0e10cSrcweir m_pStringToDataMap = NULL;
138cdf0e10cSrcweir }
139cdf0e10cSrcweir if( m_pStringToValPosMap != NULL )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir delete m_pStringToValPosMap;
142cdf0e10cSrcweir m_pStringToValPosMap = NULL;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir }
145cdf0e10cSrcweir
146cdf0e10cSrcweir
getValueForKey(const rtl::OString & rKey,HDFData & rValue)147*70e197d9SHerbert Dürr bool Hdf::getValueForKey( const rtl::OString& rKey, HDFData& rValue )
148cdf0e10cSrcweir {
149cdf0e10cSrcweir bool bSuccess = false;
150cdf0e10cSrcweir if( !m_xSFA.is() )
151cdf0e10cSrcweir return bSuccess;
152cdf0e10cSrcweir
153cdf0e10cSrcweir try
154cdf0e10cSrcweir {
155cdf0e10cSrcweir
156cdf0e10cSrcweir if( m_pStringToDataMap == NULL && m_pStringToValPosMap == NULL )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir bool bOptimizeForPerformance = false;
159cdf0e10cSrcweir createHashMap( bOptimizeForPerformance );
160cdf0e10cSrcweir }
161cdf0e10cSrcweir
162cdf0e10cSrcweir if( m_pStringToValPosMap != NULL )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir StringToValPosMap::const_iterator it = m_pStringToValPosMap->find( rKey );
165cdf0e10cSrcweir if( it != m_pStringToValPosMap->end() )
166cdf0e10cSrcweir {
167cdf0e10cSrcweir const std::pair<int,int>& rValPair = it->second;
168cdf0e10cSrcweir int iValuePos = rValPair.first;
169cdf0e10cSrcweir int nValueLen = rValPair.second;
170cdf0e10cSrcweir
171cdf0e10cSrcweir Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
172cdf0e10cSrcweir if( xIn.is() )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir Reference< XSeekable > xXSeekable( xIn, UNO_QUERY );
175cdf0e10cSrcweir if( xXSeekable.is() )
176cdf0e10cSrcweir {
177cdf0e10cSrcweir xXSeekable->seek( iValuePos );
178cdf0e10cSrcweir
179cdf0e10cSrcweir Sequence< sal_Int8 > aData;
180cdf0e10cSrcweir sal_Int32 nRead = xIn->readBytes( aData, nValueLen );
181cdf0e10cSrcweir if( nRead == nValueLen )
182cdf0e10cSrcweir {
183cdf0e10cSrcweir const char* pData = (const sal_Char*)aData.getConstArray();
184cdf0e10cSrcweir rValue.copyToBuffer( pData, nValueLen );
185cdf0e10cSrcweir bSuccess = true;
186cdf0e10cSrcweir }
187cdf0e10cSrcweir }
188cdf0e10cSrcweir xIn->closeInput();
189cdf0e10cSrcweir }
190cdf0e10cSrcweir }
191cdf0e10cSrcweir }
192cdf0e10cSrcweir
193cdf0e10cSrcweir else if( m_pStringToDataMap != NULL )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir StringToDataMap::const_iterator it = m_pStringToDataMap->find( rKey );
196cdf0e10cSrcweir if( it != m_pStringToDataMap->end() )
197cdf0e10cSrcweir {
198cdf0e10cSrcweir const rtl::OString& rValueStr = it->second;
199cdf0e10cSrcweir int nValueLen = rValueStr.getLength();
200cdf0e10cSrcweir const char* pData = rValueStr.getStr();
201cdf0e10cSrcweir rValue.copyToBuffer( pData, nValueLen );
202cdf0e10cSrcweir bSuccess = true;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
206cdf0e10cSrcweir }
207cdf0e10cSrcweir catch( Exception & )
208cdf0e10cSrcweir {
209cdf0e10cSrcweir bSuccess = false;
210cdf0e10cSrcweir }
211cdf0e10cSrcweir
212cdf0e10cSrcweir return bSuccess;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir
startIteration(void)215*70e197d9SHerbert Dürr bool Hdf::startIteration( void )
216cdf0e10cSrcweir {
217cdf0e10cSrcweir bool bSuccess = false;
218cdf0e10cSrcweir
219cdf0e10cSrcweir sal_Int32 nSize = m_xSFA->getSize( m_aFileURL );
220cdf0e10cSrcweir
221cdf0e10cSrcweir Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
222cdf0e10cSrcweir if( xIn.is() )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir m_nItRead = xIn->readBytes( m_aItData, nSize );
225cdf0e10cSrcweir if( m_nItRead == nSize )
226cdf0e10cSrcweir {
227cdf0e10cSrcweir bSuccess = true;
228cdf0e10cSrcweir m_pItData = (const char*)m_aItData.getConstArray();
229cdf0e10cSrcweir m_iItPos = 0;
230cdf0e10cSrcweir }
231cdf0e10cSrcweir else
232cdf0e10cSrcweir {
233cdf0e10cSrcweir stopIteration();
234cdf0e10cSrcweir }
235cdf0e10cSrcweir }
236cdf0e10cSrcweir
237cdf0e10cSrcweir return bSuccess;
238cdf0e10cSrcweir }
239cdf0e10cSrcweir
getNextKeyAndValue(HDFData & rKey,HDFData & rValue)240*70e197d9SHerbert Dürr bool Hdf::getNextKeyAndValue( HDFData& rKey, HDFData& rValue )
241cdf0e10cSrcweir {
242cdf0e10cSrcweir bool bSuccess = false;
243cdf0e10cSrcweir
244cdf0e10cSrcweir if( m_iItPos < m_nItRead )
245cdf0e10cSrcweir {
246cdf0e10cSrcweir if( implReadLenAndData( m_pItData, m_iItPos, rKey ) )
247cdf0e10cSrcweir {
248cdf0e10cSrcweir if( implReadLenAndData( m_pItData, m_iItPos, rValue ) )
249cdf0e10cSrcweir bSuccess = true;
250cdf0e10cSrcweir }
251cdf0e10cSrcweir }
252cdf0e10cSrcweir
253cdf0e10cSrcweir return bSuccess;
254cdf0e10cSrcweir }
255cdf0e10cSrcweir
stopIteration(void)256*70e197d9SHerbert Dürr void Hdf::stopIteration( void )
257cdf0e10cSrcweir {
258cdf0e10cSrcweir m_aItData = Sequence<sal_Int8>();
259cdf0e10cSrcweir m_pItData = NULL;
260cdf0e10cSrcweir m_nItRead = -1;
261cdf0e10cSrcweir m_iItPos = -1;
262cdf0e10cSrcweir }
263*70e197d9SHerbert Dürr } // end of namespace helpdatafileproxy
264