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