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 .
20 #ifndef INCLUDED_SC_SOURCE_FILTER_INC_FTOOLS_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_FTOOLS_HXX
26 #include <sal/macros.h>
27 #include <sot/storage.hxx>
28 #include <boost/noncopyable.hpp>
29 #include <boost/shared_ptr.hpp>
30 #include <oox/helper/helper.hxx>
34 // Common macros ==============================================================
36 // items and item sets --------------------------------------------------------
38 /** Expands to the item (with type 'itemtype') with Which-ID 'which'. */
39 #define GETITEM( itemset, itemtype, which ) \
40 static_cast< const itemtype & >( (itemset).Get( which ) )
42 /** Expands to the value (with type 'valuetype') of the item with Which-ID 'which'. */
43 #define GETITEMVALUE( itemset, itemtype, which, valuetype ) \
44 static_cast< valuetype >( GETITEM( itemset, itemtype, which ).GetValue() )
46 /** Expands to the value of the SfxBoolItem with Which-ID 'which'. */
47 #define GETITEMBOOL( itemset, which ) \
48 GETITEMVALUE( itemset, SfxBoolItem, which, bool )
50 // Global static helpers ======================================================
52 // Value range limit helpers --------------------------------------------------
54 /** Returns the value, if it is not less than nMin, otherwise nMin. */
55 template< typename ReturnType
, typename Type
>
56 inline ReturnType
llimit_cast( Type nValue
, ReturnType nMin
)
57 { return static_cast< ReturnType
>( ::std::max
< Type
>( nValue
, nMin
) ); }
59 /** Returns the value, if it fits into ReturnType, otherwise the minimum value of ReturnType. */
60 template< typename ReturnType
, typename Type
>
61 inline ReturnType
llimit_cast( Type nValue
)
62 { return llimit_cast( nValue
, ::std::numeric_limits
< ReturnType
>::min() ); }
64 /** Returns the value, if it is not greater than nMax, otherwise nMax. */
65 template< typename ReturnType
, typename Type
>
66 inline ReturnType
ulimit_cast( Type nValue
, ReturnType nMax
)
67 { return static_cast< ReturnType
>( ::std::min
< Type
>( nValue
, nMax
) ); }
69 /** Returns the value, if it fits into ReturnType, otherwise the maximum value of ReturnType. */
70 template< typename ReturnType
, typename Type
>
71 inline ReturnType
ulimit_cast( Type nValue
)
72 { return ulimit_cast( nValue
, ::std::numeric_limits
< ReturnType
>::max() ); }
74 /** Returns the value, if it is not less than nMin and not greater than nMax, otherwise one of the limits. */
75 template< typename ReturnType
, typename Type
>
76 inline ReturnType
limit_cast( Type nValue
, ReturnType nMin
, ReturnType nMax
)
77 { return static_cast< ReturnType
>( ::std::max
< Type
>( ::std::min
< Type
>( nValue
, nMax
), nMin
) ); }
79 /** Returns the value, if it fits into ReturnType, otherwise one of the limits of ReturnType. */
80 template< typename ReturnType
, typename Type
>
81 inline ReturnType
limit_cast( Type nValue
)
82 { return limit_cast( nValue
, ::std::numeric_limits
< ReturnType
>::min(), ::std::numeric_limits
< ReturnType
>::max() ); }
84 // Read from bitfields --------------------------------------------------------
86 /** Returns true, if at least one of the bits set in nMask is set in nBitField. */
87 template< typename Type
>
88 inline bool get_flag( Type nBitField
, Type nMask
)
89 { return (nBitField
& nMask
) != 0; }
91 /** Returns nSet, if at least one bit of nMask is set in nBitField, otherwise nUnset. */
92 template< typename ReturnType
, typename Type
>
93 inline ReturnType
get_flagvalue( Type nBitField
, Type nMask
, ReturnType nSet
, ReturnType nUnset
)
94 { return ::get_flag( nBitField
, nMask
) ? nSet
: nUnset
; }
96 /** Extracts a value from a bit field.
97 @descr Returns in rnRet the data fragment from nBitField, that starts at bit nStartBit
98 (0-based, bit 0 is rightmost) with the width of nBitCount. rnRet will be right-aligned (normalized).
99 For instance: extract_value( n, 0x4321, 8, 4 ) stores 3 in n (value in bits 8-11). */
100 template< typename ReturnType
, typename Type
>
101 inline ReturnType
extract_value( Type nBitField
, sal_uInt8 nStartBit
, sal_uInt8 nBitCount
)
102 { return static_cast< ReturnType
>( ((1UL << nBitCount
) - 1) & (nBitField
>> nStartBit
) ); }
104 // Write to bitfields ---------------------------------------------------------
106 /** Sets or clears (according to bSet) all set bits of nMask in rnBitField. */
107 template< typename Type
>
108 inline void set_flag( Type
& rnBitField
, Type nMask
, bool bSet
= true )
109 { if( bSet
) rnBitField
|= nMask
; else rnBitField
&= ~nMask
; }
111 /** Inserts a value into a bitfield.
112 @descr Inserts the lower nBitCount bits of nValue into rnBitField, starting
113 there at bit nStartBit. Other contents of rnBitField keep unchanged. */
114 template< typename Type
, typename InsertType
>
115 void insert_value( Type
& rnBitField
, InsertType nValue
, sal_uInt8 nStartBit
, sal_uInt8 nBitCount
)
117 unsigned long nMask
= ((1UL << nBitCount
) - 1);
118 Type nNewValue
= static_cast< Type
>( nValue
& nMask
);
119 (rnBitField
&= ~(nMask
<< nStartBit
)) |= (nNewValue
<< nStartBit
);
126 class ScStyleSheetPool
;
129 /** Contains static methods used anywhere in the filters. */
130 class ScfTools
: boost::noncopyable
134 // *** common methods *** -----------------------------------------------------
136 /** Reads a 10-byte-long-double and converts it to double. */
137 static double ReadLongDouble( SvStream
& rStrm
);
138 /** Returns system text encoding for byte string conversion. */
139 static rtl_TextEncoding
GetSystemTextEncoding();
140 /** Returns a string representing the hexadecimal value of nValue. */
141 static OUString
GetHexStr( sal_uInt16 nValue
);
143 /** Mixes RGB components with given transparence.
144 @param nTrans Foreground transparence (0x00 == full nFore ... 0x80 = full nBack). */
145 static sal_uInt8
GetMixedColorComp( sal_uInt8 nFore
, sal_uInt8 nBack
, sal_uInt8 nTrans
);
146 /** Mixes colors with given transparence.
147 @param nTrans Foreground transparence (0x00 == full rFore ... 0x80 = full rBack). */
148 static Color
GetMixedColor( const Color
& rFore
, const Color
& rBack
, sal_uInt8 nTrans
);
150 // *** conversion of names *** ------------------------------------------------
152 /** Converts a string to a valid Calc defined name or database range name.
153 @descr Defined names in Calc may contain letters, digits (*), underscores, periods (*),
154 colons (*), question marks, and dollar signs.
155 (*) = not allowed at first position. */
156 static OUString
ConvertToScDefinedName( const OUString
& rName
);
158 // *** streams and storages *** -----------------------------------------------
160 /** Tries to open an existing storage with the specified name in the passed storage (read-only). */
161 static tools::SvRef
<SotStorage
> OpenStorageRead( tools::SvRef
<SotStorage
> xStrg
, const OUString
& rStrgName
);
162 /** Creates and opens a storage with the specified name in the passed storage (read/write). */
163 static tools::SvRef
<SotStorage
> OpenStorageWrite( tools::SvRef
<SotStorage
> xStrg
, const OUString
& rStrgName
);
165 /** Tries to open an existing stream with the specified name in the passed storage (read-only). */
166 static tools::SvRef
<SotStorageStream
> OpenStorageStreamRead( tools::SvRef
<SotStorage
> xStrg
, const OUString
& rStrmName
);
167 /** Creates and opens a stream with the specified name in the passed storage (read/write). */
168 static tools::SvRef
<SotStorageStream
> OpenStorageStreamWrite( tools::SvRef
<SotStorage
> xStrg
, const OUString
& rStrmName
);
170 // *** item handling *** ------------------------------------------------------
172 /** Returns true, if the passed item set contains the item.
173 @param bDeep true = Searches in parent item sets too. */
174 static bool CheckItem( const SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
, bool bDeep
);
175 /** Returns true, if the passed item set contains at least one of the items.
176 @param pnWhichIds Zero-terminated array of Which-IDs.
177 @param bDeep true = Searches in parent item sets too. */
178 static bool CheckItems( const SfxItemSet
& rItemSet
, const sal_uInt16
* pnWhichIds
, bool bDeep
);
180 /** Puts the item into the passed item set.
181 @descr The item will be put into the item set, if bSkipPoolDef is false,
182 or if the item differs from the default pool item.
183 @param rItemSet The destination item set.
184 @param rItem The item to put into the item set.
185 @param nWhichId The Which-ID to set with the item.
186 @param bSkipPoolDef true = Do not put item if it is equal to pool default; false = Always put the item. */
188 SfxItemSet
& rItemSet
, const SfxPoolItem
& rItem
,
189 sal_uInt16 nWhichId
, bool bSkipPoolDef
);
191 /** Puts the item into the passed item set.
192 @descr The item will be put into the item set, if bSkipPoolDef is false,
193 or if the item differs from the default pool item.
194 @param rItemSet The destination item set.
195 @param rItem The item to put into the item set.
196 @param bSkipPoolDef true = Do not put item if it is equal to pool default; false = Always put the item. */
197 static void PutItem( SfxItemSet
& rItemSet
, const SfxPoolItem
& rItem
, bool bSkipPoolDef
);
199 // *** style sheet handling *** -----------------------------------------------
201 /** Creates and returns a cell style sheet and inserts it into the pool.
202 @descr If the style sheet is already in the pool, another unused style name is used.
203 @param bForceName Controls behaviour, if the style already exists:
204 true = Old existing style will be renamed; false = New style gets another name. */
205 static ScStyleSheet
& MakeCellStyleSheet(
206 ScStyleSheetPool
& rPool
,
207 const OUString
& rStyleName
, bool bForceName
);
208 /** Creates and returns a page style sheet and inserts it into the pool.
209 @descr If the style sheet is already in the pool, another unused style name is used.
210 @param bForceName Controls behaviour, if the style already exists:
211 true = Old existing style will be renamed; false = New style gets another name. */
212 static ScStyleSheet
& MakePageStyleSheet(
213 ScStyleSheetPool
& rPool
,
214 const OUString
& rStyleName
, bool bForceName
);
216 // *** byte string import operations *** --------------------------------------
218 /** Reads and returns a zero terminated byte string and decreases a stream counter. */
219 static OString
read_zeroTerminated_uInt8s_ToOString(SvStream
& rStrm
, sal_Int32
& rnBytesLeft
);
220 /** Reads and returns a zero terminated byte string and decreases a stream counter. */
221 inline static OUString
read_zeroTerminated_uInt8s_ToOUString(SvStream
& rStrm
, sal_Int32
& rnBytesLeft
, rtl_TextEncoding eTextEnc
)
223 return OStringToOUString(read_zeroTerminated_uInt8s_ToOString(rStrm
, rnBytesLeft
), eTextEnc
);
226 /** Appends a zero terminated byte string. */
227 static void AppendCString( SvStream
& rStrm
, OUString
& rString
, rtl_TextEncoding eTextEnc
);
229 // *** HTML table names <-> named range names *** -----------------------------
231 /** Returns the built-in range name for an HTML document. */
232 static const OUString
& GetHTMLDocName();
233 /** Returns the built-in range name for all HTML tables. */
234 static const OUString
& GetHTMLTablesName();
235 /** Returns the built-in range name for an HTML table, specified by table index. */
236 static OUString
GetNameFromHTMLIndex( sal_uInt32 nIndex
);
237 /** Returns the built-in range name for an HTML table, specified by table name. */
238 static OUString
GetNameFromHTMLName( const OUString
& rTabName
);
240 /** Returns true, if rSource is the built-in range name for an HTML document. */
241 static bool IsHTMLDocName( const OUString
& rSource
);
242 /** Returns true, if rSource is the built-in range name for all HTML tables. */
243 static bool IsHTMLTablesName( const OUString
& rSource
);
244 /** Converts a built-in range name to an HTML table name.
245 @param rSource The string to be determined.
246 @param rName The HTML table name.
247 @return true, if conversion was successful. */
248 static bool GetHTMLNameFromName( const OUString
& rSource
, OUString
& rName
);
251 /** Returns the prefix for table index names. */
252 static const OUString
& GetHTMLIndexPrefix();
253 /** Returns the prefix for table names. */
254 static const OUString
& GetHTMLNamePrefix();
255 /** We don't want anybody to instantiate this class, since it is just a
256 collection of static items. To enforce this, the default constructor
261 // Containers =================================================================
263 typedef ::std::vector
< sal_uInt8
> ScfUInt8Vec
;
264 typedef ::std::vector
< sal_Int16
> ScfInt16Vec
;
265 typedef ::std::vector
< sal_uInt16
> ScfUInt16Vec
;
266 typedef ::std::vector
< sal_Int32
> ScfInt32Vec
;
267 typedef ::std::vector
< sal_uInt32
> ScfUInt32Vec
;
268 typedef ::std::vector
< OUString
> ScfStringVec
;
270 class ScFormatFilterPluginImpl
: public ScFormatFilterPlugin
273 ScFormatFilterPluginImpl();
274 virtual ~ScFormatFilterPluginImpl();
275 // various import filters
276 virtual FltError
ScImportLotus123( SfxMedium
&, ScDocument
*, rtl_TextEncoding eSrc
= RTL_TEXTENCODING_DONTKNOW
) SAL_OVERRIDE
;
277 virtual FltError
ScImportQuattroPro( SfxMedium
&rMedium
, ScDocument
*pDoc
) SAL_OVERRIDE
;
278 virtual FltError
ScImportExcel( SfxMedium
&, ScDocument
*, const EXCIMPFORMAT
) SAL_OVERRIDE
;
279 // eFormat == EIF_AUTO -> passender Filter wird automatisch verwendet
280 // eFormat == EIF_BIFF5 -> nur Biff5-Stream fuehrt zum Erfolg (auch wenn in einem Excel97-Doc)
281 // eFormat == EIF_BIFF8 -> nur Biff8-Stream fuehrt zum Erfolg (nur in Excel97-Docs)
282 // eFormat == EIF_BIFF_LE4 -> nur Nicht-Storage-Dateien _koennen_ zum Erfolg fuehren
283 virtual FltError
ScImportStarCalc10( SvStream
&, ScDocument
* ) SAL_OVERRIDE
;
284 virtual FltError
ScImportDif( SvStream
&, ScDocument
*, const ScAddress
& rInsPos
,
285 const rtl_TextEncoding eSrc
= RTL_TEXTENCODING_DONTKNOW
, sal_uInt32 nDifOption
= SC_DIFOPT_EXCEL
) SAL_OVERRIDE
;
286 virtual FltError
ScImportRTF( SvStream
&, const OUString
& rBaseURL
, ScDocument
*, ScRange
& rRange
) SAL_OVERRIDE
;
287 virtual FltError
ScImportHTML( SvStream
&, const OUString
& rBaseURL
, ScDocument
*, ScRange
& rRange
,
288 double nOutputFactor
= 1.0, bool bCalcWidthHeight
= true,
289 SvNumberFormatter
* pFormatter
= NULL
, bool bConvertDate
= true ) SAL_OVERRIDE
;
291 virtual ScEEAbsImport
*CreateRTFImport( ScDocument
* pDoc
, const ScRange
& rRange
) SAL_OVERRIDE
;
292 virtual ScEEAbsImport
*CreateHTMLImport( ScDocument
* pDocP
, const OUString
& rBaseURL
, const ScRange
& rRange
, bool bCalcWidthHeight
) SAL_OVERRIDE
;
293 virtual OUString
GetHTMLRangeNameList( ScDocument
* pDoc
, const OUString
& rOrigName
) SAL_OVERRIDE
;
295 // various export filters
296 virtual FltError
ScExportExcel5( SfxMedium
&, ScDocument
*, ExportFormatExcel eFormat
, rtl_TextEncoding eDest
) SAL_OVERRIDE
;
297 virtual FltError
ScExportDif( SvStream
&, ScDocument
*, const ScAddress
& rOutPos
, const rtl_TextEncoding eDest
,
298 sal_uInt32 nDifOption
= SC_DIFOPT_EXCEL
) SAL_OVERRIDE
;
299 virtual FltError
ScExportDif( SvStream
&, ScDocument
*, const ScRange
& rRange
, const rtl_TextEncoding eDest
,
300 sal_uInt32 nDifOption
= SC_DIFOPT_EXCEL
) SAL_OVERRIDE
;
301 virtual FltError
ScExportHTML( SvStream
&, const OUString
& rBaseURL
, ScDocument
*, const ScRange
& rRange
, const rtl_TextEncoding eDest
, bool bAll
,
302 const OUString
& rStreamPath
, OUString
& rNonConvertibleChars
, const OUString
& rFilterOptions
) SAL_OVERRIDE
;
303 virtual FltError
ScExportRTF( SvStream
&, ScDocument
*, const ScRange
& rRange
, const rtl_TextEncoding eDest
) SAL_OVERRIDE
;
305 virtual ScOrcusFilters
* GetOrcusFilters() SAL_OVERRIDE
;
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */