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#pragma once
21
22#include <algorithm>
23#include <vector>
24#include <limits>
25#include <string_view>
26
27#include <tools/ref.hxx>
28#include <filter.hxx>
29
30// Common macros ==============================================================
31
32// items and item sets --------------------------------------------------------
33
35#define GETITEM( itemset, itemtype, which ) \
36 static_cast< const itemtype & >( (itemset).Get( which ) )
37
39#define GETITEMBOOL( itemset, which ) \
40 (static_cast<const SfxBoolItem &>( (itemset).Get( which )).GetValue() )
41
42// Global static helpers ======================================================
43
44// Value range limit helpers --------------------------------------------------
45
47template< typename ReturnType, typename Type >
48inline ReturnType llimit_cast( Type nValue, ReturnType nMin )
49{ return static_cast< ReturnType >( ::std::max< Type >( nValue, nMin ) ); }
50
52template< typename ReturnType, typename Type >
53inline ReturnType ulimit_cast( Type nValue, ReturnType nMax )
54{ return static_cast< ReturnType >( ::std::min< Type >( nValue, nMax ) ); }
55
57template< typename ReturnType, typename Type >
58inline ReturnType ulimit_cast( Type nValue )
59{ return ulimit_cast( nValue, ::std::numeric_limits< ReturnType >::max() ); }
60
62template< typename ReturnType, typename Type >
63inline ReturnType limit_cast( Type nValue, ReturnType nMin, ReturnType nMax )
64{ return static_cast< ReturnType >( ::std::clamp< Type >( nValue, nMin, nMax ) ); }
65
67template< typename ReturnType, typename Type >
68inline ReturnType limit_cast( Type nValue )
69{ return limit_cast( nValue, ::std::numeric_limits< ReturnType >::min(), ::std::numeric_limits< ReturnType >::max() ); }
70
71// Read from bitfields --------------------------------------------------------
72
74template< typename Type >
75inline bool get_flag( Type nBitField, Type nMask )
76{ return (nBitField & nMask) != 0; }
77
79template< typename ReturnType, typename Type >
80inline ReturnType get_flagvalue( Type nBitField, Type nMask, ReturnType nSet, ReturnType nUnset )
81{ return ::get_flag( nBitField, nMask ) ? nSet : nUnset; }
82
87template< typename ReturnType, typename Type >
88inline ReturnType extract_value( Type nBitField, sal_uInt8 nStartBit, sal_uInt8 nBitCount )
89{ return static_cast< ReturnType >( ((1UL << nBitCount) - 1) & (nBitField >> nStartBit) ); }
90
91// Write to bitfields ---------------------------------------------------------
92
94template< typename Type >
95inline void set_flag( Type& rnBitField, Type nMask, bool bSet = true )
96{ if( bSet ) rnBitField |= nMask; else rnBitField &= ~nMask; }
97
101template< typename Type, typename InsertType >
102void insert_value( Type& rnBitField, InsertType nValue, sal_uInt8 nStartBit, sal_uInt8 nBitCount )
103{
104 unsigned int nMask = (1U << nBitCount) - 1;
105 Type nNewValue = static_cast< Type >( nValue & nMask );
106 rnBitField = (rnBitField & ~(nMask << nStartBit)) | (nNewValue << nStartBit);
107}
108
109class Color;
110class SfxPoolItem;
111class SfxItemSet;
112class ScStyleSheet;
113class ScStyleSheetPool;
114class SvStream;
115class SotStorage;
116class SotStorageStream;
117
120{
121public:
124 ScfTools() = delete;
125 ScfTools(const ScfTools&) = delete;
126 const ScfTools& operator=(const ScfTools&) = delete;
127
128// *** common methods *** -----------------------------------------------------
129
131 static void ReadLongDouble(SvStream& rStrm, double& fResult);
133 static rtl_TextEncoding GetSystemTextEncoding();
135 static OUString GetHexStr( sal_uInt16 nValue );
136
139 static sal_uInt8 GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans );
142 static Color GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans );
143
144// *** conversion of names *** ------------------------------------------------
145
150 static OUString ConvertToScDefinedName( const OUString& rName );
151
152// *** streams and storages *** -----------------------------------------------
153
155 static tools::SvRef<SotStorage> OpenStorageRead( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrgName );
157 static tools::SvRef<SotStorage> OpenStorageWrite( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrgName );
158
160 static tools::SvRef<SotStorageStream> OpenStorageStreamRead( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName );
162 static tools::SvRef<SotStorageStream> OpenStorageStreamWrite( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName );
163
164// *** item handling *** ------------------------------------------------------
165
168 static bool CheckItem( const SfxItemSet& rItemSet, sal_uInt16 nWhichId, bool bDeep );
172 static bool CheckItems( const SfxItemSet& rItemSet, const sal_uInt16* pnWhichIds, bool bDeep );
173
181 static void PutItem(
182 SfxItemSet& rItemSet, const SfxPoolItem& rItem,
183 sal_uInt16 nWhichId, bool bSkipPoolDef );
184
191 static void PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, bool bSkipPoolDef );
192
193// *** style sheet handling *** -----------------------------------------------
194
200 ScStyleSheetPool& rPool,
201 const OUString& rStyleName, bool bForceName );
207 ScStyleSheetPool& rPool,
208 const OUString& rStyleName, bool bForceName );
209
210// *** byte string import operations *** --------------------------------------
211
213 static OString read_zeroTerminated_uInt8s_ToOString(SvStream& rStrm, sal_Int32& rnBytesLeft);
215 static OUString read_zeroTerminated_uInt8s_ToOUString(SvStream& rStrm, sal_Int32& rnBytesLeft, rtl_TextEncoding eTextEnc)
216 {
217 return OStringToOUString(read_zeroTerminated_uInt8s_ToOString(rStrm, rnBytesLeft), eTextEnc);
218 }
219
221 static void AppendCString( SvStream& rStrm, OUString& rString, rtl_TextEncoding eTextEnc );
222
223// *** HTML table names <-> named range names *** -----------------------------
224
226 static const OUString& GetHTMLDocName();
228 static const OUString& GetHTMLTablesName();
230 static OUString GetNameFromHTMLIndex( sal_uInt32 nIndex );
232 static OUString GetNameFromHTMLName( std::u16string_view rTabName );
233
235 static bool IsHTMLDocName( std::u16string_view rSource );
237 static bool IsHTMLTablesName( std::u16string_view rSource );
242 static bool GetHTMLNameFromName( const OUString& rSource, OUString& rName );
243
244private:
246 static const OUString& GetHTMLIndexPrefix();
248 static const OUString& GetHTMLNamePrefix();
249};
250
251// Containers =================================================================
252
253typedef ::std::vector< sal_uInt8 > ScfUInt8Vec;
254typedef ::std::vector< sal_Int16 > ScfInt16Vec;
255typedef ::std::vector< sal_uInt16 > ScfUInt16Vec;
256typedef ::std::vector< sal_Int32 > ScfInt32Vec;
257typedef ::std::vector< sal_uInt32 > ScfUInt32Vec;
258typedef ::std::vector< OUString > ScfStringVec;
259
261{
262public:
265 // various import filters
266 virtual ErrCode ScImportLotus123( SfxMedium&, ScDocument&, rtl_TextEncoding eSrc ) override;
267 virtual ErrCode ScImportQuattroPro(SvStream* pStream, ScDocument& rDoc) override;
268 virtual ErrCode ScImportExcel( SfxMedium&, ScDocument*, const EXCIMPFORMAT ) override;
269 // eFormat == EIF_AUTO -> matching filter is used automatically
270 // eFormat == EIF_BIFF5 -> only Biff5 stream leads to success (even in an Excel97 doc)
271 // eFormat == EIF_BIFF8 -> only Biff8 stream leads to success (only in Excel97 docs)
272 // eFormat == EIF_BIFF_LE4 -> only non-storage files _could_ lead to success
273 virtual ErrCode ScImportDif( SvStream&, ScDocument*, const ScAddress& rInsPos,
274 const rtl_TextEncoding eSrc ) override;
275 virtual ErrCode ScImportRTF( SvStream&, const OUString& rBaseURL, ScDocument*, ScRange& rRange ) override;
276 virtual ErrCode ScImportHTML( SvStream&, const OUString& rBaseURL, ScDocument*, ScRange& rRange,
277 double nOutputFactor, bool bCalcWidthHeight,
278 SvNumberFormatter* pFormatter, bool bConvertDate, bool bConvertScientific ) override;
279
280 virtual std::unique_ptr<ScEEAbsImport> CreateRTFImport( ScDocument* pDoc, const ScRange& rRange ) override;
281 virtual std::unique_ptr<ScEEAbsImport> CreateHTMLImport( ScDocument* pDocP, const OUString& rBaseURL, const ScRange& rRange ) override;
282 virtual OUString GetHTMLRangeNameList( ScDocument& rDoc, const OUString& rOrigName ) override;
283
284 // various export filters
285 virtual ErrCode ScExportExcel5( SfxMedium&, ScDocument*, ExportFormatExcel eFormat, rtl_TextEncoding eDest ) override;
286 virtual void ScExportDif( SvStream&, ScDocument*, const ScAddress& rOutPos, const rtl_TextEncoding eDest ) override;
287 virtual void ScExportDif( SvStream&, ScDocument*, const ScRange& rRange, const rtl_TextEncoding eDest ) override;
288 virtual void ScExportHTML( SvStream&, const OUString& rBaseURL, ScDocument*, const ScRange& rRange, const rtl_TextEncoding eDest, bool bAll,
289 const OUString& rStreamPath, OUString& rNonConvertibleChars, const OUString& rFilterOptions ) override;
290 virtual void ScExportRTF( SvStream&, ScDocument*, const ScRange& rRange, const rtl_TextEncoding eDest ) override;
291
292 virtual ScOrcusFilters* GetOrcusFilters() override;
293};
294
295/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void ScExportRTF(SvStream &, ScDocument *, const ScRange &rRange, const rtl_TextEncoding eDest) override
Definition: rtfexp.cxx:39
virtual ErrCode ScImportDif(SvStream &, ScDocument *, const ScAddress &rInsPos, const rtl_TextEncoding eSrc) override
Definition: difimp.cxx:42
virtual ErrCode ScImportLotus123(SfxMedium &, ScDocument &, rtl_TextEncoding eSrc) override
Definition: lotus.cxx:31
virtual ErrCode ScImportQuattroPro(SvStream *pStream, ScDocument &rDoc) override
Definition: qpro.cxx:145
virtual ErrCode ScExportExcel5(SfxMedium &, ScDocument *, ExportFormatExcel eFormat, rtl_TextEncoding eDest) override
Definition: excel.cxx:400
virtual std::unique_ptr< ScEEAbsImport > CreateHTMLImport(ScDocument *pDocP, const OUString &rBaseURL, const ScRange &rRange) override
Definition: htmlimp.cxx:58
virtual std::unique_ptr< ScEEAbsImport > CreateRTFImport(ScDocument *pDoc, const ScRange &rRange) override
Definition: rtfimp.cxx:36
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:133
virtual ScOrcusFilters * GetOrcusFilters() override
Definition: ftools.cxx:352
virtual ErrCode ScImportExcel(SfxMedium &, ScDocument *, const EXCIMPFORMAT) override
Definition: excel.cxx:146
virtual ErrCode ScImportRTF(SvStream &, const OUString &rBaseURL, ScDocument *, ScRange &rRange) override
Definition: rtfimp.cxx:26
virtual OUString GetHTMLRangeNameList(ScDocument &rDoc, const OUString &rOrigName) override
Definition: htmlimp.cxx:199
virtual ~ScFormatFilterPluginImpl()
Definition: ftools.cxx:350
virtual ErrCode ScImportHTML(SvStream &, const OUString &rBaseURL, ScDocument *, ScRange &rRange, double nOutputFactor, bool bCalcWidthHeight, SvNumberFormatter *pFormatter, bool bConvertDate, bool bConvertScientific) override
Definition: htmlimp.cxx:46
virtual void ScExportDif(SvStream &, ScDocument *, const ScAddress &rOutPos, const rtl_TextEncoding eDest) override
Definition: difexp.cxx:35
Collection of orcus filter wrappers.
Contains static methods used anywhere in the filters.
Definition: ftools.hxx:120
ScfTools(const ScfTools &)=delete
static const OUString & GetHTMLDocName()
Returns the built-in range name for an HTML document.
Definition: ftools.cxx:285
static sal_uInt8 GetMixedColorComp(sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans)
Mixes RGB components with given transparence.
Definition: ftools.cxx:131
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:165
static bool CheckItem(const SfxItemSet &rItemSet, sal_uInt16 nWhichId, bool bDeep)
Returns true, if the passed item set contains the item.
Definition: ftools.cxx:200
static bool IsHTMLTablesName(std::u16string_view rSource)
Returns true, if rSource is the built-in range name for all HTML tables.
Definition: ftools.cxx:327
static OUString ConvertToScDefinedName(const OUString &rName)
Converts a string to a valid Calc defined name or database range name.
Definition: ftools.cxx:149
static bool GetHTMLNameFromName(const OUString &rSource, OUString &rName)
Converts a built-in range name to an HTML table name.
Definition: ftools.cxx:332
static Color GetMixedColor(const Color &rFore, const Color &rBack, sal_uInt8 nTrans)
Mixes colors with given transparence.
Definition: ftools.cxx:137
ScfTools()=delete
We don't want anybody to instantiate this class, since it is just a collection of static items.
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:205
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:262
static const OUString & GetHTMLTablesName()
Returns the built-in range name for all HTML tables.
Definition: ftools.cxx:291
static rtl_TextEncoding GetSystemTextEncoding()
Returns system text encoding for byte string conversion.
Definition: ftools.cxx:116
const ScfTools & operator=(const ScfTools &)=delete
static OUString GetHexStr(sal_uInt16 nValue)
Returns a string representing the hexadecimal value of nValue.
Definition: ftools.cxx:121
static OUString GetNameFromHTMLIndex(sal_uInt32 nIndex)
Returns the built-in range name for an HTML table, specified by table index.
Definition: ftools.cxx:310
static void PutItem(SfxItemSet &rItemSet, const SfxPoolItem &rItem, sal_uInt16 nWhichId, bool bSkipPoolDef)
Puts the item into the passed item set.
Definition: ftools.cxx:214
static void AppendCString(SvStream &rStrm, OUString &rString, rtl_TextEncoding eTextEnc)
Appends a zero terminated byte string.
Definition: ftools.cxx:278
static bool IsHTMLDocName(std::u16string_view rSource)
Returns true, if rSource is the built-in range name for an HTML document.
Definition: ftools.cxx:322
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:173
static const OUString & GetHTMLIndexPrefix()
Returns the prefix for table index names.
Definition: ftools.cxx:297
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:181
static void ReadLongDouble(SvStream &rStrm, double &fResult)
Reads a 10-byte-long-double and converts it to double.
Definition: ftools.cxx:42
static const OUString & GetHTMLNamePrefix()
Returns the prefix for table names.
Definition: ftools.cxx:304
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:189
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:269
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:257
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:215
static OUString GetNameFromHTMLName(std::u16string_view rTabName)
Returns the built-in range name for an HTML table, specified by table name.
Definition: ftools.cxx:317
ExportFormatExcel
Definition: filter.hxx:42
EXCIMPFORMAT
Definition: filter.hxx:39
sal_Int16 nValue
::std::vector< OUString > ScfStringVec
Definition: ftools.hxx:258
::std::vector< sal_uInt8 > ScfUInt8Vec
Definition: ftools.hxx:253
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:80
void insert_value(Type &rnBitField, InsertType nValue, sal_uInt8 nStartBit, sal_uInt8 nBitCount)
Inserts a value into a bitfield.
Definition: ftools.hxx:102
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:75
ReturnType ulimit_cast(Type nValue, ReturnType nMax)
Returns the value, if it is not greater than nMax, otherwise nMax.
Definition: ftools.hxx:53
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:95
::std::vector< sal_Int16 > ScfInt16Vec
Definition: ftools.hxx:254
::std::vector< sal_uInt16 > ScfUInt16Vec
Definition: ftools.hxx:255
::std::vector< sal_Int32 > ScfInt32Vec
Definition: ftools.hxx:256
ReturnType extract_value(Type nBitField, sal_uInt8 nStartBit, sal_uInt8 nBitCount)
Extracts a value from a bit field.
Definition: ftools.hxx:88
ReturnType llimit_cast(Type nValue, ReturnType nMin)
Returns the value, if it is not less than nMin, otherwise nMin.
Definition: ftools.hxx:48
::std::vector< sal_uInt32 > ScfUInt32Vec
Definition: ftools.hxx:257
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:63
short nBitCount
Type
void SvStream & rStrm
unsigned char sal_uInt8