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