Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <rtl/math.hxx>
21 :
22 : #include "dif.hxx"
23 : #include "filter.hxx"
24 : #include "document.hxx"
25 : #include "formulacell.hxx"
26 : #include "globstr.hrc"
27 : #include "global.hxx"
28 : #include "progress.hxx"
29 : #include <rtl/tencinfo.h>
30 : #include "ftools.hxx"
31 : #include "cellvalue.hxx"
32 : #include <rtl/strbuf.hxx>
33 : #include <osl/diagnose.h>
34 :
35 0 : FltError ScFormatFilterPluginImpl::ScExportDif( SvStream& rStream, ScDocument* pDoc,
36 : const ScAddress& rOutPos, const rtl_TextEncoding eNach, sal_uInt32 nDifOption )
37 : {
38 : SCCOL nEndCol;
39 : SCROW nEndRow;
40 0 : pDoc->GetTableArea( rOutPos.Tab(), nEndCol, nEndRow );
41 0 : ScAddress aEnd( nEndCol, nEndRow, rOutPos.Tab() );
42 0 : ScAddress aStart( rOutPos );
43 :
44 0 : aStart.PutInOrder( aEnd );
45 :
46 0 : return ScExportDif( rStream, pDoc, ScRange( aStart, aEnd ), eNach, nDifOption );
47 : }
48 :
49 0 : FltError ScFormatFilterPluginImpl::ScExportDif( SvStream& rOut, ScDocument* pDoc,
50 : const ScRange&rRange, const rtl_TextEncoding eCharSet, sal_uInt32 nDifOption )
51 : {
52 : OSL_ENSURE( rRange.aStart <= rRange.aEnd, "*ScExportDif(): Range not sorted!" );
53 : OSL_ENSURE( rRange.aStart.Tab() == rRange.aEnd.Tab(),
54 : "ScExportDif(): only one table please!" );
55 :
56 0 : const rtl_TextEncoding eStreamCharSet = rOut.GetStreamCharSet();
57 0 : if ( eStreamCharSet != eCharSet )
58 0 : rOut.SetStreamCharSet( eCharSet );
59 :
60 0 : sal_Unicode cStrDelim('"');
61 0 : OString aStrDelimEncoded; // only used if not Unicode
62 0 : OUString aStrDelimDecoded; // only used if context encoding
63 : bool bContextOrNotAsciiEncoding;
64 0 : if ( eCharSet == RTL_TEXTENCODING_UNICODE )
65 : {
66 0 : rOut.StartWritingUnicodeText();
67 0 : bContextOrNotAsciiEncoding = false;
68 : }
69 : else
70 : {
71 0 : aStrDelimEncoded = OString(&cStrDelim, 1, eCharSet);
72 : rtl_TextEncodingInfo aInfo;
73 0 : aInfo.StructSize = sizeof(aInfo);
74 0 : if ( rtl_getTextEncodingInfo( eCharSet, &aInfo ) )
75 : {
76 : bContextOrNotAsciiEncoding =
77 0 : (((aInfo.Flags & RTL_TEXTENCODING_INFO_CONTEXT) != 0) ||
78 0 : ((aInfo.Flags & RTL_TEXTENCODING_INFO_ASCII) == 0));
79 0 : if ( bContextOrNotAsciiEncoding )
80 0 : aStrDelimDecoded = OStringToOUString(aStrDelimEncoded, eCharSet);
81 : }
82 : else
83 0 : bContextOrNotAsciiEncoding = false;
84 : }
85 :
86 0 : const sal_Char* p2DoubleQuotes_LF = "\"\"\n";
87 0 : const sal_Char* pSpecDataType_LF = "-1,0\n";
88 0 : const sal_Char* pEmptyData = "1,0\n\"\"\n";
89 0 : const sal_Char* pStringData = "1,0\n";
90 0 : const sal_Char* pNumData = "0,";
91 0 : const sal_Char* pNumDataERROR = "0,0\nERROR\n";
92 :
93 0 : FltError eRet = eERR_OK;
94 0 : OUStringBuffer aOS;
95 0 : OUString aString;
96 0 : SCCOL nEndCol = rRange.aEnd.Col();
97 0 : SCROW nEndRow = rRange.aEnd.Row();
98 0 : SCCOL nNumCols = nEndCol - rRange.aStart.Col() + 1;
99 0 : SCROW nNumRows = nEndRow - rRange.aStart.Row() + 1;
100 0 : SCTAB nTab = rRange.aStart.Tab();
101 :
102 : double fVal;
103 :
104 0 : const bool bPlain = ( nDifOption == SC_DIFOPT_PLAIN );
105 :
106 0 : ScProgress aPrgrsBar( pDoc->GetDocumentShell(), ScGlobal::GetRscString( STR_LOAD_DOC ), nNumRows );
107 :
108 0 : aPrgrsBar.SetState( 0 );
109 :
110 : // TABLE
111 : OSL_ENSURE( pDoc->HasTable( nTab ), "*ScExportDif(): Table not existent!" );
112 :
113 0 : aOS.append(pKeyTABLE);
114 0 : aOS.appendAscii("\n0,1\n\"");
115 :
116 0 : pDoc->GetName( nTab, aString );
117 0 : aOS.append(aString);
118 0 : aOS.appendAscii("\"\n");
119 0 : rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
120 :
121 : // VECTORS
122 0 : aOS.append(pKeyVECTORS);
123 0 : aOS.appendAscii("\n0,");
124 0 : aOS.append(static_cast<sal_Int32>(nNumCols));
125 0 : aOS.append('\n');
126 0 : aOS.appendAscii(p2DoubleQuotes_LF);
127 0 : rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
128 :
129 : // TUPLES
130 0 : aOS.append(pKeyTUPLES);
131 0 : aOS.appendAscii("\n0,");
132 0 : aOS.append(static_cast<sal_Int32>(nNumRows));
133 0 : aOS.append('\n');
134 0 : aOS.appendAscii(p2DoubleQuotes_LF);
135 0 : rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
136 :
137 : // DATA
138 0 : aOS.append(pKeyDATA);
139 0 : aOS.appendAscii("\n0,0\n");
140 0 : aOS.appendAscii(p2DoubleQuotes_LF);
141 0 : rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
142 :
143 : SCCOL nColCnt;
144 : SCROW nRowCnt;
145 :
146 0 : ScRefCellValue aCell;
147 :
148 0 : for( nRowCnt = rRange.aStart.Row() ; nRowCnt <= nEndRow ; nRowCnt++ )
149 : {
150 : OSL_ASSERT(aOS.getLength() == 0);
151 0 : aOS.appendAscii(pSpecDataType_LF);
152 0 : aOS.append(pKeyBOT);
153 0 : aOS.append('\n');
154 0 : rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
155 0 : for( nColCnt = rRange.aStart.Col() ; nColCnt <= nEndCol ; nColCnt++ )
156 : {
157 : OSL_ASSERT(aOS.getLength() == 0);
158 0 : bool bWriteStringData = false;
159 0 : aCell.assign(*pDoc, ScAddress(nColCnt, nRowCnt, nTab));
160 :
161 0 : switch (aCell.meType)
162 : {
163 : case CELLTYPE_NONE:
164 0 : aOS.appendAscii(pEmptyData);
165 0 : break;
166 : case CELLTYPE_VALUE:
167 0 : aOS.appendAscii(pNumData);
168 0 : if( bPlain )
169 : {
170 : aOS.append(
171 : rtl::math::doubleToUString(
172 0 : aCell.mfValue, rtl_math_StringFormat_G, 14, '.', true));
173 : }
174 : else
175 : {
176 0 : pDoc->GetInputString( nColCnt, nRowCnt, nTab, aString );
177 0 : aOS.append(aString);
178 : }
179 0 : aOS.appendAscii("\nV\n");
180 0 : break;
181 : case CELLTYPE_EDIT:
182 : case CELLTYPE_STRING:
183 0 : aString = aCell.getString(pDoc);
184 0 : bWriteStringData = true;
185 0 : break;
186 : case CELLTYPE_FORMULA:
187 0 : if (aCell.mpFormula->GetErrCode())
188 0 : aOS.appendAscii(pNumDataERROR);
189 0 : else if (aCell.mpFormula->IsValue())
190 : {
191 0 : aOS.appendAscii(pNumData);
192 0 : if( bPlain )
193 : {
194 0 : fVal = aCell.mpFormula->GetValue();
195 : aOS.append(
196 : rtl::math::doubleToUString(
197 0 : fVal, rtl_math_StringFormat_G, 14, '.', true));
198 : }
199 : else
200 : {
201 0 : pDoc->GetInputString( nColCnt, nRowCnt, nTab, aString );
202 0 : aOS.append(aString);
203 : }
204 0 : aOS.appendAscii("\nV\n");
205 : }
206 : else
207 : {
208 0 : aString = aCell.mpFormula->GetString().getString();
209 0 : bWriteStringData = true;
210 : }
211 :
212 0 : break;
213 : default:;
214 : }
215 :
216 0 : if ( !bWriteStringData )
217 0 : rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
218 : else
219 : {
220 : // for an explanation why this complicated, see
221 : // sc/source/ui/docsh.cxx:ScDocShell::AsciiSave()
222 : // In fact we should create a common method if this would be
223 : // needed just one more time..
224 : OSL_ASSERT(aOS.getLength() == 0);
225 0 : OUString aTmpStr = aString;
226 0 : aOS.appendAscii(pStringData);
227 0 : rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear(), eCharSet);
228 0 : if ( eCharSet == RTL_TEXTENCODING_UNICODE )
229 : {
230 0 : sal_Int32 nPos = aTmpStr.indexOf( cStrDelim );
231 0 : while ( nPos != -1 )
232 : {
233 0 : aTmpStr = aTmpStr.replaceAt( nPos, 0, OUString(cStrDelim) );
234 0 : nPos = aTmpStr.indexOf( cStrDelim, nPos+2 );
235 : }
236 0 : rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
237 0 : write_uInt16s_FromOUString(rOut, aTmpStr);
238 0 : rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
239 : }
240 0 : else if ( bContextOrNotAsciiEncoding )
241 : {
242 : // to byte encoding
243 0 : OString aStrEnc = OUStringToOString(aTmpStr, eCharSet);
244 : // back to Unicode
245 0 : OUString aStrDec = OStringToOUString(aStrEnc, eCharSet);
246 : // search on re-decoded string
247 0 : sal_Int32 nPos = aStrDec.indexOf(aStrDelimDecoded);
248 0 : while (nPos >= 0)
249 : {
250 0 : OUStringBuffer aBuf(aStrDec);
251 0 : aBuf.insert(nPos, aStrDelimDecoded);
252 0 : aStrDec = aBuf.makeStringAndClear();
253 : nPos = aStrDec.indexOf(
254 0 : aStrDelimDecoded, nPos+1+aStrDelimDecoded.getLength());
255 0 : }
256 : // write byte re-encoded
257 0 : rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
258 0 : rOut.WriteUnicodeOrByteText( aStrDec, eCharSet );
259 0 : rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
260 : }
261 : else
262 : {
263 0 : OString aStrEnc = OUStringToOString(aTmpStr, eCharSet);
264 : // search on encoded string
265 0 : sal_Int32 nPos = aStrEnc.indexOf(aStrDelimEncoded);
266 0 : while (nPos >= 0)
267 : {
268 0 : OStringBuffer aBuf(aStrEnc);
269 0 : aBuf.insert(nPos, aStrDelimEncoded);
270 0 : aStrEnc = aBuf.makeStringAndClear();
271 : nPos = aStrEnc.indexOf(
272 0 : aStrDelimEncoded, nPos+1+aStrDelimEncoded.getLength());
273 0 : }
274 : // write byte encoded
275 0 : rOut.Write(aStrDelimEncoded.getStr(), aStrDelimEncoded.getLength());
276 0 : rOut.Write(aStrEnc.getStr(), aStrEnc.getLength());
277 0 : rOut.Write(aStrDelimEncoded.getStr(), aStrDelimEncoded.getLength());
278 : }
279 0 : rOut.WriteUniOrByteChar( '\n', eCharSet );
280 : }
281 : }
282 0 : aPrgrsBar.SetState( nRowCnt );
283 : }
284 :
285 : OSL_ASSERT(aOS.getLength() == 0);
286 0 : aOS.appendAscii(pSpecDataType_LF);
287 0 : aOS.append(pKeyEOD);
288 0 : aOS.append('\n');
289 0 : rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
290 :
291 : // restore original value
292 0 : rOut.SetStreamCharSet( eStreamCharSet );
293 :
294 0 : return eRet;
295 30 : }
296 :
297 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|