/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
//! TODO make dynamic const SCSIZE ASCIIDLG_MAXROWS = MAXROWCOUNT;
// Maximum number of source lines to concatenate while generating the preview // for one logical line. This may result in a wrong preview if the actual // number of embedded line feeds is greater, but a number too high would take // too much time (loop excessively if unlimited and large data) if none of the // selected separators are actually used in data but a field at start of line // is quoted.
constexpr sal_uInt32 kMaxEmbeddedLinefeeds = 500;
usingnamespace com::sun::star::uno;
namespace { // The values of this enum are stored in config's "SeparatorType" node enum SeparatorType
{
FIXED = 0,
SEPARATOR = 1,
DETECT_SEPARATOR = 2,
};
}
// Config items for all three paths are defined in // officecfg/registry/schema/org/openoffice/Office/Calc.xcs // If not, options are neither loaded nor saved.
constexpr OUString CSVIO_MergeDelimiters = u"MergeDelimiters"_ustr;
constexpr OUString CSVIO_Separators = u"Separators"_ustr;
constexpr OUString CSVIO_TextSeparators = u"TextSeparators"_ustr;
constexpr OUString CSVIO_RemoveSpace = u"RemoveSpace"_ustr;
constexpr OUString CSVIO_EvaluateFormulas = u"EvaluateFormulas"_ustr;
constexpr OUString CSVIO_SeparatorType = u"SeparatorType"_ustr;
constexpr OUString CSVIO_FromRow = u"FromRow"_ustr;
constexpr OUString CSVIO_Encoding = u"Encoding"_ustr;
constexpr OUString CSVIO_QuotedAsText = u"QuotedFieldAsText"_ustr;
constexpr OUString CSVIO_DetectSpecialNum = u"DetectSpecialNumbers"_ustr;
constexpr OUString CSVIO_DetectScientificNum = u"DetectScientificNumbers"_ustr;
constexpr OUString CSVIO_Language = u"Language"_ustr;
constexpr OUString CSVIO_SkipEmptyCells = u"SkipEmptyCells"_ustr;
// To be able to prefill the correct values based on the file extension bool bIsTSV = (o3tl::endsWithIgnoreAsciiCase(aDatName, ".tsv") || o3tl::endsWithIgnoreAsciiCase(aDatName, ".tab"));
if( bMergeDelimiters && !bIsTSV )
mxCkbMergeDelimiters->set_active(true); if (bQuotedFieldAsText)
mxCkbQuotedAsText->set_active(true); if (bRemoveSpace)
mxCkbRemoveSpace->set_active(true); if (bDetectSpecialNum)
{
mxCkbDetectNumber->set_active(true);
bDetectScientificNum = true;
mxCkbDetectScientificNumber->set_sensitive(false);
} if (bDetectScientificNum)
mxCkbDetectScientificNumber->set_active(true); if (bEvaluateFormulas)
mxCkbEvaluateFormulas->set_active(true); if (bSkipEmptyCells)
mxCkbSkipEmptyCells->set_active(true); if (eSepType == SeparatorType::FIXED)
{ if (bIsTSV)
{
eSepType = SeparatorType::SEPARATOR;
mxRbSeparated->set_active(true);
} else
mxRbFixed->set_active(true);
} elseif (eSepType == SeparatorType::SEPARATOR)
mxRbSeparated->set_active(true); else
mxRbDetectSep->set_active(true);
// Detect character set only once and then use it for "Detect" option.
SvStreamEndian eEndian;
SfxObjectShell::DetectCharSet(*mpDatStream, meDetectedCharSet, eEndian); if (meDetectedCharSet == RTL_TEXTENCODING_UNICODE)
mpDatStream->SetEndian(eEndian); elseif ( meDetectedCharSet == RTL_TEXTENCODING_DONTKNOW )
{
meDetectedCharSet = osl_getThreadTextEncoding(); // Prefer UTF-8, as UTF-16 would have already been detected from the stream. // This gives a better chance that the file is going to be opened correctly. if ( meDetectedCharSet == RTL_TEXTENCODING_UNICODE && mpDatStream )
meDetectedCharSet = RTL_TEXTENCODING_UTF8;
}
if (bIsTSV)
SetSeparators('\t'); else
SetSeparators(0);
// Get Separators from the dialog (empty are set from default)
maFieldSeparators = GetActiveSeparators();
// *** Separator characters ***
lcl_FillCombo( *mxCbTextSep, SCSTR_TEXTSEP, mcTextSep );
mxCbTextSep->set_entry_text(sTextSeparators); // tdf#69207 - use selected text delimiter to parse the provided data
mcTextSep = lcl_CharFromCombo(*mxCbTextSep, SCSTR_TEXTSEP);
// *** text encoding ListBox *** // all encodings allowed, including Unicode, but subsets are excluded
mxLbCharSet->FillFromTextEncodingTable( true ); // Insert one "SYSTEM" entry for compatibility in AsciiOptions and system // independent document linkage.
mxLbCharSet->InsertTextEncoding( RTL_TEXTENCODING_DONTKNOW, ScResId( SCSTR_CHARSET_USER ) ); // Insert one for detecting charset.
mxLbCharSet->InsertTextEncoding( RTL_TEXTENCODING_USER_DETECTED, "- " + ScResId( SCSTR_AUTOMATIC ) + " -" );
// Clipboard is always Unicode, and TextToColumns doesn't use encoding. if (meCall != SC_IMPORTFILE)
eEncoding = RTL_TEXTENCODING_UNICODE; elseif (eEncoding == RTL_TEXTENCODING_DONTKNOW)
eEncoding = RTL_TEXTENCODING_USER_DETECTED;
if (nLine >= mnRowPosCount)
{ // need to work out some more line information do
{ if (!Seek(mpRowPosArray[mnRowPosCount]) || !mpDatStream->good())
{
bRet = false; break;
}
rText = ReadCsvLine(*mpDatStream, !bFixed, maFieldSeparators,
mcTextSep, rcDetectSep, kMaxEmbeddedLinefeeds);
mnStreamPos = mpDatStream->Tell();
mpRowPosArray[++mnRowPosCount] = mnStreamPos;
} while (nLine >= mnRowPosCount && mpDatStream->good()); if (mpDatStream->eof() && mnRowPosCount &&
mnStreamPos == mpRowPosArray[mnRowPosCount-1])
{ // the very end, not even an empty line read
bRet = false;
--mnRowPosCount;
}
} else
{
Seek( mpRowPosArray[nLine]);
rText = ReadCsvLine(*mpDatStream, !bFixed, maFieldSeparators, mcTextSep, rcDetectSep, kMaxEmbeddedLinefeeds);
mnStreamPos = mpDatStream->Tell();
}
// If the file content isn't unicode, ReadUniStringLine // may try to seek beyond the file's end and cause a CANTSEEK error // (depending on the stream type). The error code has to be cleared, // or further read operations (including non-unicode) will fail. if ( mpDatStream->GetError() == ERRCODE_IO_CANTSEEK )
mpDatStream->ResetError();
/* #i41550# First update state of the controls. The GetSeparators()
function needs final state of the check boxes. */ if (pCtrl == mxCkbOther.get() && mxCkbOther->get_active())
mxEdOther->grab_focus(); elseif (pCtrl == mxEdOther.get())
mxCkbOther->set_active(!mxEdOther->get_text().isEmpty());
OUString aOldFldSeps( maFieldSeparators);
sal_Unicode cOldSep = mcTextSep;
mcTextSep = lcl_CharFromCombo( *mxCbTextSep, SCSTR_TEXTSEP ); // Any separator changed may result in completely different lines due to // embedded line breaks. if (cOldSep != mcTextSep)
{
DetectCsvSeparators();
sal_Int32 nBaseLine = mxTableBox->GetGrid().GetFirstVisLine();
sal_Int32 nRead = mxTableBox->GetGrid().GetVisLineCount(); // If mnRowPosCount==0, this is an initializing call, read ahead for row // count and resulting scroll bar size and position to be able to scroll at // all. When adding lines, read only the amount of next lines to be // displayed. if (!mnRowPosCount || nRead > CSV_PREVIEW_LINES)
nRead = CSV_PREVIEW_LINES;
sal_Int32 i; for (i = 0; i < nRead; i++)
{ if (!GetLine( nBaseLine + i, maPreviewLine[i], cDetectSep)) break;
} for (; i < CSV_PREVIEW_LINES; i++)
maPreviewLine[i].clear();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.