1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SC_SOURCE_UI_INC_IMPEX_HXX
21 #define INCLUDED_SC_SOURCE_UI_INC_IMPEX_HXX
23 #include <o3tl/deleter.hxx>
24 #include <sot/formats.hxx>
25 #include <address.hxx>
26 #include <tools/stream.hxx>
33 * These options control how multi-line cells are converted during export in
34 * certain lossy formats (such as csv).
36 struct ScExportTextOptions
38 enum NewlineConversion
{ ToSystem
, ToSpace
, None
};
39 ScExportTextOptions( NewlineConversion eNewlineConversion
= ToSystem
, sal_Unicode cSeparatorConvertTo
= 0, bool bAddQuotes
= false ) :
40 meNewlineConversion( eNewlineConversion
), mcSeparatorConvertTo( cSeparatorConvertTo
), mbAddQuotes( bAddQuotes
) {}
42 NewlineConversion meNewlineConversion
;
43 sal_Unicode mcSeparatorConvertTo
; // Convert separator to this character
47 class SC_DLLPUBLIC ScImportExport
51 std::unique_ptr
<ScDocument
, o3tl::default_delete
<ScDocument
>> pUndoDoc
;
54 OUString aNonConvertibleChars
;
55 OUString maFilterOptions
;
56 sal_uInt32 nSizeLimit
;
57 SCROW
const nMaxImportRow
;
58 sal_Unicode cSep
; // Separator
59 sal_Unicode cStr
; // String Delimiter
60 bool bFormulas
; // Formula in Text?
61 bool bIncludeFiltered
; // include filtered rows? (default true)
62 bool bAll
; // no selection
63 bool bSingle
; // Single selection
64 bool const bUndo
; // with Undo?
65 bool bOverflowRow
; // too many rows
66 bool bOverflowCol
; // too many columns
67 bool bOverflowCell
; // too much data for a cell
69 bool mbImportBroadcast
; // whether or not to broadcast after data import.
70 bool mbOverwriting
; // Whether we could be overwriting existing values (paste).
71 // In this case we cannot use the insert optimization, but we
72 // do not need to broadcast after the import.
73 ScExportTextOptions mExportTextOptions
;
75 std::unique_ptr
<ScAsciiOptions
> pExtOptions
; // extended options
77 bool StartPaste(); // Protect check, set up Undo
78 void EndPaste(bool bAutoRowHeight
= true); // Undo/Redo actions, Repaint
79 bool Doc2Text( SvStream
& );
80 bool Text2Doc( SvStream
& );
81 bool Doc2Sylk( SvStream
& );
82 bool Sylk2Doc( SvStream
& );
83 bool Doc2HTML( SvStream
&, const OUString
& );
84 bool Doc2RTF( SvStream
& );
85 bool Doc2Dif( SvStream
& );
86 bool Dif2Doc( SvStream
& );
87 bool ExtText2Doc( SvStream
& ); // with pExtOptions
88 bool RTF2Doc( SvStream
&, const OUString
& rBaseURL
);
89 bool HTML2Doc( SvStream
&, const OUString
& rBaseURL
);
92 ScImportExport( ScDocument
* ); // the whole document
93 ScImportExport( ScDocument
*, const OUString
& ); // Range/cell input
94 ScImportExport( ScDocument
*, const ScAddress
& );
95 ScImportExport( ScDocument
*, const ScRange
& );
96 ~ScImportExport() COVERITY_NOEXCEPT_FALSE
;
98 void SetExtOptions( const ScAsciiOptions
& rOpt
);
99 void SetFilterOptions( const OUString
& rFilterOptions
);
100 bool IsRef() const { return !bAll
; }
102 const ScRange
& GetRange() const { return aRange
; }
104 static void EmbeddedNullTreatment( OUString
& rStr
);
106 static bool IsFormatSupported( SotClipboardFormatId nFormat
);
107 static const sal_Unicode
* ScanNextFieldFromString( const sal_Unicode
* p
,
108 OUString
& rField
, sal_Unicode cStr
, const sal_Unicode
* pSeps
,
109 bool bMergeSeps
, bool& rbIsQuoted
, bool& rbOverflowCell
, bool bRemoveSpace
);
110 static void WriteUnicodeOrByteString( SvStream
& rStrm
, const OUString
& rString
, bool bZero
= false );
111 static void WriteUnicodeOrByteEndl( SvStream
& rStrm
);
113 //! only if stream is only used in own (!) memory
114 static void SetNoEndianSwap( SvStream
& rStrm
);
116 void SetSeparator( sal_Unicode c
) { cSep
= c
; }
117 void SetDelimiter( sal_Unicode c
) { cStr
= c
; }
118 void SetFormulas( bool b
) { bFormulas
= b
; }
119 void SetIncludeFiltered( bool b
) { bIncludeFiltered
= b
; }
121 void SetStreamPath( const OUString
& rPath
) { aStreamPath
= rPath
; }
123 bool ImportString( const OUString
&, SotClipboardFormatId
);
124 bool ExportString( OUString
&, SotClipboardFormatId
);
125 bool ExportByteString( OString
&, rtl_TextEncoding
, SotClipboardFormatId
);
127 bool ImportStream( SvStream
&, const OUString
& rBaseURL
, SotClipboardFormatId
);
128 bool ExportStream( SvStream
&, const OUString
& rBaseURL
, SotClipboardFormatId
);
130 bool ExportData( const OUString
& rMimeType
,
131 css::uno::Any
& rValue
);
134 bool IsOverflowRow() const { return bOverflowRow
; }
135 bool IsOverflowCol() const { return bOverflowCol
; }
136 bool IsOverflowCell() const { return bOverflowCell
; }
137 bool IsOverflow() const { return bOverflowRow
|| bOverflowCol
|| bOverflowCell
; }
139 const OUString
& GetNonConvertibleChars() const { return aNonConvertibleChars
; }
141 void SetApi( bool bApi
) { mbApi
= bApi
; }
142 void SetImportBroadcast( bool b
) { mbImportBroadcast
= b
; }
143 void SetOverwriting( const bool bOverwriting
) { mbOverwriting
= bOverwriting
; }
144 void SetExportTextOptions( const ScExportTextOptions
& options
) { mExportTextOptions
= options
; }
147 // Helper class for importing clipboard strings as streams.
148 class ScImportStringStream
: public SvMemoryStream
151 ScImportStringStream(const OUString
& rStr
);
154 /** Read a CSV (comma separated values) data line using
155 ReadUniOrByteStringLine().
157 @param bEmbeddedLineBreak
158 If TRUE and a line-break occurs inside a field of data,
159 a line feed LF '\n' and the next line are appended. Repeats
160 until a line-break is not in a field. A field is determined
161 by delimiting rFieldSeparators and optionally surrounded by
162 a pair of cFieldQuote characters. For a line-break to be
163 within a field, the field content MUST be surrounded by
164 cFieldQuote characters, and the opening cFieldQuote MUST be
165 at the very start of a line or follow right behind a field
166 separator with no extra characters in between, with the
167 exception of blanks contradictory to RFC 4180. Anything,
168 including field separators and escaped quotes (by doubling
169 them) may appear in a quoted field.
171 If bEmbeddedLineBreak==FALSE, nothing is parsed and the
172 string returned is simply one ReadUniOrByteStringLine().
174 @param rFieldSeparators
175 A list of characters that each may act as a field separator.
176 If rcDetectSep was 0 and a separator is detected then it is appended to
180 The quote character used.
183 If 0 then attempt to detect a possible space (blank) separator if
184 rFieldSeparators doesn't include it already. This can be necessary because
185 of the "accept broken misquoted CSV fields" feature that tries to ignore
186 trailing blanks after a quoted field and if no separator follows continues
187 to add content to the field assuming the single double quote was in error.
188 If this blank separator is detected it is added to rFieldSeparators and the
189 line is reread with the new separators
191 check Stream::good() to detect IO problems during read
194 Note that the string returned may be truncated even inside
195 a quoted field if some (arbitrary) maximum length was reached.
196 There currently is no way to exactly determine the conditions,
197 whether this was at a line end, or whether open quotes
198 would have closed the field before the line end, as even a
199 ReadUniOrByteStringLine() may return prematurely but the
200 stream was positioned ahead until the real end of line.
201 Additionally, due to character encoding conversions, string
202 length and bytes read don't necessarily match, and
203 resyncing to a previous position matching the string's
204 length isn't always possible. As a result, a logical line
205 with embedded line breaks and more than the maximum length
206 characters will be spoiled, and a subsequent ReadCsvLine()
207 may start under false preconditions.
210 SC_DLLPUBLIC OUString
ReadCsvLine( SvStream
&rStream
, bool bEmbeddedLineBreak
,
211 OUString
& rFieldSeparators
, sal_Unicode cFieldQuote
, sal_Unicode
& rcDetectSep
);
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */