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