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 "cell.hxx"
28 : #include "globstr.hrc"
29 : #include "global.hxx"
30 : #include "progress.hxx"
31 : #include <rtl/tencinfo.h>
32 : #include "ftools.hxx"
33 : #include "rtl/strbuf.hxx"
34 :
35 0 : FltError ScFormatFilterPluginImpl::ScExportDif( SvStream& rStream, ScDocument* pDoc,
36 : const ScAddress& rOutPos, const CharSet 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 :
50 0 : FltError ScFormatFilterPluginImpl::ScExportDif( SvStream& rOut, ScDocument* pDoc,
51 : const ScRange&rRange, const CharSet eCharSet, sal_uInt32 nDifOption )
52 : {
53 : OSL_ENSURE( rRange.aStart <= rRange.aEnd, "*ScExportDif(): Range unsortiert!" );
54 : OSL_ENSURE( rRange.aStart.Tab() == rRange.aEnd.Tab(),
55 : "ScExportDif(): nur eine Tabelle bidde!" );
56 :
57 0 : const CharSet eStreamCharSet = rOut.GetStreamCharSet();
58 0 : if ( eStreamCharSet != eCharSet )
59 0 : rOut.SetStreamCharSet( eCharSet );
60 :
61 0 : sal_Unicode cStrDelim('"');
62 0 : rtl::OString aStrDelimEncoded; // only used if not Unicode
63 0 : rtl::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 = rtl::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 : (((aInfo.Flags & RTL_TEXTENCODING_INFO_CONTEXT) != 0) ||
79 0 : ((aInfo.Flags & RTL_TEXTENCODING_INFO_ASCII) == 0));
80 0 : if ( bContextOrNotAsciiEncoding )
81 0 : aStrDelimDecoded = rtl::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 : rtl::OUStringBuffer aOS;
96 0 : rtl::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(): Tabelle nicht vorhanden!" );
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(sal_Unicode('\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(sal_Unicode('\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 : ScBaseCell* pAkt;
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(sal_Unicode('\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 : pDoc->GetCell( nColCnt, nRowCnt, nTab, pAkt );
160 0 : if( pAkt )
161 : {
162 0 : switch( pAkt->GetCellType() )
163 : {
164 : case CELLTYPE_NONE:
165 : case CELLTYPE_NOTE:
166 0 : aOS.appendAscii(pEmptyData);
167 0 : break;
168 : case CELLTYPE_VALUE:
169 0 : aOS.appendAscii(pNumData);
170 0 : if( bPlain )
171 : {
172 0 : fVal = static_cast<ScValueCell*>(pAkt)->GetValue();
173 : aOS.append(
174 : rtl::math::doubleToUString(
175 0 : fVal, rtl_math_StringFormat_G, 14, '.', true));
176 : }
177 : else
178 : {
179 0 : pDoc->GetInputString( nColCnt, nRowCnt, nTab, aString );
180 0 : aOS.append(aString);
181 : }
182 0 : aOS.appendAscii("\nV\n");
183 0 : break;
184 : case CELLTYPE_EDIT:
185 0 : aString = static_cast<ScEditCell*>(pAkt)->GetString();
186 0 : bWriteStringData = true;
187 0 : break;
188 : case CELLTYPE_STRING:
189 0 : aString = static_cast<ScStringCell*>(pAkt)->GetString();
190 0 : bWriteStringData = true;
191 0 : break;
192 : case CELLTYPE_FORMULA:
193 0 : if (static_cast<ScFormulaCell*>(pAkt)->GetErrCode())
194 0 : aOS.appendAscii(pNumDataERROR);
195 0 : else if( pAkt->HasValueData() )
196 : {
197 0 : aOS.appendAscii(pNumData);
198 0 : if( bPlain )
199 : {
200 0 : fVal = static_cast<ScFormulaCell*>(pAkt)->GetValue();
201 : aOS.append(
202 : rtl::math::doubleToUString(
203 0 : fVal, rtl_math_StringFormat_G, 14, '.', true));
204 : }
205 : else
206 : {
207 0 : pDoc->GetInputString( nColCnt, nRowCnt, nTab, aString );
208 0 : aOS.append(aString);
209 : }
210 0 : aOS.appendAscii("\nV\n");
211 : }
212 0 : else if( pAkt->HasStringData() )
213 : {
214 0 : aString = static_cast<ScFormulaCell*>(pAkt)->GetString();
215 0 : bWriteStringData = true;
216 : }
217 : else
218 0 : aOS.appendAscii(pNumDataERROR);
219 :
220 0 : break;
221 : default:;
222 : }
223 : }
224 : else
225 0 : aOS.appendAscii(pEmptyData);
226 :
227 0 : if ( !bWriteStringData )
228 0 : rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
229 : else
230 : {
231 : // for an explanation why this complicated, see
232 : // sc/source/ui/docsh.cxx:ScDocShell::AsciiSave()
233 : // In fact we should create a common method if this would be
234 : // needed just one more time..
235 : OSL_ASSERT(aOS.getLength() == 0);
236 0 : String aTmpStr = aString;
237 0 : aOS.appendAscii(pStringData);
238 0 : rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear(), eCharSet);
239 0 : if ( eCharSet == RTL_TEXTENCODING_UNICODE )
240 : {
241 0 : xub_StrLen nPos = aTmpStr.Search( cStrDelim );
242 0 : while ( nPos != STRING_NOTFOUND )
243 : {
244 0 : aTmpStr.Insert( cStrDelim, nPos );
245 0 : nPos = aTmpStr.Search( cStrDelim, nPos+2 );
246 : }
247 0 : rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
248 0 : write_uInt16s_FromOUString(rOut, aTmpStr);
249 0 : rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
250 : }
251 0 : else if ( bContextOrNotAsciiEncoding )
252 : {
253 : // to byte encoding
254 0 : rtl::OString aStrEnc = rtl::OUStringToOString(aTmpStr, eCharSet);
255 : // back to Unicode
256 0 : rtl::OUString aStrDec = rtl::OStringToOUString(aStrEnc, eCharSet);
257 : // search on re-decoded string
258 0 : sal_Int32 nPos = aStrDec.indexOf(aStrDelimDecoded);
259 0 : while (nPos >= 0)
260 : {
261 0 : rtl::OUStringBuffer aBuf(aStrDec);
262 0 : aBuf.insert(nPos, aStrDelimDecoded);
263 0 : aStrDec = aBuf.makeStringAndClear();
264 : nPos = aStrDec.indexOf(
265 0 : aStrDelimDecoded, nPos+1+aStrDelimDecoded.getLength());
266 0 : }
267 : // write byte re-encoded
268 0 : rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
269 0 : rOut.WriteUnicodeOrByteText( aStrDec, eCharSet );
270 0 : rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
271 : }
272 : else
273 : {
274 0 : rtl::OString aStrEnc = rtl::OUStringToOString(aTmpStr, eCharSet);
275 : // search on encoded string
276 0 : sal_Int32 nPos = aStrEnc.indexOf(aStrDelimEncoded);
277 0 : while (nPos >= 0)
278 : {
279 0 : rtl::OStringBuffer aBuf(aStrEnc);
280 0 : aBuf.insert(nPos, aStrDelimEncoded);
281 0 : aStrEnc = aBuf.makeStringAndClear();
282 : nPos = aStrEnc.indexOf(
283 0 : aStrDelimEncoded, nPos+1+aStrDelimEncoded.getLength());
284 0 : }
285 : // write byte encoded
286 0 : rOut.Write(aStrDelimEncoded.getStr(), aStrDelimEncoded.getLength());
287 0 : rOut.Write(aStrEnc.getStr(), aStrEnc.getLength());
288 0 : rOut.Write(aStrDelimEncoded.getStr(), aStrDelimEncoded.getLength());
289 : }
290 0 : rOut.WriteUniOrByteChar( '\n', eCharSet );
291 : }
292 : }
293 0 : aPrgrsBar.SetState( nRowCnt );
294 : }
295 :
296 : OSL_ASSERT(aOS.getLength() == 0);
297 0 : aOS.appendAscii(pSpecDataType_LF);
298 0 : aOS.append(pKeyEOD);
299 0 : aOS.append(sal_Unicode('\n'));
300 0 : rOut.WriteUnicodeOrByteText(aOS.makeStringAndClear());
301 :
302 : // restore original value
303 0 : rOut.SetStreamCharSet( eStreamCharSet );
304 :
305 0 : return eRet;
306 : }
307 :
308 :
309 :
310 :
311 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|