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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/* -*- 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 .
*/
#include <rtl/math.hxx>
#include <dif.hxx>
#include <filter.hxx>
#include <document.hxx>
#include <formulacell.hxx>
#include <globstr.hrc>
#include <global.hxx>
#include <progress.hxx>
#include <rtl/tencinfo.h>
#include <ftools.hxx>
#include <cellvalue.hxx>
#include <rtl/strbuf.hxx>
#include <osl/diagnose.h>
#include <formula/errorcodes.hxx>
void ScFormatFilterPluginImpl::ScExportDif( SvStream& rStream, ScDocument* pDoc,
const ScAddress& rOutPos, const rtl_TextEncoding eNach )
{
SCCOL nEndCol;
SCROW nEndRow;
pDoc->GetTableArea( rOutPos.Tab(), nEndCol, nEndRow );
ScAddress aEnd( nEndCol, nEndRow, rOutPos.Tab() );
ScAddress aStart( rOutPos );
aStart.PutInOrder( aEnd );
ScExportDif( rStream, pDoc, ScRange( aStart, aEnd ), eNach );
}
void ScFormatFilterPluginImpl::ScExportDif( SvStream& rOut, ScDocument* pDoc,
const ScRange&rRange, const rtl_TextEncoding eCharSet )
{
OSL_ENSURE( rRange.aStart <= rRange.aEnd, "*ScExportDif(): Range not sorted!" );
OSL_ENSURE( rRange.aStart.Tab() == rRange.aEnd.Tab(),
"ScExportDif(): only one table please!" );
const rtl_TextEncoding eStreamCharSet = rOut.GetStreamCharSet();
if ( eStreamCharSet != eCharSet )
rOut.SetStreamCharSet( eCharSet );
sal_Unicode cStrDelim('"');
OString aStrDelimEncoded; // only used if not Unicode
OUString aStrDelimDecoded; // only used if context encoding
bool bContextOrNotAsciiEncoding;
if ( eCharSet == RTL_TEXTENCODING_UNICODE )
{
rOut.StartWritingUnicodeText();
bContextOrNotAsciiEncoding = false;
}
else
{
aStrDelimEncoded = OString(&cStrDelim, 1, eCharSet);
rtl_TextEncodingInfo aInfo;
aInfo.StructSize = sizeof(aInfo);
if ( rtl_getTextEncodingInfo( eCharSet, &aInfo ) )
{
bContextOrNotAsciiEncoding =
(((aInfo.Flags & RTL_TEXTENCODING_INFO_CONTEXT) != 0) ||
((aInfo.Flags & RTL_TEXTENCODING_INFO_ASCII) == 0));
if ( bContextOrNotAsciiEncoding )
aStrDelimDecoded = OStringToOUString(aStrDelimEncoded, eCharSet);
}
else
bContextOrNotAsciiEncoding = false;
}
const sal_Char p2DoubleQuotes_LF[] = "\"\"\n";
const sal_Char pSpecDataType_LF[] = "-1,0\n";
const sal_Char pEmptyData[] = "1,0\n\"\"\n";
const sal_Char pStringData[] = "1,0\n";
const sal_Char pNumData[] = "0,";
const sal_Char pNumDataERROR[] = "0,0\nERROR\n";
OUStringBuffer aOS;
OUString aString;
SCCOL nEndCol = rRange.aEnd.Col();
SCROW nEndRow = rRange.aEnd.Row();
SCCOL nNumCols = nEndCol - rRange.aStart.Col() + 1;
SCROW nNumRows = nEndRow - rRange.aStart.Row() + 1;
SCTAB nTab = rRange.aStart.Tab();
ScProgress aPrgrsBar( pDoc->GetDocumentShell(), ScGlobal::GetRscString( STR_LOAD_DOC ), nNumRows, true );
aPrgrsBar.SetState( 0 );
// TABLE
OSL_ENSURE( pDoc->HasTable( nTab ), "*ScExportDif(): Table not existent!" );
aOS.append(pKeyTABLE);
aOS.append("\n0,1\n\"");
pDoc->GetName( nTab, aString );
aOS.append(aString);
aOS.append("\"\n");
rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
// VECTORS
aOS.append(pKeyVECTORS);
aOS.append("\n0,");
aOS.append(static_cast<sal_Int32>(nNumCols));
aOS.append('\n');
aOS.append(p2DoubleQuotes_LF);
rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
// TUPLES
aOS.append(pKeyTUPLES);
aOS.append("\n0,");
aOS.append(static_cast<sal_Int32>(nNumRows));
aOS.append('\n');
aOS.append(p2DoubleQuotes_LF);
rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
// DATA
aOS.append(pKeyDATA);
aOS.append("\n0,0\n");
aOS.append(p2DoubleQuotes_LF);
rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
SCCOL nColCnt;
SCROW nRowCnt;
for( nRowCnt = rRange.aStart.Row() ; nRowCnt <= nEndRow ; nRowCnt++ )
{
assert( aOS.isEmpty() && "aOS should be empty");
aOS.append(pSpecDataType_LF);
aOS.append(pKeyBOT);
aOS.append('\n');
rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
for( nColCnt = rRange.aStart.Col() ; nColCnt <= nEndCol ; nColCnt++ )
{
assert( aOS.isEmpty() && "aOS should be empty");
bool bWriteStringData = false;
ScRefCellValue aCell(*pDoc, ScAddress(nColCnt, nRowCnt, nTab));
switch (aCell.meType)
{
case CELLTYPE_NONE:
aOS.append(pEmptyData);
break;
case CELLTYPE_VALUE:
aOS.append(pNumData);
pDoc->GetInputString( nColCnt, nRowCnt, nTab, aString );
aOS.append(aString);
aOS.append("\nV\n");
break;
case CELLTYPE_EDIT:
case CELLTYPE_STRING:
aString = aCell.getString(pDoc);
bWriteStringData = true;
break;
case CELLTYPE_FORMULA:
if (aCell.mpFormula->GetErrCode() != FormulaError::NONE)
aOS.append(pNumDataERROR);
else if (aCell.mpFormula->IsValue())
{
aOS.append(pNumData);
pDoc->GetInputString( nColCnt, nRowCnt, nTab, aString );
aOS.append(aString);
aOS.append("\nV\n");
}
else
{
aString = aCell.mpFormula->GetString().getString();
bWriteStringData = true;
}
break;
default:;
}
if ( !bWriteStringData )
rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
else
{
// for an explanation why this complicated, see
// sc/source/ui/docsh.cxx:ScDocShell::AsciiSave()
// In fact we should create a common method if this would be
// needed just one more time..
assert( aOS.isEmpty() && "aOS should be empty");
OUString aTmpStr = aString;
aOS.append(pStringData);
rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear(), eCharSet);
if ( eCharSet == RTL_TEXTENCODING_UNICODE )
{
sal_Int32 nPos = aTmpStr.indexOf( cStrDelim );
while ( nPos != -1 )
{
aTmpStr = aTmpStr.replaceAt( nPos, 0, OUString(cStrDelim) );
nPos = aTmpStr.indexOf( cStrDelim, nPos+2 );
}
rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
write_uInt16s_FromOUString(rOut, aTmpStr);
rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
}
else if ( bContextOrNotAsciiEncoding )
{
// to byte encoding
OString aStrEnc = OUStringToOString(aTmpStr, eCharSet);
// back to Unicode
OUString aStrDec = OStringToOUString(aStrEnc, eCharSet);
// search on re-decoded string
sal_Int32 nPos = aStrDec.indexOf(aStrDelimDecoded);
while (nPos >= 0)
{
OUStringBuffer aBuf(aStrDec);
aBuf.insert(nPos, aStrDelimDecoded);
aStrDec = aBuf.makeStringAndClear();
nPos = aStrDec.indexOf(
aStrDelimDecoded, nPos+1+aStrDelimDecoded.getLength());
}
// write byte re-encoded
rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
rOut.WriteUnicodeOrByteText( aStrDec, eCharSet );
rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
}
else
{
OString aStrEnc = OUStringToOString(aTmpStr, eCharSet);
// search on encoded string
sal_Int32 nPos = aStrEnc.indexOf(aStrDelimEncoded);
while (nPos >= 0)
{
OStringBuffer aBuf(aStrEnc);
aBuf.insert(nPos, aStrDelimEncoded);
aStrEnc = aBuf.makeStringAndClear();
nPos = aStrEnc.indexOf(
aStrDelimEncoded, nPos+1+aStrDelimEncoded.getLength());
}
// write byte encoded
rOut.WriteBytes(aStrDelimEncoded.getStr(), aStrDelimEncoded.getLength());
rOut.WriteBytes(aStrEnc.getStr(), aStrEnc.getLength());
rOut.WriteBytes(aStrDelimEncoded.getStr(), aStrDelimEncoded.getLength());
}
rOut.WriteUniOrByteChar( '\n', eCharSet );
}
}
aPrgrsBar.SetState( nRowCnt );
}
assert( aOS.isEmpty() && "aOS should be empty");
aOS.append(pSpecDataType_LF);
aOS.append(pKeyEOD);
aOS.append('\n');
rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
// restore original value
rOut.SetStreamCharSet( eStreamCharSet );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|