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