[Libreoffice-commits] core.git: sc/Library_sc.mk sc/source
Markus Mohrhard
markus.mohrhard at googlemail.com
Sat Aug 12 11:23:40 UTC 2017
sc/Library_sc.mk | 3
sc/source/ui/dataprovider/csvdataprovider.cxx | 223 ++++++++++++++++++++++++++
sc/source/ui/dataprovider/dataprovider.cxx | 185 ---------------------
sc/source/ui/inc/dataprovider.hxx | 39 ----
4 files changed, 232 insertions(+), 218 deletions(-)
New commits:
commit 8a5f9baf4113ce9c160f8bef3230cfe4a0a1d1e1
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date: Fri Aug 11 08:35:46 2017 +0200
external data: move code to own directory and split file up
Change-Id: Ia1037c7b80c492585fb903e712d1743ed2ed00d6
Reviewed-on: https://gerrit.libreoffice.org/41082
Tested-by: Jenkins <ci at libreoffice.org>
Reviewed-by: Markus Mohrhard <markus.mohrhard at googlemail.com>
diff --git a/sc/Library_sc.mk b/sc/Library_sc.mk
index 4bc99c5b939d..ffe9b96bc488 100644
--- a/sc/Library_sc.mk
+++ b/sc/Library_sc.mk
@@ -383,6 +383,8 @@ $(eval $(call gb_Library_add_exception_objects,sc,\
sc/source/ui/condformat/condformatdlgitem \
sc/source/ui/condformat/condformathelper \
sc/source/ui/condformat/colorformat \
+ sc/source/ui/dataprovider/csvdataprovider \
+ sc/source/ui/dataprovider/dataprovider \
sc/source/ui/dbgui/asciiopt \
sc/source/ui/dbgui/consdlg \
sc/source/ui/dbgui/csvcontrol \
@@ -418,7 +420,6 @@ $(eval $(call gb_Library_add_exception_objects,sc,\
sc/source/ui/docshell/docsh8 \
sc/source/ui/docshell/documentlinkmgr \
sc/source/ui/docshell/editable \
- sc/source/ui/docshell/dataprovider \
sc/source/ui/docshell/externalrefmgr \
sc/source/ui/docshell/impex \
sc/source/ui/docshell/macromgr \
diff --git a/sc/source/ui/dataprovider/csvdataprovider.cxx b/sc/source/ui/dataprovider/csvdataprovider.cxx
new file mode 100644
index 000000000000..cd87d21c3de4
--- /dev/null
+++ b/sc/source/ui/dataprovider/csvdataprovider.cxx
@@ -0,0 +1,223 @@
+/* -*- 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/.
+ */
+
+#include <dataprovider.hxx>
+#include <stringutil.hxx>
+
+#if defined(_WIN32)
+#if !defined __ORCUS_STATIC_LIB // avoid -Werror,-Wunused-macros
+#define __ORCUS_STATIC_LIB
+#endif
+#endif
+#include <orcus/csv_parser.hpp>
+
+namespace {
+
+struct Cell
+{
+ struct Str
+ {
+ size_t Pos;
+ size_t Size;
+ };
+
+ union
+ {
+ Str maStr;
+ double mfValue;
+ };
+
+ bool mbValue;
+
+ Cell();
+ Cell( const Cell& r );
+};
+
+struct Line
+{
+ OString maLine;
+ std::vector<Cell> maCells;
+};
+
+Cell::Cell() : mfValue(0.0), mbValue(true) {}
+
+Cell::Cell(const Cell& r) : mbValue(r.mbValue)
+{
+ if (r.mbValue)
+ mfValue = r.mfValue;
+ else
+ {
+ maStr.Pos = r.maStr.Pos;
+ maStr.Size = r.maStr.Size;
+ }
+}
+
+class CSVHandler
+{
+ Line& mrLine;
+ size_t mnColCount;
+ size_t mnCols;
+ const char* mpLineHead;
+
+public:
+ CSVHandler( Line& rLine, size_t nColCount ) :
+ mrLine(rLine), mnColCount(nColCount), mnCols(0), mpLineHead(rLine.maLine.getStr()) {}
+
+ static void begin_parse() {}
+ static void end_parse() {}
+ static void begin_row() {}
+ static void end_row() {}
+
+ void cell(const char* p, size_t n)
+ {
+ if (mnCols >= mnColCount)
+ return;
+
+ Cell aCell;
+ if (ScStringUtil::parseSimpleNumber(p, n, '.', ',', aCell.mfValue))
+ {
+ aCell.mbValue = true;
+ }
+ else
+ {
+ aCell.mbValue = false;
+ aCell.maStr.Pos = std::distance(mpLineHead, p);
+ aCell.maStr.Size = n;
+ }
+ mrLine.maCells.push_back(aCell);
+
+ ++mnCols;
+ }
+};
+
+}
+
+namespace sc {
+
+CSVFetchThread::CSVFetchThread(ScDocument& rDoc, const OUString& mrURL, Idle* pIdle):
+ Thread("CSV Fetch Thread"),
+ mrDocument(rDoc),
+ maURL (mrURL),
+ mbTerminate(false),
+ mpIdle(pIdle)
+{
+ maConfig.delimiters.push_back(',');
+ maConfig.text_qualifier = '"';
+}
+
+CSVFetchThread::~CSVFetchThread()
+{
+}
+
+bool CSVFetchThread::IsRequestedTerminate()
+{
+ osl::MutexGuard aGuard(maMtxTerminate);
+ return mbTerminate;
+}
+
+void CSVFetchThread::RequestTerminate()
+{
+ osl::MutexGuard aGuard(maMtxTerminate);
+ mbTerminate = true;
+}
+
+void CSVFetchThread::EndThread()
+{
+ RequestTerminate();
+}
+
+void CSVFetchThread::execute()
+{
+ OStringBuffer aBuffer(64000);
+ std::unique_ptr<SvStream> pStream = DataProvider::FetchStreamFromURL(maURL, aBuffer);
+ SCROW nCurRow = 0;
+ SCCOL nCol = 0;
+ while (pStream->good())
+ {
+ if (mbTerminate)
+ break;
+
+ Line aLine;
+ aLine.maCells.clear();
+ pStream->ReadLine(aLine.maLine);
+ CSVHandler aHdl(aLine, MAXCOL);
+ orcus::csv_parser<CSVHandler> parser(aLine.maLine.getStr(), aLine.maLine.getLength(), aHdl, maConfig);
+ parser.parse();
+
+ if (aLine.maCells.empty())
+ {
+ break;
+ }
+
+ nCol = 0;
+ const char* pLineHead = aLine.maLine.getStr();
+ for (auto& rCell : aLine.maCells)
+ {
+ if (rCell.mbValue)
+ {
+ mrDocument.SetValue(ScAddress(nCol, nCurRow, 0 /* Tab */), rCell.mfValue);
+ }
+ else
+ {
+ mrDocument.SetString(nCol, nCurRow, 0 /* Tab */, OUString(pLineHead+rCell.maStr.Pos, rCell.maStr.Size, RTL_TEXTENCODING_UTF8));
+ }
+ ++nCol;
+ }
+ nCurRow++;
+ }
+ SolarMutexGuard aGuard;
+ mpIdle->Start();
+}
+
+CSVDataProvider::CSVDataProvider(ScDocument* pDoc, const OUString& rURL, ScDBDataManager* pBDDataManager):
+ maURL(rURL),
+ mpDocument(pDoc),
+ mpDBDataManager(pBDDataManager),
+ maIdle("CSVDataProvider CopyHandler")
+{
+ maIdle.SetInvokeHandler(LINK(this, CSVDataProvider, ImportFinishedHdl));
+}
+
+CSVDataProvider::~CSVDataProvider()
+{
+ if (mxCSVFetchThread.is())
+ {
+ mxCSVFetchThread->join();
+ }
+}
+
+void CSVDataProvider::Import()
+{
+ // already importing data
+ if (mpDoc)
+ return;
+
+ mpDoc.reset(new ScDocument(SCDOCMODE_CLIP));
+ mpDoc->ResetClip(mpDocument, (SCTAB)0);
+ mxCSVFetchThread = new CSVFetchThread(*mpDoc, maURL, &maIdle);
+ mxCSVFetchThread->launch();
+}
+
+IMPL_LINK_NOARG(CSVDataProvider, ImportFinishedHdl, Timer*, void)
+{
+ mpDBDataManager->WriteToDoc(*mpDoc);
+ mxCSVFetchThread.clear();
+ mpDoc.reset();
+ Refresh();
+}
+
+void CSVDataProvider::Refresh()
+{
+ ScDocShell* pDocShell = static_cast<ScDocShell*>(mpDocument->GetDocumentShell());
+ pDocShell->SetDocumentModified();
+}
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/docshell/dataprovider.cxx b/sc/source/ui/dataprovider/dataprovider.cxx
similarity index 58%
rename from sc/source/ui/docshell/dataprovider.cxx
rename to sc/source/ui/dataprovider/dataprovider.cxx
index aa6a4dde0e86..4c23420a18a4 100644
--- a/sc/source/ui/docshell/dataprovider.cxx
+++ b/sc/source/ui/dataprovider/dataprovider.cxx
@@ -12,23 +12,13 @@
#include <com/sun/star/ucb/SimpleFileAccess.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include "officecfg/Office/Calc.hxx"
-#include <stringutil.hxx>
#include <rtl/strbuf.hxx>
-#if defined(_WIN32)
-#if !defined __ORCUS_STATIC_LIB // avoid -Werror,-Wunused-macros
-#define __ORCUS_STATIC_LIB
-#endif
-#endif
-#include <orcus/csv_parser.hpp>
-
using namespace com::sun::star;
namespace sc {
-namespace {
-
-std::unique_ptr<SvStream> FetchStreamFromURL(const OUString& rURL, OStringBuffer& rBuffer)
+std::unique_ptr<SvStream> DataProvider::FetchStreamFromURL(const OUString& rURL, OStringBuffer& rBuffer)
{
uno::Reference< ucb::XSimpleFileAccess3 > xFileAccess( ucb::SimpleFileAccess::create( comphelper::getProcessComponentContext() ), uno::UNO_QUERY );
@@ -55,8 +45,6 @@ std::unique_ptr<SvStream> FetchStreamFromURL(const OUString& rURL, OStringBuffer
return std::unique_ptr<SvStream>(pStream);
}
-}
-
ExternalDataSource::ExternalDataSource(const OUString& rURL,
const OUString& rProvider, ScDocument* pDoc):
maURL(rURL),
@@ -162,177 +150,6 @@ DataProvider::~DataProvider()
{
}
-Cell::Cell() : mfValue(0.0), mbValue(true) {}
-
-Cell::Cell(const Cell& r) : mbValue(r.mbValue)
-{
- if (r.mbValue)
- mfValue = r.mfValue;
- else
- {
- maStr.Pos = r.maStr.Pos;
- maStr.Size = r.maStr.Size;
- }
-}
-
-class CSVHandler
-{
- Line& mrLine;
- size_t mnColCount;
- size_t mnCols;
- const char* mpLineHead;
-
-public:
- CSVHandler( Line& rLine, size_t nColCount ) :
- mrLine(rLine), mnColCount(nColCount), mnCols(0), mpLineHead(rLine.maLine.getStr()) {}
-
- static void begin_parse() {}
- static void end_parse() {}
- static void begin_row() {}
- static void end_row() {}
-
- void cell(const char* p, size_t n)
- {
- if (mnCols >= mnColCount)
- return;
-
- Cell aCell;
- if (ScStringUtil::parseSimpleNumber(p, n, '.', ',', aCell.mfValue))
- {
- aCell.mbValue = true;
- }
- else
- {
- aCell.mbValue = false;
- aCell.maStr.Pos = std::distance(mpLineHead, p);
- aCell.maStr.Size = n;
- }
- mrLine.maCells.push_back(aCell);
-
- ++mnCols;
- }
-};
-
-CSVFetchThread::CSVFetchThread(ScDocument& rDoc, const OUString& mrURL, Idle* pIdle):
- Thread("CSV Fetch Thread"),
- mrDocument(rDoc),
- maURL (mrURL),
- mbTerminate(false),
- mpIdle(pIdle)
-{
- maConfig.delimiters.push_back(',');
- maConfig.text_qualifier = '"';
-}
-
-CSVFetchThread::~CSVFetchThread()
-{
-}
-
-bool CSVFetchThread::IsRequestedTerminate()
-{
- osl::MutexGuard aGuard(maMtxTerminate);
- return mbTerminate;
-}
-
-void CSVFetchThread::RequestTerminate()
-{
- osl::MutexGuard aGuard(maMtxTerminate);
- mbTerminate = true;
-}
-
-void CSVFetchThread::EndThread()
-{
- RequestTerminate();
-}
-
-void CSVFetchThread::execute()
-{
- OStringBuffer aBuffer(64000);
- std::unique_ptr<SvStream> pStream = FetchStreamFromURL(maURL, aBuffer);
- SCROW nCurRow = 0;
- SCCOL nCol = 0;
- while (pStream->good())
- {
- if (mbTerminate)
- break;
-
- Line aLine;
- aLine.maCells.clear();
- pStream->ReadLine(aLine.maLine);
- CSVHandler aHdl(aLine, MAXCOL);
- orcus::csv_parser<CSVHandler> parser(aLine.maLine.getStr(), aLine.maLine.getLength(), aHdl, maConfig);
- parser.parse();
-
- if (aLine.maCells.empty())
- {
- break;
- }
-
- nCol = 0;
- const char* pLineHead = aLine.maLine.getStr();
- for (auto& rCell : aLine.maCells)
- {
- if (rCell.mbValue)
- {
- mrDocument.SetValue(ScAddress(nCol, nCurRow, 0 /* Tab */), rCell.mfValue);
- }
- else
- {
- mrDocument.SetString(nCol, nCurRow, 0 /* Tab */, OUString(pLineHead+rCell.maStr.Pos, rCell.maStr.Size, RTL_TEXTENCODING_UTF8));
- }
- ++nCol;
- }
- nCurRow++;
- }
- SolarMutexGuard aGuard;
- mpIdle->Start();
-}
-
-CSVDataProvider::CSVDataProvider(ScDocument* pDoc, const OUString& rURL, ScDBDataManager* pBDDataManager):
- maURL(rURL),
- mpDocument(pDoc),
- mpDBDataManager(pBDDataManager),
- mpLines(nullptr),
- mnLineCount(0),
- maIdle("CSVDataProvider CopyHandler")
-{
- maIdle.SetInvokeHandler(LINK(this, CSVDataProvider, ImportFinishedHdl));
-}
-
-CSVDataProvider::~CSVDataProvider()
-{
- if (mxCSVFetchThread.is())
- {
- mxCSVFetchThread->join();
- }
-}
-
-void CSVDataProvider::Import()
-{
- // already importing data
- if (mpDoc)
- return;
-
- mpDoc.reset(new ScDocument(SCDOCMODE_CLIP));
- mpDoc->ResetClip(mpDocument, (SCTAB)0);
- mxCSVFetchThread = new CSVFetchThread(*mpDoc, maURL, &maIdle);
- mxCSVFetchThread->launch();
-}
-
-IMPL_LINK_NOARG(CSVDataProvider, ImportFinishedHdl, Timer*, void)
-{
- mpDBDataManager->WriteToDoc(*mpDoc);
- mxCSVFetchThread.clear();
- mpDoc.reset();
- Refresh();
-}
-
-void CSVDataProvider::Refresh()
-{
- ScDocShell* pDocShell = static_cast<ScDocShell*>(mpDocument->GetDocumentShell());
- pDocShell->SetDocumentModified();
-}
-
void ScDBDataManager::WriteToDoc(ScDocument& rDoc)
{
bool bShrunk = false;
diff --git a/sc/source/ui/inc/dataprovider.hxx b/sc/source/ui/inc/dataprovider.hxx
index 659afffe2cd1..00fec69638fb 100644
--- a/sc/source/ui/inc/dataprovider.hxx
+++ b/sc/source/ui/inc/dataprovider.hxx
@@ -25,6 +25,7 @@
#include "docsh.hxx"
#include "scdllapi.h"
#include "datamapper.hxx"
+#include <rtl/strbuf.hxx>
#include <queue>
@@ -33,42 +34,17 @@
#if defined(_WIN32)
#define __ORCUS_STATIC_LIB
#endif
+
#include <orcus/csv_parser.hpp>
+class SvStream;
+
namespace sc {
class DataProvider;
class CSVDataProvider;
class ScDBDataManager;
-struct Cell
-{
- struct Str
- {
- size_t Pos;
- size_t Size;
- };
-
- union
- {
- Str maStr;
- double mfValue;
- };
-
- bool mbValue;
-
- Cell();
- Cell( const Cell& r );
-};
-
-struct Line
-{
- OString maLine;
- std::vector<Cell> maCells;
-};
-
-typedef std::vector<Line> LinesType;
-
class CSVFetchThread : public salhelper::Thread
{
ScDocument& mrDocument;
@@ -77,8 +53,6 @@ class CSVFetchThread : public salhelper::Thread
bool mbTerminate;
osl::Mutex maMtxTerminate;
- std::queue<LinesType*> maPendingLines;
-
orcus::csv::parser_config maConfig;
Idle* mpIdle;
@@ -108,6 +82,8 @@ public:
virtual void Import() = 0;
virtual const OUString& GetURL() const = 0;
+
+ static std::unique_ptr<SvStream> FetchStreamFromURL(const OUString&, OStringBuffer& rBuffer);
};
class CSVDataProvider : public DataProvider
@@ -116,13 +92,10 @@ class CSVDataProvider : public DataProvider
rtl::Reference<CSVFetchThread> mxCSVFetchThread;
ScDocument* mpDocument;
ScDBDataManager* mpDBDataManager;
- LinesType* mpLines;
- size_t mnLineCount;
std::unique_ptr<ScDocument> mpDoc;
Idle maIdle;
void Refresh();
- Line GetLine();
public:
CSVDataProvider (ScDocument* pDoc, const OUString& rURL, ScDBDataManager* pDBManager);
More information about the Libreoffice-commits
mailing list