Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef SC_XERECORD_HXX
30 : : #define SC_XERECORD_HXX
31 : :
32 : : #include "xlconst.hxx"
33 : : #include "xestream.hxx"
34 : : #include <boost/shared_ptr.hpp>
35 : :
36 : : // Base classes to export Excel records =======================================
37 : :
38 : : /** Base class for all Excel records.
39 : :
40 : : Derive from this class to implement any functionality performed during
41 : : saving the records - except really writing a record (i.e. write a list of
42 : : records contained in the class). Derive from XclExpRecord (instead from
43 : : this class) to write common records.
44 : : */
45 : 0 : class XclExpRecordBase
46 : : {
47 : : public:
48 : : virtual ~XclExpRecordBase();
49 : :
50 : : /** Overwrite this method to do any operation while saving the record. */
51 : : virtual void Save( XclExpStream& rStrm );
52 : : virtual void SaveXml( XclExpXmlStream& rStrm );
53 : : };
54 : :
55 : : // ----------------------------------------------------------------------------
56 : :
57 : : class XclExpDelegatingRecord : public XclExpRecordBase
58 : : {
59 : : public:
60 : : XclExpDelegatingRecord( XclExpRecordBase* pRecord );
61 : : ~XclExpDelegatingRecord();
62 : :
63 : : virtual void SaveXml( XclExpXmlStream& rStrm );
64 : : private:
65 : : XclExpRecordBase* mpRecord;
66 : : };
67 : :
68 : : // ----------------------------------------------------------------------------
69 : :
70 : : class XclExpXmlElementRecord : public XclExpRecordBase
71 : : {
72 : : public:
73 : : XclExpXmlElementRecord( sal_Int32 nElement, void (*pAttributes)( XclExpXmlStream& rStrm) = NULL );
74 : : virtual ~XclExpXmlElementRecord();
75 : :
76 : : protected:
77 : : sal_Int32 mnElement;
78 : : void (*mpAttributes)( XclExpXmlStream& rStrm );
79 : : };
80 : :
81 : : // ----------------------------------------------------------------------------
82 : :
83 : : class XclExpXmlStartElementRecord : public XclExpXmlElementRecord
84 : : {
85 : : public:
86 : : XclExpXmlStartElementRecord( sal_Int32 nElement, void (*pAttributes)( XclExpXmlStream& rStrm) = NULL );
87 : : virtual ~XclExpXmlStartElementRecord();
88 : :
89 : : /** Starts the element nElement */
90 : : virtual void SaveXml( XclExpXmlStream& rStrm );
91 : : };
92 : :
93 : : // ----------------------------------------------------------------------------
94 : :
95 : : class XclExpXmlEndElementRecord : public XclExpXmlElementRecord
96 : : {
97 : : public:
98 : : XclExpXmlEndElementRecord( sal_Int32 nElement );
99 : : virtual ~XclExpXmlEndElementRecord();
100 : :
101 : : /** Ends the element nElement */
102 : : virtual void SaveXml( XclExpXmlStream& rStrm );
103 : : };
104 : :
105 : : // ----------------------------------------------------------------------------
106 : :
107 : : class XclExpXmlStartSingleElementRecord : public XclExpXmlElementRecord
108 : : {
109 : : public:
110 : : XclExpXmlStartSingleElementRecord( sal_Int32 nElement, void (*pAttributes)( XclExpXmlStream& rStrm) = NULL );
111 : : virtual ~XclExpXmlStartSingleElementRecord();
112 : :
113 : : /** Starts the single element nElement */
114 : : virtual void SaveXml( XclExpXmlStream& rStrm );
115 : : };
116 : :
117 : : // ----------------------------------------------------------------------------
118 : :
119 : : class XclExpXmlEndSingleElementRecord : public XclExpRecordBase
120 : : {
121 : : public:
122 : : XclExpXmlEndSingleElementRecord();
123 : : virtual ~XclExpXmlEndSingleElementRecord();
124 : :
125 : : /** Ends the single element nElement */
126 : : virtual void SaveXml( XclExpXmlStream& rStrm );
127 : : };
128 : :
129 : : // ----------------------------------------------------------------------------
130 : :
131 : : /** Base class for single records with any content.
132 : :
133 : : This class handles writing the record header. Derived classes only have to
134 : : write the record body. Calculating the record size before saving optimizes
135 : : the write process (the stream does not have to seek back and update the
136 : : written record size). But it is not required to calculate a valid size
137 : : (maybe it would be too complex or just impossible until the record is
138 : : really written).
139 : : */
140 : 0 : class XclExpRecord : public XclExpRecordBase
141 : : {
142 : : public:
143 : : /** @param nRecId The record ID of this record. May be set later with SetRecId().
144 : : @param nRecSize The predicted record size. May be set later with SetRecSize(). */
145 : : explicit XclExpRecord(
146 : : sal_uInt16 nRecId = EXC_ID_UNKNOWN,
147 : : sal_Size nRecSize = 0 );
148 : :
149 : : virtual ~XclExpRecord();
150 : :
151 : : /** Returns the current record ID. */
152 : 0 : inline sal_uInt16 GetRecId() const { return mnRecId; }
153 : : /** Returns the current record size prediction. */
154 : 0 : inline sal_Size GetRecSize() const { return mnRecSize; }
155 : :
156 : : /** Sets a new record ID. */
157 : 0 : inline void SetRecId( sal_uInt16 nRecId ) { mnRecId = nRecId; }
158 : : /** Sets a new record size prediction. */
159 : 0 : inline void SetRecSize( sal_Size nRecSize ) { mnRecSize = nRecSize; }
160 : : /** Adds a size value to the record size prediction. */
161 : 0 : inline void AddRecSize( sal_Size nRecSize ) { mnRecSize += nRecSize; }
162 : : /** Sets record ID and size with one call. */
163 : : void SetRecHeader( sal_uInt16 nRecId, sal_Size nRecSize );
164 : :
165 : : /** Writes the record header and calls WriteBody(). */
166 : : virtual void Save( XclExpStream& rStrm );
167 : :
168 : : protected:
169 : : /** Writes the body of the record (without record header).
170 : : @descr Usually this method will be overwritten by derived classes. */
171 : : virtual void WriteBody( XclExpStream& rStrm );
172 : :
173 : : private:
174 : : sal_Size mnRecSize; /// The predicted record size.
175 : : sal_uInt16 mnRecId; /// The record ID.
176 : : };
177 : :
178 : : // ----------------------------------------------------------------------------
179 : :
180 : : /** A record without body. Only the record ID and the size 0 will be written. */
181 [ # # ]: 0 : class XclExpEmptyRecord : public XclExpRecord
182 : : {
183 : : public:
184 : : /** @param nRecId The record ID of this record. */
185 : : inline explicit XclExpEmptyRecord( sal_uInt16 nRecId );
186 : : };
187 : :
188 : 0 : inline XclExpEmptyRecord::XclExpEmptyRecord( sal_uInt16 nRecId ) :
189 : 0 : XclExpRecord( nRecId, 0 )
190 : : {
191 : 0 : }
192 : :
193 : : // ============================================================================
194 : :
195 : : /** A record with a single value of type Type.
196 : : @descr Requires operator<<( XclExpStream&, const Type& ). */
197 : : template< typename Type >
198 [ # # ][ # # ]: 0 : class XclExpValueRecord : public XclExpRecord
199 : : {
200 : : public:
201 : : /** @param nRecId The record ID of this record.
202 : : @param rValue The value for the record body.
203 : : @param nSize Record size. Uses sizeof( Type ), if this parameter is omitted. */
204 : 0 : inline explicit XclExpValueRecord( sal_uInt16 nRecId, const Type& rValue, sal_Size nSize = sizeof( Type ) ) :
205 : 0 : XclExpRecord( nRecId, nSize ), maValue( rValue ), mnAttribute( -1 ) {}
206 : :
207 : : /** Returns the value of the record. */
208 : 0 : inline const Type& GetValue() const { return maValue; }
209 : : /** Sets a new record value. */
210 : 0 : inline void SetValue( const Type& rValue ) { maValue = rValue; }
211 : :
212 : : /** Sets the OOXML attribute this record corresponds to */
213 : : XclExpValueRecord* SetAttribute( sal_Int32 nId );
214 : :
215 : : /** Write the OOXML attribute and its value */
216 : : void SaveXml( XclExpXmlStream& rStrm );
217 : :
218 : : private:
219 : : /** Writes the body of the record. */
220 : 0 : inline virtual void WriteBody( XclExpStream& rStrm ) { rStrm << maValue; }
221 : : // inlining prevents warning in wntmsci10
222 : :
223 : : private:
224 : : Type maValue; /// The record data.
225 : : sal_Int32 mnAttribute; /// The OOXML attribute Id
226 : : };
227 : :
228 : : template< typename Type >
229 : 0 : void XclExpValueRecord< Type >::SaveXml( XclExpXmlStream& rStrm )
230 : : {
231 [ # # ]: 0 : if( mnAttribute == -1 )
232 : 0 : return;
233 [ # # ]: 0 : rStrm.WriteAttributes(
234 : : mnAttribute, rtl::OString::valueOf( (sal_Int32) maValue ).getStr(),
235 : : FSEND );
236 : : }
237 : :
238 : : template<>
239 : : void XclExpValueRecord<double>::SaveXml( XclExpXmlStream& rStrm );
240 : :
241 : : template< typename Type >
242 : 0 : XclExpValueRecord< Type >* XclExpValueRecord< Type >::SetAttribute( sal_Int32 nId )
243 : : {
244 : 0 : mnAttribute = nId;
245 : 0 : return this;
246 : : }
247 : :
248 : : // ----------------------------------------------------------------------------
249 : :
250 : : /** A record containing an unsigned 16-bit value. */
251 : : typedef XclExpValueRecord< sal_uInt16 > XclExpUInt16Record;
252 : :
253 : : /** A record containing an unsigned 32-bit value. */
254 : : typedef XclExpValueRecord< sal_uInt32 > XclExpUInt32Record;
255 : :
256 : : /** A record containing a double value. */
257 : : typedef XclExpValueRecord< double > XclExpDoubleRecord;
258 : :
259 : : // ----------------------------------------------------------------------------
260 : :
261 : : /** Record which contains a Boolean value.
262 : : @descr The value is stored as 16-bit value: 0x0000 = sal_False, 0x0001 = TRUE. */
263 [ # # ]: 0 : class XclExpBoolRecord : public XclExpRecord
264 : : {
265 : : public:
266 : : /** @param nRecId The record ID of this record.
267 : : @param nValue The value for the record body. */
268 : 0 : inline explicit XclExpBoolRecord( sal_uInt16 nRecId, bool bValue, sal_Int32 nAttribute = -1 ) :
269 : 0 : XclExpRecord( nRecId, 2 ), mbValue( bValue ), mnAttribute( nAttribute ) {}
270 : :
271 : : /** Returns the Boolean value of the record. */
272 : 0 : inline bool GetBool() const { return mbValue; }
273 : : /** Sets a new Boolean record value. */
274 : : inline void SetBool( bool bValue ) { mbValue = bValue; }
275 : :
276 : : virtual void SaveXml( XclExpXmlStream& rStrm );
277 : :
278 : : private:
279 : : /** Writes the body of the record. */
280 : : virtual void WriteBody( XclExpStream& rStrm );
281 : :
282 : : private:
283 : : bool mbValue; /// The record data.
284 : : sal_Int32 mnAttribute; /// The attribute to generate within SaveXml()
285 : : };
286 : :
287 : : // ----------------------------------------------------------------------------
288 : :
289 : : /** Record which exports a memory data array. */
290 [ # # ]: 0 : class XclExpDummyRecord : public XclExpRecord
291 : : {
292 : : public:
293 : : /** @param nRecId The record ID of this record.
294 : : @param pRecData Pointer to the data array representing the record body.
295 : : @param nRecSize Size of the data array. */
296 : : explicit XclExpDummyRecord(
297 : : sal_uInt16 nRecId, const void* pRecData, sal_Size nRecSize );
298 : :
299 : : /** Sets a data array. */
300 : : void SetData( const void* pRecData, sal_Size nRecSize );
301 : :
302 : : private:
303 : : /** Writes the body of the record. */
304 : : virtual void WriteBody( XclExpStream& rStrm );
305 : :
306 : : private:
307 : : const void* mpData; /// The record data.
308 : : };
309 : :
310 : : // Future records =============================================================
311 : :
312 [ # # ]: 0 : class XclExpFutureRecord : public XclExpRecord
313 : : {
314 : : public:
315 : : explicit XclExpFutureRecord( XclFutureRecType eRecType,
316 : : sal_uInt16 nRecId, sal_Size nRecSize = 0 );
317 : :
318 : : /** Writes the extended record header and calls WriteBody(). */
319 : : virtual void Save( XclExpStream& rStrm );
320 : :
321 : : private:
322 : : XclFutureRecType meRecType;
323 : : };
324 : :
325 : : // List of records ============================================================
326 : :
327 : : /** A list of Excel record objects.
328 : :
329 : : Provides saving the compete list. This class is derived from
330 : : XclExpRecordBase, so it can be used as record in another record list.
331 : : Requires RecType::Save( XclExpStream& ).
332 : : */
333 : : template< typename RecType = XclExpRecordBase >
334 [ # # ][ # # ]: 0 : class XclExpRecordList : public XclExpRecordBase
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
[ # # # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
335 : : {
336 : : public:
337 : : typedef boost::shared_ptr< RecType > RecordRefType;
338 : :
339 : 0 : inline bool IsEmpty() const { return maRecs.empty(); }
340 : 0 : inline size_t GetSize() const { return maRecs.size(); }
341 : :
342 : : /** Returns true, if the passed index points to an exiting record. */
343 : : inline bool HasRecord( size_t nPos ) const
344 : : { return nPos < maRecs.size(); }
345 : : /** Returns reference to an existing record or empty reference on error. */
346 : 0 : inline RecordRefType GetRecord( size_t nPos ) const
347 [ # # ][ # # ]: 0 : { return (nPos < maRecs.size()) ? maRecs[ nPos ] : RecordRefType(); }
[ # # ][ # # ]
[ # # ][ # # ]
348 : : /** Returns reference to the first existing record or empty reference, if list is empty. */
349 : 0 : inline RecordRefType GetFirstRecord() const
350 [ # # ]: 0 : { return maRecs.empty() ? RecordRefType() : maRecs.front(); }
351 : : /** Returns reference to the last existing record or empty reference, if list is empty. */
352 : 0 : inline RecordRefType GetLastRecord() const
353 [ # # ]: 0 : { return maRecs.empty() ? RecordRefType() : maRecs.back(); }
354 : :
355 : : /** Inserts a record at the specified position into the list. */
356 : 0 : inline void InsertRecord( RecordRefType xRec, size_t nPos )
357 [ # # ][ # # ]: 0 : { if( xRec.get() ) maRecs.insert( maRecs.begin() + ::std::min( nPos, maRecs.size() ), xRec ); }
[ # # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
358 : : /** Appends a record to the list. */
359 : 0 : inline void AppendRecord( RecordRefType xRec )
360 [ # # ][ # # ]: 0 : { if( xRec.get() ) maRecs.push_back( xRec ); }
[ # # ][ # # ]
[ # # ][ # # ]
361 : : /** Replaces the record at the specified position from the list with the passed record. */
362 : 0 : inline void ReplaceRecord( RecordRefType xRec, size_t nPos )
363 [ # # ][ # # ]: 0 : { RemoveRecord( nPos ); InsertRecord( xRec, nPos ); }
364 : :
365 : : /** Inserts a newly created record at the specified position into the list. */
366 : : inline void InsertNewRecord( RecType* pRec, size_t nPos )
367 : : { if( pRec ) InsertRecord( RecordRefType( pRec ), nPos ); }
368 : : /** Appends a newly created record to the list. */
369 : 0 : inline void AppendNewRecord( RecType* pRec )
370 [ # # ][ # # ]: 0 : { if( pRec ) AppendRecord( RecordRefType( pRec ) ); }
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
371 : : /** Replaces the record at the specified position from the list with the passed newly created record. */
372 : : inline void ReplaceNewRecord( RecType* pRec, size_t nPos )
373 : : { RemoveRecord( nPos ); InsertNewRecord( pRec, nPos ); }
374 : :
375 : : /** Removes the record at the specified position from the list. */
376 : 0 : inline void RemoveRecord( size_t nPos )
377 [ # # ]: 0 : { if( nPos < maRecs.size() ) maRecs.erase( maRecs.begin() + nPos ); }
[ # # # ]
[ # # ][ # # ]
[ # # # ]
[ # # # ]
[ # # ]
378 : : /** Removes all records from the list. */
379 : 0 : inline void RemoveAllRecords() { maRecs.clear(); }
380 : :
381 : : /** Writes the complete record list. */
382 : 0 : inline virtual void Save( XclExpStream& rStrm )
383 : : {
384 : : // inlining prevents warning in wntmsci10
385 [ # # ][ # # ]: 0 : for( typename RecordVec::iterator aIt = maRecs.begin(), aEnd = maRecs.end(); aIt != aEnd; ++aIt )
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
386 [ # # ][ # # ]: 0 : (*aIt)->Save( rStrm );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
387 : 0 : }
388 : :
389 : 0 : inline virtual void SaveXml( XclExpXmlStream& rStrm )
390 : : {
391 : : // inlining prevents warning in wntmsci10
392 [ # # ][ # # ]: 0 : for( typename RecordVec::iterator aIt = maRecs.begin(), aEnd = maRecs.end(); aIt != aEnd; ++aIt )
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
393 [ # # ][ # # ]: 0 : (*aIt)->SaveXml( rStrm );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
394 : 0 : }
395 : :
396 : : private:
397 : : typedef ::std::vector< RecordRefType > RecordVec;
398 : : RecordVec maRecs;
399 : : };
400 : :
401 : : // ============================================================================
402 : :
403 : : /** Represents a complete substream of records enclosed into a pair of BOF/EOF records. */
404 [ # # ]: 0 : class XclExpSubStream : public XclExpRecordList<>
405 : : {
406 : : public:
407 : : explicit XclExpSubStream( sal_uInt16 nSubStrmType );
408 : :
409 : : /** Writes the complete substream, including leading BOF and trailing EOF. */
410 : : virtual void Save( XclExpStream& rStrm );
411 : :
412 : : private:
413 : : sal_uInt16 mnSubStrmType; /// Substream type, stored in leading BOF record.
414 : : };
415 : :
416 : : // ============================================================================
417 : :
418 : : #endif
419 : :
420 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|