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