Kaydet (Commit) 2f4a1487 authored tarafından Matúš Kukan's avatar Matúš Kukan

datastreams: Simplify code for importing CSV data.

The code is copied and simplified from sc/source/ui/docshell/impex.cxx.

Change-Id: I15a426a16c4f2ba659af86492c1b2e4ba879d8b6
üst 1e719646
...@@ -315,22 +315,87 @@ IMPL_LINK_NOARG(DataStream, RefreshHdl) ...@@ -315,22 +315,87 @@ IMPL_LINK_NOARG(DataStream, RefreshHdl)
return 0; return 0;
} }
// lcl_ScanString and Text2Doc is simplified version
// of code from sc/source/ui/docshell/impex.cxx
const sal_Unicode* lcl_ScanString( const sal_Unicode* p, OUString& rString, sal_Unicode cStr)
{
const sal_Unicode* p0 = p;
for( ;; )
{
if (!*p)
break;
if (*p == cStr)
{
if (*++p != cStr)
break;
p++;
}
else
p++;
}
if (p0 < p)
if (rString.getLength() + (p - p0) <= STRING_MAXLEN)
rString += OUString( p0, sal::static_int_cast<sal_Int32>( p - p0 ) );
return p;
}
void DataStream::Text2Doc()
{
sal_Unicode cSep(',');
sal_Unicode cStr('"');
SCCOL nStartCol = maRange.aStart.Col();
SCROW nStartRow = maRange.aStart.Row();
SCCOL nEndCol = maRange.aEnd.Col();
SCROW nEndRow = maRange.aEnd.Row();
OUString aCell;
SCROW nRow = nStartRow;
ScDocumentImport aDocImport(*mpScDocument);
while (nRow <= nEndRow)
{
SCCOL nCol = nStartCol;
OUString sLine( OStringToOUString(ConsumeLine(), RTL_TEXTENCODING_UTF8) );
const sal_Unicode* p = sLine.getStr();
while (*p)
{
aCell = "";
const sal_Unicode* q = p;
while (*p && *p != cSep)
{
// Always look for a pairing quote and ignore separator in between.
while (*p && *p == cStr)
q = p = lcl_ScanString(p, aCell, cStr);
// All until next separator or quote.
while (*p && *p != cSep && *p != cStr)
++p;
if (aCell.getLength() + (p - q) <= STRING_MAXLEN)
aCell += OUString( q, sal::static_int_cast<sal_Int32>( p - q ) );
q = p;
}
if (*p)
++p;
if (nCol <= nEndCol && nRow <= nEndRow)
{
ScAddress aAddress(nCol, nRow, maRange.aStart.Tab());
if (aCell == "0" || ( aCell.indexOf(':') == -1 && aCell.toDouble() ))
aDocImport.setNumericCell(aAddress, aCell.toDouble());
else
aDocImport.setStringCell(aAddress, aCell);
}
++nCol;
}
++nRow;
}
aDocImport.finalize();
mpScDocShell->PostPaint( maRange, PAINT_GRID );
}
bool DataStream::ImportData() bool DataStream::ImportData()
{ {
MoveData(); MoveData();
if (mbValuesInLine) if (mbValuesInLine)
{ {
SCROW nHeight = maRange.aEnd.Row() - maRange.aStart.Row() + 1; // do CSV import
OStringBuffer aBuf; Text2Doc();
while (nHeight--)
{
aBuf.append(ConsumeLine());
aBuf.append('\n');
}
SvMemoryStream aMemoryStream((void *)aBuf.getStr(), aBuf.getLength(), STREAM_READ);
ScImportExport aImport(mpScDocument, maRange);
aImport.SetSeparator(',');
aImport.ImportStream(aMemoryStream, OUString(), FORMAT_STRING);
} }
else else
{ {
...@@ -359,8 +424,6 @@ bool DataStream::ImportData() ...@@ -359,8 +424,6 @@ bool DataStream::ImportData()
aDocImport.finalize(); aDocImport.finalize();
mpScDocShell->PostPaint( aRangeList, PAINT_GRID ); mpScDocShell->PostPaint( aRangeList, PAINT_GRID );
} }
// ImportStream calls PostPaint for relevant area,
// we need to call it explicitly only when moving rows.
if (meMove == NO_MOVE) if (meMove == NO_MOVE)
return mbRunning; return mbRunning;
......
...@@ -32,6 +32,7 @@ class DataStream : boost::noncopyable, public sfx2::SvBaseLink, ScRefreshTimer ...@@ -32,6 +32,7 @@ class DataStream : boost::noncopyable, public sfx2::SvBaseLink, ScRefreshTimer
{ {
OString ConsumeLine(); OString ConsumeLine();
void MoveData(); void MoveData();
void Text2Doc();
DECL_LINK( RefreshHdl, void* ); DECL_LINK( RefreshHdl, void* );
public: public:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment