1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 #include <rtl/alloc.h>
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
)
37 m_pBuffer
= new char[m_nSize
+1];
38 memcpy( m_pBuffer
, pSrcData
, m_nSize
);
39 m_pBuffer
[m_nSize
] = 0;
45 bool Hdf::implReadLenAndData( const char* pData
, int& riPos
, HDFData
& rValue
)
47 bool bSuccess
= false;
50 const char* pStartPtr
= pData
+ riPos
;
52 sal_Int32 nKeyLen
= strtol( pStartPtr
, &pEndPtr
, 16 );
53 if( pEndPtr
== pStartPtr
)
55 riPos
+= (pEndPtr
- pStartPtr
) + 1;
57 const char* pKeySrc
= pData
+ riPos
;
58 rValue
.copyToBuffer( pKeySrc
, nKeyLen
);
65 void Hdf::createHashMap( bool bOptimizeForPerformance
)
68 if( bOptimizeForPerformance
)
70 if( m_pStringToDataMap
!= NULL
)
72 m_pStringToDataMap
= new StringToDataMap();
76 if( m_pStringToValPosMap
!= NULL
)
78 m_pStringToValPosMap
= new StringToValPosMap();
81 Reference
< XInputStream
> xIn
= m_xSFA
->openFileRead( m_aFileURL
);
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());
93 if( !implReadLenAndData( pData
, iPos
, aDBKey
) )
96 OString aOKeyStr
= aDBKey
.getData();
99 const char* pStartPtr
= pData
+ iPos
;
101 sal_Int32 nValLen
= strtol( pStartPtr
, &pEndPtr
, 16 );
102 if( pEndPtr
== pStartPtr
)
105 iPos
+= (pEndPtr
- pStartPtr
) + 1;
107 if( bOptimizeForPerformance
)
109 const char* pValSrc
= pData
+ iPos
;
110 OString
aValStr( pValSrc
, nValLen
);
111 (*m_pStringToDataMap
)[aOKeyStr
] = aValStr
;
115 // store value start position
116 (*m_pStringToValPosMap
)[aOKeyStr
] = std::pair
<int,int>( iPos
, nValLen
);
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;
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
);
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
);
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
);
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
);
217 m_nItRead
= xIn
->readBytes( m_aItData
, nSize
);
218 if( m_nItRead
== nSize
)
221 m_pItData
= reinterpret_cast<const char*>(m_aItData
.getConstArray());
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
) )
249 void Hdf::stopIteration()
251 m_aItData
= Sequence
<sal_Int8
>();
257 } // end of namespace helpdatafileproxy
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */