LibreOffice Module sc (master)  1
ftools.hxx
Go to the documentation of this file.
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 INCLUDED_SC_SOURCE_FILTER_INC_FTOOLS_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_FTOOLS_HXX
22 
23 #include <algorithm>
24 #include <vector>
25 #include <limits>
26 #include <string_view>
27 
28 #include <tools/ref.hxx>
29 #include <filter.hxx>
30 
31 // Common macros ==============================================================
32 
33 // items and item sets --------------------------------------------------------
34 
36 #define GETITEM( itemset, itemtype, which ) \
37  static_cast< const itemtype & >( (itemset).Get( which ) )
38 
40 #define GETITEMBOOL( itemset, which ) \
41  (static_cast<const SfxBoolItem &>( (itemset).Get( which )).GetValue() )
42 
43 // Global static helpers ======================================================
44 
45 // Value range limit helpers --------------------------------------------------
46 
48 template< typename ReturnType, typename Type >
49 inline ReturnType llimit_cast( Type nValue, ReturnType nMin )
50 { return static_cast< ReturnType >( ::std::max< Type >( nValue, nMin ) ); }
51 
53 template< typename ReturnType, typename Type >
54 inline ReturnType ulimit_cast( Type nValue, ReturnType nMax )
55 { return static_cast< ReturnType >( ::std::min< Type >( nValue, nMax ) ); }
56 
58 template< typename ReturnType, typename Type >
59 inline ReturnType ulimit_cast( Type nValue )
60 { return ulimit_cast( nValue, ::std::numeric_limits< ReturnType >::max() ); }
61 
63 template< typename ReturnType, typename Type >
64 inline ReturnType limit_cast( Type nValue, ReturnType nMin, ReturnType nMax )
65 { return static_cast< ReturnType >( ::std::clamp< Type >( nValue, nMin, nMax ) ); }
66 
68 template< typename ReturnType, typename Type >
69 inline ReturnType limit_cast( Type nValue )
70 { return limit_cast( nValue, ::std::numeric_limits< ReturnType >::min(), ::std::numeric_limits< ReturnType >::max() ); }
71 
72 // Read from bitfields --------------------------------------------------------
73 
75 template< typename Type >
76 inline bool get_flag( Type nBitField, Type nMask )
77 { return (nBitField & nMask) != 0; }
78 
80 template< typename ReturnType, typename Type >
81 inline ReturnType get_flagvalue( Type nBitField, Type nMask, ReturnType nSet, ReturnType nUnset )
82 { return ::get_flag( nBitField, nMask ) ? nSet : nUnset; }
83 
88 template< typename ReturnType, typename Type >
89 inline ReturnType extract_value( Type nBitField, sal_uInt8 nStartBit, sal_uInt8 nBitCount )
90 { return static_cast< ReturnType >( ((1UL << nBitCount) - 1) & (nBitField >> nStartBit) ); }
91 
92 // Write to bitfields ---------------------------------------------------------
93 
95 template< typename Type >
96 inline void set_flag( Type& rnBitField, Type nMask, bool bSet = true )
97 { if( bSet ) rnBitField |= nMask; else rnBitField &= ~nMask; }
98 
102 template< typename Type, typename InsertType >
103 void insert_value( Type& rnBitField, InsertType nValue, sal_uInt8 nStartBit, sal_uInt8 nBitCount )
104 {
105  unsigned int nMask = (1U << nBitCount) - 1;
106  Type nNewValue = static_cast< Type >( nValue & nMask );
107  rnBitField = (rnBitField & ~(nMask << nStartBit)) | (nNewValue << nStartBit);
108 }
109 
110 class Color;
111 class SfxPoolItem;
112 class SfxItemSet;
113 class ScStyleSheet;
114 class ScStyleSheetPool;
115 class SvStream;
116 class SotStorage;
117 class SotStorageStream;
118 
120 class ScfTools
121 {
122 public:
125  ScfTools() = delete;
126  ScfTools(const ScfTools&) = delete;
127  const ScfTools& operator=(const ScfTools&) = delete;
128 
129 // *** common methods *** -----------------------------------------------------
130 
132  static double ReadLongDouble( SvStream& rStrm );
134  static rtl_TextEncoding GetSystemTextEncoding();
136  static OUString GetHexStr( sal_uInt16 nValue );
137 
140  static sal_uInt8 GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans );
143  static Color GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans );
144 
145 // *** conversion of names *** ------------------------------------------------
146 
151  static OUString ConvertToScDefinedName( const OUString& rName );
152 
153 // *** streams and storages *** -----------------------------------------------
154 
156  static tools::SvRef<SotStorage> OpenStorageRead( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrgName );
158  static tools::SvRef<SotStorage> OpenStorageWrite( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrgName );
159 
161  static tools::SvRef<SotStorageStream> OpenStorageStreamRead( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName );
163  static tools::SvRef<SotStorageStream> OpenStorageStreamWrite( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName );
164 
165 // *** item handling *** ------------------------------------------------------
166 
169  static bool CheckItem( const SfxItemSet& rItemSet, sal_uInt16 nWhichId, bool bDeep );
173  static bool CheckItems( const SfxItemSet& rItemSet, const sal_uInt16* pnWhichIds, bool bDeep );
174 
182  static void PutItem(
183  SfxItemSet& rItemSet, const SfxPoolItem& rItem,
184  sal_uInt16 nWhichId, bool bSkipPoolDef );
185 
192  static void PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, bool bSkipPoolDef );
193 
194 // *** style sheet handling *** -----------------------------------------------
195 
201  ScStyleSheetPool& rPool,
202  const OUString& rStyleName, bool bForceName );
208  ScStyleSheetPool& rPool,
209  const OUString& rStyleName, bool bForceName );
210 
211 // *** byte string import operations *** --------------------------------------
212 
214  static OString read_zeroTerminated_uInt8s_ToOString(SvStream& rStrm, sal_Int32& rnBytesLeft);
216  static OUString read_zeroTerminated_uInt8s_ToOUString(SvStream& rStrm, sal_Int32& rnBytesLeft, rtl_TextEncoding eTextEnc)
217  {
218  return OStringToOUString(read_zeroTerminated_uInt8s_ToOString(rStrm, rnBytesLeft), eTextEnc);
219  }
220 
222  static void AppendCString( SvStream& rStrm, OUString& rString, rtl_TextEncoding eTextEnc );
223 
224 // *** HTML table names <-> named range names *** -----------------------------
225 
227  static const OUString& GetHTMLDocName();
229  static const OUString& GetHTMLTablesName();
231  static OUString GetNameFromHTMLIndex( sal_uInt32 nIndex );
233  static OUString GetNameFromHTMLName( std::u16string_view rTabName );
234 
236  static bool IsHTMLDocName( const OUString& rSource );
238  static bool IsHTMLTablesName( const OUString& rSource );
243  static bool GetHTMLNameFromName( const OUString& rSource, OUString& rName );
244 
245 private:
247  static const OUString& GetHTMLIndexPrefix();
249  static const OUString& GetHTMLNamePrefix();
250 };
251 
252 // Containers =================================================================
253 
254 typedef ::std::vector< sal_uInt8 > ScfUInt8Vec;
255 typedef ::std::vector< sal_Int16 > ScfInt16Vec;
256 typedef ::std::vector< sal_uInt16 > ScfUInt16Vec;
257 typedef ::std::vector< sal_Int32 > ScfInt32Vec;
258 typedef ::std::vector< sal_uInt32 > ScfUInt32Vec;
259 typedef ::std::vector< OUString > ScfStringVec;
260 
262 {
263 public:
265  virtual ~ScFormatFilterPluginImpl();
266  // various import filters
267  virtual ErrCode ScImportLotus123( SfxMedium&, ScDocument&, rtl_TextEncoding eSrc ) override;
268  virtual ErrCode ScImportQuattroPro(SvStream* pStream, ScDocument& rDoc) override;
269  virtual ErrCode ScImportExcel( SfxMedium&, ScDocument*, const EXCIMPFORMAT ) override;
270  // eFormat == EIF_AUTO -> matching filter is used automatically
271  // eFormat == EIF_BIFF5 -> only Biff5 stream leads to success (even in an Excel97 doc)
272  // eFormat == EIF_BIFF8 -> only Biff8 stream leads to success (only in Excel97 docs)
273  // eFormat == EIF_BIFF_LE4 -> only non-storage files _could_ lead to success
274  virtual ErrCode ScImportDif( SvStream&, ScDocument*, const ScAddress& rInsPos,
275  const rtl_TextEncoding eSrc ) override;
276  virtual ErrCode ScImportRTF( SvStream&, const OUString& rBaseURL, ScDocument*, ScRange& rRange ) override;
277  virtual ErrCode ScImportHTML( SvStream&, const OUString& rBaseURL, ScDocument*, ScRange& rRange,
278  double nOutputFactor, bool bCalcWidthHeight,
279  SvNumberFormatter* pFormatter, bool bConvertDate ) override;
280 
281  virtual std::unique_ptr<ScEEAbsImport> CreateRTFImport( ScDocument* pDoc, const ScRange& rRange ) override;
282  virtual std::unique_ptr<ScEEAbsImport> CreateHTMLImport( ScDocument* pDocP, const OUString& rBaseURL, const ScRange& rRange ) override;
283  virtual OUString GetHTMLRangeNameList( ScDocument& rDoc, const OUString& rOrigName ) override;
284 
285  // various export filters
286  virtual ErrCode ScExportExcel5( SfxMedium&, ScDocument*, ExportFormatExcel eFormat, rtl_TextEncoding eDest ) override;
287  virtual void ScExportDif( SvStream&, ScDocument*, const ScAddress& rOutPos, const rtl_TextEncoding eDest ) override;
288  virtual void ScExportDif( SvStream&, ScDocument*, const ScRange& rRange, const rtl_TextEncoding eDest ) override;
289  virtual void ScExportHTML( SvStream&, const OUString& rBaseURL, ScDocument*, const ScRange& rRange, const rtl_TextEncoding eDest, bool bAll,
290  const OUString& rStreamPath, OUString& rNonConvertibleChars, const OUString& rFilterOptions ) override;
291  virtual void ScExportRTF( SvStream&, ScDocument*, const ScRange& rRange, const rtl_TextEncoding eDest ) override;
292 
293  virtual ScOrcusFilters* GetOrcusFilters() override;
294 };
295 
296 #endif
297 
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static double ReadLongDouble(SvStream &rStrm)
Reads a 10-byte-long-double and converts it to double.
Definition: ftools.cxx:41
Type
static void AppendCString(SvStream &rStrm, OUString &rString, rtl_TextEncoding eTextEnc)
Appends a zero terminated byte string.
Definition: ftools.cxx:273
::std::vector< sal_uInt8 > ScfUInt8Vec
Definition: ftools.hxx:254
static bool GetHTMLNameFromName(const OUString &rSource, OUString &rName)
Converts a built-in range name to an HTML table name.
Definition: ftools.cxx:327
static bool CheckItem(const SfxItemSet &rItemSet, sal_uInt16 nWhichId, bool bDeep)
Returns true, if the passed item set contains the item.
Definition: ftools.cxx:195
ReturnType get_flagvalue(Type nBitField, Type nMask, ReturnType nSet, ReturnType nUnset)
Returns nSet, if at least one bit of nMask is set in nBitField, otherwise nUnset. ...
Definition: ftools.hxx:81
Collection of orcus filter wrappers.
static bool IsHTMLTablesName(const OUString &rSource)
Returns true, if rSource is the built-in range name for all HTML tables.
Definition: ftools.cxx:322
virtual ErrCode ScImportRTF(SvStream &, const OUString &rBaseURL, ScDocument *, ScRange &rRange) override
Definition: rtfimp.cxx:26
virtual ErrCode ScExportExcel5(SfxMedium &, ScDocument *, ExportFormatExcel eFormat, rtl_TextEncoding eDest) override
Definition: excel.cxx:398
static tools::SvRef< SotStorageStream > OpenStorageStreamWrite(tools::SvRef< SotStorage > const &xStrg, const OUString &rStrmName)
Creates and opens a stream with the specified name in the passed storage (read/write).
Definition: ftools.cxx:184
static OUString ConvertToScDefinedName(const OUString &rName)
Converts a string to a valid Calc defined name or database range name.
Definition: ftools.cxx:144
static bool IsHTMLDocName(const OUString &rSource)
Returns true, if rSource is the built-in range name for an HTML document.
Definition: ftools.cxx:317
virtual void ScExportDif(SvStream &, ScDocument *, const ScAddress &rOutPos, const rtl_TextEncoding eDest) override
Definition: difexp.cxx:35
void set_flag(Type &rnBitField, Type nMask, bool bSet=true)
Sets or clears (according to bSet) all set bits of nMask in rnBitField.
Definition: ftools.hxx:96
static OUString GetHexStr(sal_uInt16 nValue)
Returns a string representing the hexadecimal value of nValue.
Definition: ftools.cxx:116
virtual ErrCode ScImportQuattroPro(SvStream *pStream, ScDocument &rDoc) override
Definition: qpro.cxx:130
virtual void ScExportRTF(SvStream &, ScDocument *, const ScRange &rRange, const rtl_TextEncoding eDest) override
Definition: rtfexp.cxx:39
ReturnType limit_cast(Type nValue, ReturnType nMin, ReturnType nMax)
Returns the value, if it is not less than nMin and not greater than nMax, otherwise one of the limits...
Definition: ftools.hxx:64
static tools::SvRef< SotStorage > OpenStorageWrite(tools::SvRef< SotStorage > const &xStrg, const OUString &rStrgName)
Creates and opens a storage with the specified name in the passed storage (read/write).
Definition: ftools.cxx:168
static OUString read_zeroTerminated_uInt8s_ToOUString(SvStream &rStrm, sal_Int32 &rnBytesLeft, rtl_TextEncoding eTextEnc)
Reads and returns a zero terminated byte string and decreases a stream counter.
Definition: ftools.hxx:216
virtual ErrCode ScImportDif(SvStream &, ScDocument *, const ScAddress &rInsPos, const rtl_TextEncoding eSrc) override
Definition: difimp.cxx:48
static ScStyleSheet & MakePageStyleSheet(ScStyleSheetPool &rPool, const OUString &rStyleName, bool bForceName)
Creates and returns a page style sheet and inserts it into the pool.
Definition: ftools.cxx:257
static const OUString & GetHTMLTablesName()
Returns the built-in range name for all HTML tables.
Definition: ftools.cxx:286
ReturnType ulimit_cast(Type nValue, ReturnType nMax)
Returns the value, if it is not greater than nMax, otherwise nMax.
Definition: ftools.hxx:54
ScfTools()=delete
We don't want anybody to instantiate this class, since it is just a collection of static items...
Contains static methods used anywhere in the filters.
Definition: ftools.hxx:120
static OUString GetNameFromHTMLIndex(sal_uInt32 nIndex)
Returns the built-in range name for an HTML table, specified by table index.
Definition: ftools.cxx:305
static tools::SvRef< SotStorage > OpenStorageRead(tools::SvRef< SotStorage > const &xStrg, const OUString &rStrgName)
Tries to open an existing storage with the specified name in the passed storage (read-only).
Definition: ftools.cxx:160
static Color GetMixedColor(const Color &rFore, const Color &rBack, sal_uInt8 nTrans)
Mixes colors with given transparence.
Definition: ftools.cxx:132
virtual ScOrcusFilters * GetOrcusFilters() override
Definition: ftools.cxx:347
ReturnType extract_value(Type nBitField, sal_uInt8 nStartBit, sal_uInt8 nBitCount)
Extracts a value from a bit field.
Definition: ftools.hxx:89
static void PutItem(SfxItemSet &rItemSet, const SfxPoolItem &rItem, sal_uInt16 nWhichId, bool bSkipPoolDef)
Puts the item into the passed item set.
Definition: ftools.cxx:209
virtual std::unique_ptr< ScEEAbsImport > CreateRTFImport(ScDocument *pDoc, const ScRange &rRange) override
Definition: rtfimp.cxx:36
const ScfTools & operator=(const ScfTools &)=delete
static sal_uInt8 GetMixedColorComp(sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans)
Mixes RGB components with given transparence.
Definition: ftools.cxx:126
static bool CheckItems(const SfxItemSet &rItemSet, const sal_uInt16 *pnWhichIds, bool bDeep)
Returns true, if the passed item set contains at least one of the items.
Definition: ftools.cxx:200
static const OUString & GetHTMLDocName()
Returns the built-in range name for an HTML document.
Definition: ftools.cxx:280
bool get_flag(Type nBitField, Type nMask)
Returns true, if at least one of the bits set in nMask is set in nBitField.
Definition: ftools.hxx:76
virtual std::unique_ptr< ScEEAbsImport > CreateHTMLImport(ScDocument *pDocP, const OUString &rBaseURL, const ScRange &rRange) override
Definition: htmlimp.cxx:57
static tools::SvRef< SotStorageStream > OpenStorageStreamRead(tools::SvRef< SotStorage > const &xStrg, const OUString &rStrmName)
Tries to open an existing stream with the specified name in the passed storage (read-only).
Definition: ftools.cxx:176
ReturnType llimit_cast(Type nValue, ReturnType nMin)
Returns the value, if it is not less than nMin, otherwise nMin.
Definition: ftools.hxx:49
virtual void ScExportHTML(SvStream &, const OUString &rBaseURL, ScDocument *, const ScRange &rRange, const rtl_TextEncoding eDest, bool bAll, const OUString &rStreamPath, OUString &rNonConvertibleChars, const OUString &rFilterOptions) override
Definition: htmlexp.cxx:128
ExportFormatExcel
Definition: filter.hxx:43
virtual ~ScFormatFilterPluginImpl()
Definition: ftools.cxx:345
::std::vector< sal_uInt32 > ScfUInt32Vec
Definition: ftools.hxx:258
::std::vector< sal_uInt16 > ScfUInt16Vec
Definition: ftools.hxx:256
unsigned char sal_uInt8
virtual ErrCode ScImportLotus123(SfxMedium &, ScDocument &, rtl_TextEncoding eSrc) override
Definition: lotus.cxx:31
EXCIMPFORMAT
Definition: filter.hxx:40
static const OUString & GetHTMLNamePrefix()
Returns the prefix for table names.
Definition: ftools.cxx:299
void insert_value(Type &rnBitField, InsertType nValue, sal_uInt8 nStartBit, sal_uInt8 nBitCount)
Inserts a value into a bitfield.
Definition: ftools.hxx:103
virtual OUString GetHTMLRangeNameList(ScDocument &rDoc, const OUString &rOrigName) override
Definition: htmlimp.cxx:195
virtual ErrCode ScImportHTML(SvStream &, const OUString &rBaseURL, ScDocument *, ScRange &rRange, double nOutputFactor, bool bCalcWidthHeight, SvNumberFormatter *pFormatter, bool bConvertDate) override
Definition: htmlimp.cxx:45
static OUString GetNameFromHTMLName(std::u16string_view rTabName)
Returns the built-in range name for an HTML table, specified by table name.
Definition: ftools.cxx:312
::std::vector< sal_Int32 > ScfInt32Vec
Definition: ftools.hxx:257
::std::vector< sal_Int16 > ScfInt16Vec
Definition: ftools.hxx:255
static ScStyleSheet & MakeCellStyleSheet(ScStyleSheetPool &rPool, const OUString &rStyleName, bool bForceName)
Creates and returns a cell style sheet and inserts it into the pool.
Definition: ftools.cxx:252
void SvStream & rStrm
virtual ErrCode ScImportExcel(SfxMedium &, ScDocument *, const EXCIMPFORMAT) override
Definition: excel.cxx:144
::std::vector< OUString > ScfStringVec
Definition: ftools.hxx:259
static rtl_TextEncoding GetSystemTextEncoding()
Returns system text encoding for byte string conversion.
Definition: ftools.cxx:111
static OString read_zeroTerminated_uInt8s_ToOString(SvStream &rStrm, sal_Int32 &rnBytesLeft)
Reads and returns a zero terminated byte string and decreases a stream counter.
Definition: ftools.cxx:264
sal_Int16 nValue
static const OUString & GetHTMLIndexPrefix()
Returns the prefix for table index names.
Definition: ftools.cxx:292