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