1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/* -*- 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 <datatransformation.hxx>
#include <datamapper.hxx>
#include <stringutil.hxx>
#include <tools/stream.hxx>
#include <vcl/svapp.hxx>
#include <docsh.hxx>
#include <orcus/csv_parser.hpp>
#include <utility>
namespace {
class CSVHandler
{
ScDocument* mpDoc;
SCCOL mnCol;
SCROW mnRow;
public:
CSVHandler(ScDocument* pDoc) :
mpDoc(pDoc), mnCol(0), mnRow(0)
{
}
static void begin_parse() {}
static void end_parse() {}
static void begin_row() {}
void end_row()
{
++mnRow;
mnCol = 0;
}
void cell(const char* p, size_t n, bool /*transient*/)
{
if (mnCol > mpDoc->MaxCol())
return;
double mfValue = 0.0;
if (ScStringUtil::parseSimpleNumber(p, n, '.', ',', mfValue))
{
mpDoc->SetValue(mnCol, mnRow, 0, mfValue);
}
else
{
OString aStr(p, n);
mpDoc->SetString(mnCol, mnRow, 0, OStringToOUString(aStr, RTL_TEXTENCODING_UTF8));
}
++mnCol;
}
};
}
namespace sc {
CSVFetchThread::CSVFetchThread(
ScDocument& rDoc, const OUString& mrURL, std::function<void()> aImportFinishedHdl,
const std::vector<std::shared_ptr<sc::DataTransformation>>& rDataTransformations)
: Thread("CSV Fetch Thread")
, mrDocument(rDoc)
, maURL(mrURL)
, mbTerminate(false)
, maDataTransformations(rDataTransformations)
, maImportFinishedHdl(std::move(aImportFinishedHdl))
{
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);
DataProvider::FetchStreamFromURL(maURL, aBuffer);
if (mbTerminate)
return;
CSVHandler aHdl(&mrDocument);
orcus::csv_parser<CSVHandler> parser(aBuffer.getStr(), aBuffer.getLength(), aHdl, maConfig);
parser.parse();
for (const auto& itr : maDataTransformations)
{
itr->Transform(mrDocument);
}
SolarMutexGuard aGuard;
maImportFinishedHdl();
}
CSVDataProvider::CSVDataProvider(ScDocument* pDoc, sc::ExternalDataSource& rDataSource):
DataProvider(rDataSource),
mpDocument(pDoc)
{
}
CSVDataProvider::~CSVDataProvider()
{
if (mxCSVFetchThread.is())
{
SolarMutexReleaser aReleaser;
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, mrDataSource.getURL(), std::bind(&CSVDataProvider::ImportFinished, this), mrDataSource.getDataTransformation());
mxCSVFetchThread->launch();
if (mbDeterministic)
{
SolarMutexReleaser aReleaser;
mxCSVFetchThread->join();
}
}
void CSVDataProvider::ImportFinished()
{
mrDataSource.getDBManager()->WriteToDoc(*mpDoc);
mpDoc.reset();
Refresh();
}
void CSVDataProvider::Refresh()
{
ScDocShell* pDocShell = static_cast<ScDocShell*>(mpDocument->GetDocumentShell());
if (pDocShell)
pDocShell->SetDocumentModified();
}
const OUString& CSVDataProvider::GetURL() const
{
return mrDataSource.getURL();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|