LibreOffice Module sc (master) 1
Typedefs | Functions | Variables
impex.cxx File Reference
#include <comphelper/processfactory.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <i18nutil/unicode.hxx>
#include <sot/formats.hxx>
#include <sfx2/mieclip.hxx>
#include <com/sun/star/i18n/CalendarFieldIndex.hpp>
#include <sal/log.hxx>
#include <unotools/charclass.hxx>
#include <osl/module.hxx>
#include <o3tl/string_view.hxx>
#include <global.hxx>
#include <docsh.hxx>
#include <undoblk.hxx>
#include <rangenam.hxx>
#include <tabvwsh.hxx>
#include <filter.hxx>
#include <asciiopt.hxx>
#include <formulacell.hxx>
#include <cellform.hxx>
#include <progress.hxx>
#include <scitems.hxx>
#include <editable.hxx>
#include <compiler.hxx>
#include <warnbox.hxx>
#include <clipparam.hxx>
#include <impex.hxx>
#include <editutil.hxx>
#include <patattr.hxx>
#include <docpool.hxx>
#include <stringutil.hxx>
#include <cellvalue.hxx>
#include <tokenarray.hxx>
#include <documentimport.hxx>
#include <refundo.hxx>
#include <mtvelements.hxx>
#include <globstr.hrc>
#include <scresid.hxx>
#include <o3tl/safeint.hxx>
#include <tools/svlibrary.h>
#include <unotools/configmgr.hxx>
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <editeng/editobj.hxx>
#include <svl/numformat.hxx>
#include <rtl/character.hxx>
#include <rtl/math.hxx>
#include <sax/tools/converter.hxx>
#include <memory>
#include <string_view>
#include <unicode/uchar.h>
#include <osl/endian.h>
Include dependency graph for impex.cxx:

Go to the source code of this file.


typedef ScFormatFilterPlugin *(* FilterFn) ()


static bool lcl_isFieldEnd (sal_Unicode c, const sal_Unicode *pSeps)
static QuoteType lcl_isFieldEndQuote (const sal_Unicode *p, const sal_Unicode *pSeps, sal_Unicode &rcDetectSep)
 Determine if *p is a quote that ends a quoted field. More...
static QuoteType lcl_isEscapedOrFieldEndQuote (sal_Int32 nQuotes, const sal_Unicode *p, const sal_Unicode *pSeps, sal_Unicode cStr, sal_Unicode &rcDetectSep)
 Determine if *p is a quote that is escaped by being doubled or ends a quoted field. More...
static bool lcl_appendLineData (OUString &rField, const sal_Unicode *p1, const sal_Unicode *p2)
 Append characters of [p1,p2) to rField. More...
static const sal_Unicodelcl_ScanString (const sal_Unicode *p, OUString &rField, const sal_Unicode *pSeps, sal_Unicode cStr, DoubledQuoteMode eMode, bool &rbOverflowCell)
 Scan for a quoted string. More...
static void lcl_UnescapeSylk (OUString &rString, SylkVersion eVersion)
static const sal_Unicodelcl_ScanSylkString (const sal_Unicode *p, OUString &rString, SylkVersion eVersion)
static const sal_Unicodelcl_ScanSylkFormula (const sal_Unicode *p, OUString &rString, SylkVersion eVersion)
static void lcl_WriteString (SvStream &rStrm, OUString &rString, sal_Unicode cQuote, sal_Unicode cEsc)
static void lcl_WriteSimpleString (SvStream &rStrm, std::u16string_view rString)
static bool lcl_PutString (ScDocumentImport &rDocImport, bool bUseDocImport, SCCOL nCol, SCROW nRow, SCTAB nTab, const OUString &rStr, sal_uInt8 nColFormat, SvNumberFormatter *pFormatter, bool bDetectNumFormat, bool bDetectSciNumFormat, bool bEvaluateFormulas, bool bSkipEmptyCells, const ::utl::TransliterationWrapper &rTransliteration, CalendarWrapper &rCalendar, const ::utl::TransliterationWrapper *pSecondTransliteration, CalendarWrapper *pSecondCalendar)
static OUString lcl_GetFixed (const OUString &rLine, sal_Int32 nStart, sal_Int32 nNext, bool &rbIsQuoted, bool &rbOverflowCell)
static void thisModule ()
static const sal_Unicodelcl_UnicodeStrChr (const sal_Unicode *pStr, sal_Unicode c)
OUString ReadCsvLine (SvStream &rStream, bool bEmbeddedLineBreak, OUString &rFieldSeparators, sal_Unicode cFieldQuote, sal_Unicode &rcDetectSep, sal_uInt32 nMaxSourceLines)
 Read a CSV (comma separated values) data line using ReadUniOrByteStringLine(). More...


constexpr sal_Int32 nArbitraryCellLengthLimit = SAL_MAX_UINT16
constexpr sal_Int32 nArbitraryLineLengthLimit = 2 * MAXCOLCOUNT * nArbitraryCellLengthLimit

Typedef Documentation

◆ FilterFn

typedef ScFormatFilterPlugin *(* FilterFn) ()

Definition at line 2683 of file impex.cxx.

Function Documentation

◆ lcl_appendLineData()

static bool lcl_appendLineData ( OUString &  rField,
const sal_Unicode p1,
const sal_Unicode p2 

Append characters of [p1,p2) to rField.

TRUE if ok; FALSE if data overflow, truncated

Definition at line 710 of file impex.cxx.

References nArbitraryCellLengthLimit, and SAL_WARN.

Referenced by lcl_ScanString(), ScImportExport::ScanNextFieldFromString(), and ScImportExport::Text2Doc().

◆ lcl_GetFixed()

static OUString lcl_GetFixed ( const OUString &  rLine,
sal_Int32  nStart,
sal_Int32  nNext,
bool &  rbIsQuoted,
bool &  rbOverflowCell 

Definition at line 1535 of file impex.cxx.

Referenced by ScImportExport::ExtText2Doc().

◆ lcl_isEscapedOrFieldEndQuote()

static QuoteType lcl_isEscapedOrFieldEndQuote ( sal_Int32  nQuotes,
const sal_Unicode p,
const sal_Unicode pSeps,
sal_Unicode  cStr,
sal_Unicode rcDetectSep 

Determine if *p is a quote that is escaped by being doubled or ends a quoted field.

Precondition: *p is a quote.

nQuotesQuote characters encountered so far. Odd (after opening quote) means either no embedded quotes or only quote pairs so far. Even means either not in a quoted field or already one quote encountered, the first of a pair.
FIELDSTART_QUOTE if first quote in a field, either starting content or embedded so caller should check beforehand. FIRST_QUOTE if first of a doubled quote SECOND_QUOTE if second of a doubled quote FIELDEND_QUOTE if end of field quote DONTKNOW_QUOTE if an unescaped quote we don't consider as end of field, do not increment nQuotes in caller then!

Definition at line 688 of file impex.cxx.

References lcl_isFieldEndQuote(), p, and SAL_WARN.

Referenced by ReadCsvLine().

◆ lcl_isFieldEnd()

static bool lcl_isFieldEnd ( sal_Unicode  c,
const sal_Unicode pSeps 

Definition at line 599 of file impex.cxx.

References ScGlobal::UnicodeStrChr().

Referenced by lcl_isFieldEndQuote(), and ScImportExport::ScanNextFieldFromString().

◆ lcl_isFieldEndQuote()

static QuoteType lcl_isFieldEndQuote ( const sal_Unicode p,
const sal_Unicode pSeps,
sal_Unicode rcDetectSep 

Determine if *p is a quote that ends a quoted field.

Precondition: we are parsing a quoted field already and *p is a quote.

FIELDEND_QUOTE if end of field quote DONTKNOW_QUOTE anything else

Definition at line 625 of file impex.cxx.

References lcl_isFieldEnd(), p, and ScGlobal::UnicodeStrChr().

Referenced by lcl_isEscapedOrFieldEndQuote(), and lcl_ScanString().

◆ lcl_PutString()

static bool lcl_PutString ( ScDocumentImport rDocImport,
bool  bUseDocImport,
SCCOL  nCol,
SCROW  nRow,
SCTAB  nTab,
const OUString &  rStr,
sal_uInt8  nColFormat,
SvNumberFormatter pFormatter,
bool  bDetectNumFormat,
bool  bDetectSciNumFormat,
bool  bEvaluateFormulas,
bool  bSkipEmptyCells,
const ::utl::TransliterationWrapper rTransliteration,
CalendarWrapper rCalendar,
const ::utl::TransliterationWrapper pSecondTransliteration,
CalendarWrapper pSecondCalendar 

◆ lcl_ScanString()

static const sal_Unicode * lcl_ScanString ( const sal_Unicode p,
OUString &  rField,
const sal_Unicode pSeps,
sal_Unicode  cStr,
DoubledQuoteMode  eMode,
bool &  rbOverflowCell 

Scan for a quoted string.

Precondition: initial current position *p is a cStr quote.

For DoubledQuoteMode::ESCAPE, if after the closing quote there is a field end (with or without trailing blanks and as determined by lcl_isFieldEndQuote()), then the content is appended to rField with quotes processed and removed. Else if no field end after the quoted string was detected, nothing is appended and processing continues and is repeated until the next quote. If no closing quote at a field end was found at all, nothing is appended and the initial position is returned and caller has to decide, usually just taking all as literal data.

For DoubledQuoteMode::KEEP_ALL, the string up to and including the closing quote is appended to rField and the next position returned, regardless whether there is a field separator following or not.

jump over opening quote

Definition at line 753 of file impex.cxx.

References eMode, lcl_appendLineData(), lcl_isFieldEndQuote(), and p.

Referenced by ScImportExport::ScanNextFieldFromString(), and ScImportExport::Text2Doc().

◆ lcl_ScanSylkFormula()

static const sal_Unicode * lcl_ScanSylkFormula ( const sal_Unicode p,
OUString &  rString,
SylkVersion  eVersion 

Definition at line 888 of file impex.cxx.

References lcl_ScanSylkString(), lcl_UnescapeSylk(), and p.

Referenced by ScImportExport::Sylk2Doc().

◆ lcl_ScanSylkString()

static const sal_Unicode * lcl_ScanSylkString ( const sal_Unicode p,
OUString &  rString,
SylkVersion  eVersion 

Definition at line 846 of file impex.cxx.

References lcl_UnescapeSylk(), and p.

Referenced by lcl_ScanSylkFormula(), and ScImportExport::Sylk2Doc().

◆ lcl_UnescapeSylk()

static void lcl_UnescapeSylk ( OUString &  rString,
SylkVersion  eVersion 

Definition at line 833 of file impex.cxx.

Referenced by lcl_ScanSylkFormula(), and lcl_ScanSylkString().

◆ lcl_UnicodeStrChr()

static const sal_Unicode * lcl_UnicodeStrChr ( const sal_Unicode pStr,
sal_Unicode  c 

Definition at line 2714 of file impex.cxx.

Referenced by ReadCsvLine().

◆ lcl_WriteSimpleString()

static void lcl_WriteSimpleString ( SvStream rStrm,
std::u16string_view  rString 

◆ lcl_WriteString()

static void lcl_WriteString ( SvStream rStrm,
OUString &  rString,
sal_Unicode  cQuote,
sal_Unicode  cEsc 

◆ ReadCsvLine()

OUString ReadCsvLine ( SvStream rStream,
bool  bEmbeddedLineBreak,
OUString &  rFieldSeparators,
sal_Unicode  cFieldQuote,
sal_Unicode rcDetectSep,
sal_uInt32  nMaxSourceLines = 0 

Read a CSV (comma separated values) data line using ReadUniOrByteStringLine().

bEmbeddedLineBreakIf TRUE and a line-break occurs inside a field of data, a line feed LF '
' and the next line are appended. Repeats until a line-break is not in a field. A field is determined by delimiting rFieldSeparators and optionally surrounded by a pair of cFieldQuote characters. For a line-break to be within a field, the field content MUST be surrounded by cFieldQuote characters, and the opening cFieldQuote MUST be at the very start of a line or follow right behind a field separator with no extra characters in between, with the exception of blanks contradictory to RFC 4180. Anything, including field separators and escaped quotes (by doubling them) may appear in a quoted field.

If bEmbeddedLineBreak==FALSE, nothing is parsed and the string returned is simply one ReadUniOrByteStringLine().

rFieldSeparatorsA list of characters that each may act as a field separator. If rcDetectSep was 0 and a separator is detected then it is appended to rFieldSeparators.
cFieldQuoteThe quote character used.
rcDetectSepIf 0 then attempt to detect a possible separator if rFieldSeparators doesn't include it already. This can be necessary because of the "accept broken misquoted CSV fields" feature that tries to ignore trailing blanks after a quoted field and if no separator follows continues to add content to the field assuming the single double quote was in error. It is also necessary if the only possible separator was not selected and not included in rFieldSeparators and a line starts with a quoted field, in which case appending lines is tried until end of file. If a separator is detected it is added to rFieldSeparators and the line is reread with the new separators
nMaxSourceLinesMaximum source lines to read and combine into one logical line for embedded new line purpose. Should be limited for the preview dialog because only non-matching separators selected otherwise would lead to trying to concatenate lines until file end. If 0 no limit other than the internal arbitrary resulting line length limit.

check Stream::good() to detect IO problems during read

@ATTENTION Note that the string returned may be truncated even inside a quoted field if some (arbitrary) maximum length was reached. There currently is no way to exactly determine the conditions, whether this was at a line end, or whether open quotes would have closed the field before the line end, as even a ReadUniOrByteStringLine() may return prematurely but the stream was positioned ahead until the real end of line. Additionally, due to character encoding conversions, string length and bytes read don't necessarily match, and resyncing to a previous position matching the string's length isn't always possible. As a result, a logical line with embedded line breaks and more than the maximum length characters will be spoiled, and a subsequent ReadCsvLine() may start under false preconditions.

Definition at line 2738 of file impex.cxx.

References aStr, SvStream::eof(), SvStream::GetStreamCharSet(), lcl_isEscapedOrFieldEndQuote(), lcl_UnicodeStrChr(), nArbitraryLineLengthLimit, p, SvStream::ReadUniOrByteStringLine(), SvStream::Seek(), and SvStream::Tell().

Referenced by ScImportExport::ExtText2Doc(), and ScImportAsciiDlg::GetLine().

◆ thisModule()

static void thisModule ( )

Definition at line 2673 of file impex.cxx.

References thisModule().

Referenced by ScFormatFilter::Get(), and thisModule().

Variable Documentation

◆ nArbitraryCellLengthLimit

constexpr sal_Int32 nArbitraryCellLengthLimit = SAL_MAX_UINT16

Definition at line 82 of file impex.cxx.

Referenced by lcl_appendLineData().

◆ nArbitraryLineLengthLimit

constexpr sal_Int32 nArbitraryLineLengthLimit = 2 * MAXCOLCOUNT * nArbitraryCellLengthLimit

Definition at line 83 of file impex.cxx.

Referenced by ReadCsvLine(), and ScImportExport::Text2Doc().