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 INCLUDED_SC_SOURCE_UI_INC_IMPEX_HXX
21 : #define INCLUDED_SC_SOURCE_UI_INC_IMPEX_HXX
22 :
23 : #include <osl/endian.h>
24 : #include <sot/exchange.hxx>
25 : #include "global.hxx"
26 : #include "address.hxx"
27 :
28 : class ScDocShell;
29 : class ScDocument;
30 : class SvStream;
31 : class ScAsciiOptions;
32 :
33 : /**
34 : * These options control how multi-line cells are converted during export in
35 : * certain lossy formats (such as csv).
36 : */
37 : struct ScExportTextOptions
38 : {
39 : enum NewlineConversion { ToSystem, ToSpace, None };
40 40 : ScExportTextOptions( NewlineConversion eNewlineConversion = ToSystem, sal_Unicode cSeparatorConvertTo = 0, bool bAddQuotes = false ) :
41 40 : meNewlineConversion( eNewlineConversion ), mcSeparatorConvertTo( cSeparatorConvertTo ), mbAddQuotes( bAddQuotes ) {}
42 :
43 : NewlineConversion meNewlineConversion;
44 : sal_Unicode mcSeparatorConvertTo; // Convert separator to this character
45 : bool mbAddQuotes;
46 : };
47 :
48 : class ScImportExport
49 : {
50 : ScDocShell* pDocSh;
51 : ScDocument* pDoc;
52 : ScDocument* pUndoDoc;
53 : ScRange aRange;
54 : OUString aStreamPath;
55 : OUString aNonConvertibleChars;
56 : OUString maFilterOptions;
57 : sal_uLong nSizeLimit;
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 bUndo; // with Undo?
65 : bool bOverflowRow; // too many rows
66 : bool bOverflowCol; // too many columns
67 : bool bOverflowCell; // too much data for a cell
68 : bool mbApi;
69 : bool mbImportBroadcast; // whether or not to broadcast after data import.
70 : ScExportTextOptions mExportTextOptions;
71 :
72 : ScAsciiOptions* pExtOptions; // extended options
73 :
74 : bool StartPaste(); // Protect check, set up Undo
75 : void EndPaste(bool bAutoRowHeight = true); // Undo/Redo actions, Repaint
76 : bool Doc2Text( SvStream& );
77 : bool Text2Doc( SvStream& );
78 : bool Doc2Sylk( SvStream& );
79 : bool Sylk2Doc( SvStream& );
80 : bool Doc2HTML( SvStream&, const OUString& );
81 : bool Doc2RTF( SvStream& );
82 : bool Doc2Dif( SvStream& );
83 : bool Dif2Doc( SvStream& );
84 : bool ExtText2Doc( SvStream& ); // with pExtOptions
85 : bool RTF2Doc( SvStream&, const OUString& rBaseURL );
86 : bool HTML2Doc( SvStream&, const OUString& rBaseURL );
87 :
88 : public:
89 : ScImportExport( ScDocument* ); // the whole document
90 : ScImportExport( ScDocument*, const OUString& ); // Range/cell input
91 : ScImportExport( ScDocument*, const ScAddress& );
92 : ScImportExport( ScDocument*, const ScRange& );
93 : ~ScImportExport();
94 :
95 : void SetExtOptions( const ScAsciiOptions& rOpt );
96 : void SetFilterOptions( const OUString& rFilterOptions );
97 :
98 : bool IsDoubleRef() const { return !( bAll || bSingle ); }
99 : bool IsSingleRef() const { return bSingle; }
100 : bool IsNoRef() const { return bAll; }
101 0 : bool IsRef() const { return !bAll; }
102 :
103 0 : const ScRange& GetRange() const { return aRange; }
104 :
105 : bool IsUndo() const { return bUndo; }
106 : void SetUndo( bool b ) { bUndo = b; }
107 :
108 : SC_DLLPUBLIC static void EmbeddedNullTreatment( OUString & rStr );
109 :
110 : static bool IsFormatSupported( sal_uLong nFormat );
111 : static const sal_Unicode* ScanNextFieldFromString( const sal_Unicode* p,
112 : OUString& rField, sal_Unicode cStr, const sal_Unicode* pSeps,
113 : bool bMergeSeps, bool& rbIsQuoted, bool& rbOverflowCell );
114 : static void WriteUnicodeOrByteString( SvStream& rStrm, const OUString& rString, bool bZero = false );
115 : static void WriteUnicodeOrByteEndl( SvStream& rStrm );
116 : static inline bool IsEndianSwap( const SvStream& rStrm );
117 :
118 : //! only if stream is only used in own (!) memory
119 : static inline void SetNoEndianSwap( SvStream& rStrm );
120 :
121 : sal_Unicode GetSeparator() const { return cSep; }
122 0 : void SetSeparator( sal_Unicode c ) { cSep = c; }
123 : sal_Unicode GetDelimiter() const { return cStr; }
124 0 : void SetDelimiter( sal_Unicode c ) { cStr = c; }
125 : bool IsFormulas() const { return bFormulas; }
126 0 : void SetFormulas( bool b ) { bFormulas = b; }
127 : bool IsIncludeFiltered() const { return bIncludeFiltered; }
128 0 : void SetIncludeFiltered( bool b ) { bIncludeFiltered = b; }
129 :
130 : void SetSizeLimit( sal_uLong nNew ) { nSizeLimit = nNew; } // for the moment only for Ascii
131 :
132 6 : void SetStreamPath( const OUString& rPath ) { aStreamPath = rPath; }
133 : const OUString& GetStreamPath() const { return aStreamPath; }
134 :
135 : bool ImportString( const OUString&, sal_uLong=FORMAT_STRING );
136 : bool ExportString( OUString&, sal_uLong=FORMAT_STRING );
137 : bool ExportByteString( OString&, rtl_TextEncoding, sal_uLong=FORMAT_STRING );
138 :
139 : bool ImportStream( SvStream&, const OUString& rBaseURL, sal_uLong=FORMAT_STRING );
140 : bool ExportStream( SvStream&, const OUString& rBaseURL, sal_uLong=FORMAT_STRING );
141 :
142 : bool ImportData( const OUString& rMimeType,
143 : const ::com::sun::star::uno::Any & rValue );
144 : bool ExportData( const OUString& rMimeType,
145 : ::com::sun::star::uno::Any & rValue );
146 :
147 : // after import
148 4 : bool IsOverflowRow() const { return bOverflowRow; }
149 4 : bool IsOverflowCol() const { return bOverflowCol; }
150 4 : bool IsOverflowCell() const { return bOverflowCell; }
151 0 : bool IsOverflow() const { return bOverflowRow || bOverflowCol || bOverflowCell; }
152 :
153 6 : const OUString& GetNonConvertibleChars() const { return aNonConvertibleChars; }
154 :
155 : bool IsApi() const { return mbApi; }
156 0 : void SetApi( bool bApi ) { mbApi = bApi; }
157 2 : void SetImportBroadcast( bool b ) { mbImportBroadcast = b; }
158 : const ScExportTextOptions& GetExportTextOptions() { return mExportTextOptions; }
159 10 : void SetExportTextOptions( const ScExportTextOptions& options ) { mExportTextOptions = options; }
160 : };
161 :
162 0 : inline bool ScImportExport::IsEndianSwap( const SvStream& rStrm )
163 : {
164 : #ifdef OSL_BIGENDIAN
165 : return rStrm.GetNumberFormatInt() != NUMBERFORMAT_INT_BIGENDIAN;
166 : #else
167 0 : return rStrm.GetNumberFormatInt() != NUMBERFORMAT_INT_LITTLEENDIAN;
168 : #endif
169 : }
170 :
171 0 : inline void ScImportExport::SetNoEndianSwap( SvStream& rStrm )
172 : {
173 : #ifdef OSL_BIGENDIAN
174 : rStrm.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
175 : #else
176 0 : rStrm.SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
177 : #endif
178 0 : }
179 :
180 : // Helper class for importing clipboard strings as streams.
181 2 : class ScImportStringStream : public SvMemoryStream
182 : {
183 : public:
184 2 : ScImportStringStream( const OUString& rStr )
185 2 : : SvMemoryStream( (void*)rStr.getStr(),
186 4 : rStr.getLength() * sizeof(sal_Unicode), STREAM_READ)
187 : {
188 2 : SetStreamCharSet( RTL_TEXTENCODING_UNICODE );
189 2 : SetEndianSwap( false );
190 2 : }
191 : };
192 :
193 : /** Read a CSV (comma separated values) data line using
194 : ReadUniOrByteStringLine().
195 :
196 : @param bEmbeddedLineBreak
197 : If TRUE and a line-break occurs inside a field of data,
198 : a line feed LF '\n' and the next line are appended. Repeats
199 : until a line-break is not in a field. A field is determined
200 : by delimiting rFieldSeparators and optionally surrounded by
201 : a pair of cFieldQuote characters. For a line-break to be
202 : within a field, the field content MUST be surrounded by
203 : cFieldQuote characters, and the opening cFieldQuote MUST be
204 : at the very start of a line or follow right behind a field
205 : separator with no extra characters in between, with the
206 : exception of blanks contradictory to RFC 4180. Anything,
207 : including field separators and escaped quotes (by doubling
208 : them) may appear in a quoted field.
209 :
210 : If bEmbeddedLineBreak==FALSE, nothing is parsed and the
211 : string returned is simply one ReadUniOrByteStringLine().
212 :
213 : @param rFieldSeparators
214 : A list of characters that each may act as a field separator.
215 :
216 : @param cFieldQuote
217 : The quote character used.
218 :
219 : check Stream::good() to detect IO problems during read
220 :
221 : @ATTENTION
222 : Note that the string returned may be truncated even inside
223 : a quoted field if some (arbritary) maximum length was reached.
224 : There currently is no way to exactly determine the conditions,
225 : whether this was at a line end, or whether open quotes
226 : would have closed the field before the line end, as even a
227 : ReadUniOrByteStringLine() may return prematurely but the
228 : stream was positioned ahead until the real end of line.
229 : Additionally, due to character encoding conversions, string
230 : length and bytes read don't necessarily match, and
231 : resyncing to a previous position matching the string's
232 : length isn't always possible. As a result, a logical line
233 : with embedded line breaks and more than the maximum length
234 : characters will be spoiled, and a subsequent ReadCsvLine()
235 : may start under false preconditions.
236 :
237 : */
238 : SC_DLLPUBLIC OUString ReadCsvLine( SvStream &rStream, bool bEmbeddedLineBreak,
239 : const OUString& rFieldSeparators, sal_Unicode cFieldQuote );
240 :
241 : #endif
242 :
243 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|