diff options
author | Matúš Kukan <matus.kukan@collabora.com> | 2013-11-27 18:00:42 +0100 |
---|---|---|
committer | Matúš Kukan <matus.kukan@collabora.com> | 2013-12-02 11:13:45 +0100 |
commit | 80b9161534e3d03709bc2f41d6eaea421846042b (patch) | |
tree | 26e8eea5c5ec2c7a63b0650df80c4943c73a1e7f | |
parent | 3428ccb269d752bcfcb96228b8fb432fcb883924 (diff) |
datastreams: Simplify code for importing CSV data.
The code is copied and simplified from sc/source/ui/docshell/impex.cxx.
Change-Id: I15a426a16c4f2ba659af86492c1b2e4ba879d8b6
-rw-r--r-- | sc/source/ui/docshell/datastream.cxx | 89 | ||||
-rw-r--r-- | sc/source/ui/inc/datastream.hxx | 1 |
2 files changed, 77 insertions, 13 deletions
diff --git a/sc/source/ui/docshell/datastream.cxx b/sc/source/ui/docshell/datastream.cxx index 88c2ae40b45c..358879af8438 100644 --- a/sc/source/ui/docshell/datastream.cxx +++ b/sc/source/ui/docshell/datastream.cxx @@ -315,22 +315,87 @@ IMPL_LINK_NOARG(DataStream, RefreshHdl) 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() { MoveData(); if (mbValuesInLine) { - SCROW nHeight = maRange.aEnd.Row() - maRange.aStart.Row() + 1; - OStringBuffer aBuf; - 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); + // do CSV import + Text2Doc(); } else { @@ -359,8 +424,6 @@ bool DataStream::ImportData() aDocImport.finalize(); 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) return mbRunning; diff --git a/sc/source/ui/inc/datastream.hxx b/sc/source/ui/inc/datastream.hxx index 039cc4833119..b9610628d54d 100644 --- a/sc/source/ui/inc/datastream.hxx +++ b/sc/source/ui/inc/datastream.hxx @@ -32,6 +32,7 @@ class DataStream : boost::noncopyable, public sfx2::SvBaseLink, ScRefreshTimer { OString ConsumeLine(); void MoveData(); + void Text2Doc(); DECL_LINK( RefreshHdl, void* ); public: |