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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sc.hxx"
//------------------------------------------------------------------------
#include <rtl/math.hxx>
#include <stdio.h>
#include "dif.hxx"
#include "filter.hxx"
#include "document.hxx"
#include "cell.hxx"
#include "globstr.hrc"
#include "global.hxx"
#include "progress.hxx"
#include <rtl/tencinfo.h>
#include "ftools.hxx"
FltError ScFormatFilterPluginImpl::ScExportDif( SvStream& rStream, ScDocument* pDoc,
const ScAddress& rOutPos, const CharSet eNach, sal_uInt32 nDifOption )
{
SCCOL nEndCol;
SCROW nEndRow;
pDoc->GetTableArea( rOutPos.Tab(), nEndCol, nEndRow );
ScAddress aEnd( nEndCol, nEndRow, rOutPos.Tab() );
ScAddress aStart( rOutPos );
aStart.PutInOrder( aEnd );
return ScExportDif( rStream, pDoc, ScRange( aStart, aEnd ), eNach, nDifOption );
}
FltError ScFormatFilterPluginImpl::ScExportDif( SvStream& rOut, ScDocument* pDoc,
const ScRange&rRange, const CharSet eCharSet, sal_uInt32 nDifOption )
{
DBG_ASSERT( rRange.aStart <= rRange.aEnd, "*ScExportDif(): Range unsortiert!" );
DBG_ASSERTWARNING( rRange.aStart.Tab() == rRange.aEnd.Tab(),
"ScExportDif(): nur eine Tabelle bidde!" );
const CharSet eStreamCharSet = rOut.GetStreamCharSet();
if ( eStreamCharSet != eCharSet )
rOut.SetStreamCharSet( eCharSet );
sal_Unicode cStrDelim('"');
ByteString aStrDelimEncoded; // only used if not Unicode
UniString aStrDelimDecoded; // only used if context encoding
sal_Bool bContextOrNotAsciiEncoding;
if ( eCharSet == RTL_TEXTENCODING_UNICODE )
{
rOut.StartWritingUnicodeText();
bContextOrNotAsciiEncoding = sal_False;
}
else
{
aStrDelimEncoded = ByteString( cStrDelim, 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 = String( aStrDelimEncoded, eCharSet );
}
else
bContextOrNotAsciiEncoding = sal_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";
FltError eRet = eERR_OK;
String aOS;
String 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();
double fVal;
const sal_Bool bPlain = ( nDifOption == SC_DIFOPT_PLAIN );
ScProgress aPrgrsBar( pDoc->GetDocumentShell(), ScGlobal::GetRscString( STR_LOAD_DOC ), nNumRows );
aPrgrsBar.SetState( 0 );
// TABLE
DBG_ASSERT( pDoc->HasTable( nTab ), "*ScExportDif(): Tabelle nicht vorhanden!" );
aOS = pKeyTABLE;
aOS.AppendAscii( "\n0,1\n\"" );
pDoc->GetName( nTab, aString );
aOS += aString;
aOS.AppendAscii( "\"\n" );
rOut.WriteUnicodeOrByteText( aOS );
// VECTORS
aOS = pKeyVECTORS;
aOS.AppendAscii( "\n0," );
aOS += String::CreateFromInt32( nNumCols );
aOS += sal_Unicode('\n');
aOS.AppendAscii( p2DoubleQuotes_LF );
rOut.WriteUnicodeOrByteText( aOS );
// TUPLES
aOS = pKeyTUPLES;
aOS.AppendAscii( "\n0," );
aOS += String::CreateFromInt32( nNumRows );
aOS += sal_Unicode('\n');
aOS.AppendAscii( p2DoubleQuotes_LF );
rOut.WriteUnicodeOrByteText( aOS );
// DATA
aOS = pKeyDATA;
aOS.AppendAscii( "\n0,0\n" );
aOS.AppendAscii( p2DoubleQuotes_LF );
rOut.WriteUnicodeOrByteText( aOS );
SCCOL nColCnt;
SCROW nRowCnt;
ScBaseCell* pAkt;
for( nRowCnt = rRange.aStart.Row() ; nRowCnt <= nEndRow ; nRowCnt++ )
{
aOS.AssignAscii( pSpecDataType_LF );
aOS += pKeyBOT;
aOS += sal_Unicode('\n');
rOut.WriteUnicodeOrByteText( aOS );
for( nColCnt = rRange.aStart.Col() ; nColCnt <= nEndCol ; nColCnt++ )
{
bool bWriteStringData = false;
pDoc->GetCell( nColCnt, nRowCnt, nTab, pAkt );
if( pAkt )
{
switch( pAkt->GetCellType() )
{
case CELLTYPE_NONE:
case CELLTYPE_NOTE:
aOS.AssignAscii( pEmptyData );
break;
case CELLTYPE_VALUE:
aOS.AssignAscii( pNumData );
if( bPlain )
{
fVal = ( ( ScValueCell * ) pAkt )->GetValue();
aOS += String( ::rtl::math::doubleToUString(
fVal, rtl_math_StringFormat_G, 14, '.',
sal_True));
}
else
{
pDoc->GetInputString( nColCnt, nRowCnt, nTab, aString );
aOS += aString;
}
aOS.AppendAscii( "\nV\n" );
break;
case CELLTYPE_EDIT:
( ( ScEditCell* ) pAkt )->GetString( aString );
bWriteStringData = true;
break;
case CELLTYPE_STRING:
( ( ScStringCell* ) pAkt )->GetString( aString );
bWriteStringData = true;
break;
case CELLTYPE_FORMULA:
if ( ((ScFormulaCell*)pAkt)->GetErrCode() )
aOS.AssignAscii( pNumDataERROR );
else if( pAkt->HasValueData() )
{
aOS.AssignAscii( pNumData );
if( bPlain )
{
fVal = ( ( ScFormulaCell * ) pAkt )->GetValue();
aOS += String( ::rtl::math::doubleToUString(
fVal, rtl_math_StringFormat_G, 14,
'.', sal_True));
}
else
{
pDoc->GetInputString( nColCnt, nRowCnt, nTab, aString );
aOS += aString;
}
aOS.AppendAscii( "\nV\n" );
}
else if( pAkt->HasStringData() )
{
( ( ScFormulaCell * ) pAkt )->GetString( aString );
bWriteStringData = true;
}
else
aOS.AssignAscii( pNumDataERROR );
break;
default:;
}
}
else
aOS.AssignAscii( pEmptyData );
if ( !bWriteStringData )
rOut.WriteUnicodeOrByteText( aOS );
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..
aOS.AssignAscii( pStringData );
rOut.WriteUnicodeOrByteText( aOS, eCharSet );
if ( eCharSet == RTL_TEXTENCODING_UNICODE )
{
xub_StrLen nPos = aString.Search( cStrDelim );
while ( nPos != STRING_NOTFOUND )
{
aString.Insert( cStrDelim, nPos );
nPos = aString.Search( cStrDelim, nPos+2 );
}
rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
rOut.WriteUnicodeText( aString );
rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
}
else if ( bContextOrNotAsciiEncoding )
{
// to byte encoding
ByteString aStrEnc( aString, eCharSet );
// back to Unicode
UniString aStrDec( aStrEnc, eCharSet );
// search on re-decoded string
xub_StrLen nPos = aStrDec.Search( aStrDelimDecoded );
while ( nPos != STRING_NOTFOUND )
{
aStrDec.Insert( aStrDelimDecoded, nPos );
nPos = aStrDec.Search( aStrDelimDecoded,
nPos+1+aStrDelimDecoded.Len() );
}
// write byte re-encoded
rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
rOut.WriteUnicodeOrByteText( aStrDec, eCharSet );
rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
}
else
{
ByteString aStrEnc( aString, eCharSet );
// search on encoded string
xub_StrLen nPos = aStrEnc.Search( aStrDelimEncoded );
while ( nPos != STRING_NOTFOUND )
{
aStrEnc.Insert( aStrDelimEncoded, nPos );
nPos = aStrEnc.Search( aStrDelimEncoded,
nPos+1+aStrDelimEncoded.Len() );
}
// write byte encoded
rOut.Write( aStrDelimEncoded.GetBuffer(),
aStrDelimEncoded.Len() );
rOut.Write( aStrEnc.GetBuffer(), aStrEnc.Len() );
rOut.Write( aStrDelimEncoded.GetBuffer(),
aStrDelimEncoded.Len() );
}
rOut.WriteUniOrByteChar( '\n', eCharSet );
}
}
aPrgrsBar.SetState( nRowCnt );
}
aOS.AssignAscii( pSpecDataType_LF );
aOS += pKeyEOD;
aOS += sal_Unicode('\n');
rOut.WriteUnicodeOrByteText( aOS );
// restore original value
rOut.SetStreamCharSet( eStreamCharSet );
return eRet;
}
|