LibreOffice Module sc (master) 1
difexp.cxx
Go to the documentation of this file.
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 <dif.hxx>
21#include <document.hxx>
22#include <formulacell.hxx>
23#include <globstr.hrc>
24#include <scresid.hxx>
25#include <global.hxx>
26#include <progress.hxx>
27#include <rtl/tencinfo.h>
28#include <ftools.hxx>
29#include <cellvalue.hxx>
30#include <rtl/strbuf.hxx>
31#include <osl/diagnose.h>
33#include <tools/stream.hxx>
34
36 const ScAddress& rOutPos, const rtl_TextEncoding eNach )
37{
38 SCCOL nEndCol;
39 SCROW nEndRow;
40 pDoc->GetTableArea( rOutPos.Tab(), nEndCol, nEndRow );
41 ScAddress aEnd( nEndCol, nEndRow, rOutPos.Tab() );
42 ScAddress aStart( rOutPos );
43
44 aStart.PutInOrder( aEnd );
45
46 ScExportDif( rStream, pDoc, ScRange( aStart, aEnd ), eNach );
47}
48
50 const ScRange&rRange, const rtl_TextEncoding eCharSet )
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 const rtl_TextEncoding eStreamCharSet = rOut.GetStreamCharSet();
57 if ( eStreamCharSet != eCharSet )
58 rOut.SetStreamCharSet( eCharSet );
59
60 sal_Unicode cStrDelim('"');
61 OString aStrDelimEncoded; // only used if not Unicode
62 OUString aStrDelimDecoded; // only used if context encoding
63 bool bContextOrNotAsciiEncoding;
64 if ( eCharSet == RTL_TEXTENCODING_UNICODE )
65 {
67 bContextOrNotAsciiEncoding = false;
68 }
69 else
70 {
71 aStrDelimEncoded = OString(&cStrDelim, 1, eCharSet);
72 rtl_TextEncodingInfo aInfo;
73 aInfo.StructSize = sizeof(aInfo);
74 if ( rtl_getTextEncodingInfo( eCharSet, &aInfo ) )
75 {
76 bContextOrNotAsciiEncoding =
77 (((aInfo.Flags & RTL_TEXTENCODING_INFO_CONTEXT) != 0) ||
78 ((aInfo.Flags & RTL_TEXTENCODING_INFO_ASCII) == 0));
79 if ( bContextOrNotAsciiEncoding )
80 aStrDelimDecoded = OStringToOUString(aStrDelimEncoded, eCharSet);
81 }
82 else
83 bContextOrNotAsciiEncoding = false;
84 }
85
86 const char p2DoubleQuotes_LF[] = "\"\"\n";
87 const char pSpecDataType_LF[] = "-1,0\n";
88 const char pEmptyData[] = "1,0\n\"\"\n";
89 const char pStringData[] = "1,0\n";
90 const char pNumData[] = "0,";
91 const char pNumDataERROR[] = "0,0\nERROR\n";
92
93 OUStringBuffer aOS;
94 OUString aString;
95 SCCOL nEndCol = rRange.aEnd.Col();
96 SCROW nEndRow = rRange.aEnd.Row();
97 SCCOL nNumCols = nEndCol - rRange.aStart.Col() + 1;
98 SCROW nNumRows = nEndRow - rRange.aStart.Row() + 1;
99 SCTAB nTab = rRange.aStart.Tab();
100
101 ScProgress aPrgrsBar( pDoc->GetDocumentShell(), ScResId( STR_LOAD_DOC ), nNumRows, true );
102
103 aPrgrsBar.SetState( 0 );
104
105 // TABLE
106 OSL_ENSURE( pDoc->HasTable( nTab ), "*ScExportDif(): Table not existent!" );
107
108 aOS.append(pKeyTABLE);
109 aOS.append("\n0,1\n\"");
110
111 pDoc->GetName( nTab, aString );
112 aOS.append(aString);
113 aOS.append("\"\n");
114 rOut.WriteUnicodeOrByteText(aOS);
115 aOS.setLength(0);
116
117 // VECTORS
118 aOS.append(pKeyVECTORS);
119 aOS.append("\n0,");
120 aOS.append(static_cast<sal_Int32>(nNumCols));
121 aOS.append('\n');
122 aOS.append(p2DoubleQuotes_LF);
123 rOut.WriteUnicodeOrByteText(aOS);
124 aOS.setLength(0);
125
126 // TUPLES
127 aOS.append(pKeyTUPLES);
128 aOS.append("\n0,");
129 aOS.append(static_cast<sal_Int32>(nNumRows));
130 aOS.append('\n');
131 aOS.append(p2DoubleQuotes_LF);
132 rOut.WriteUnicodeOrByteText(aOS);
133 aOS.setLength(0);
134
135 // DATA
136 aOS.append(pKeyDATA);
137 aOS.append("\n0,0\n");
138 aOS.append(p2DoubleQuotes_LF);
139 rOut.WriteUnicodeOrByteText(aOS);
140 aOS.setLength(0);
141
142 SCCOL nColCnt;
143 SCROW nRowCnt;
144
145 for( nRowCnt = rRange.aStart.Row() ; nRowCnt <= nEndRow ; nRowCnt++ )
146 {
147 assert( aOS.isEmpty() && "aOS should be empty");
148 aOS.append(pSpecDataType_LF);
149 aOS.append(pKeyBOT);
150 aOS.append('\n');
151 rOut.WriteUnicodeOrByteText(aOS);
152 aOS.setLength(0);
153 for( nColCnt = rRange.aStart.Col() ; nColCnt <= nEndCol ; nColCnt++ )
154 {
155 assert( aOS.isEmpty() && "aOS should be empty");
156 bool bWriteStringData = false;
157 ScRefCellValue aCell(*pDoc, ScAddress(nColCnt, nRowCnt, nTab));
158
159 switch (aCell.getType())
160 {
161 case CELLTYPE_NONE:
162 aOS.append(pEmptyData);
163 break;
164 case CELLTYPE_VALUE:
165 aOS.append(pNumData);
166 aString = pDoc->GetInputString( nColCnt, nRowCnt, nTab );
167 aOS.append(aString);
168 aOS.append("\nV\n");
169 break;
170 case CELLTYPE_EDIT:
171 case CELLTYPE_STRING:
172 aString = aCell.getString(pDoc);
173 bWriteStringData = true;
174 break;
175 case CELLTYPE_FORMULA:
176 if (aCell.getFormula()->GetErrCode() != FormulaError::NONE)
177 aOS.append(pNumDataERROR);
178 else if (aCell.getFormula()->IsValue())
179 {
180 aOS.append(pNumData);
181 aString = pDoc->GetInputString( nColCnt, nRowCnt, nTab );
182 aOS.append(aString);
183 aOS.append("\nV\n");
184 }
185 else
186 {
187 aString = aCell.getFormula()->GetString().getString();
188 bWriteStringData = true;
189 }
190
191 break;
192 default:;
193 }
194
195 if ( !bWriteStringData )
196 {
197 rOut.WriteUnicodeOrByteText(aOS);
198 aOS.setLength(0);
199 }
200 else
201 {
202 // for an explanation why this complicated, see
203 // sc/source/ui/docshell/docsh.cxx:ScDocShell::AsciiSave()
204 // In fact we should create a common method if this would be
205 // needed just one more time...
206 assert( aOS.isEmpty() && "aOS should be empty");
207 OUString aTmpStr = aString;
208 aOS.append(pStringData);
209 rOut.WriteUnicodeOrByteText(aOS, eCharSet);
210 aOS.setLength(0);
211 if ( eCharSet == RTL_TEXTENCODING_UNICODE )
212 {
213 // the goal is to replace cStrDelim by cStrDelim+cStrDelim
214 OUString strFrom(cStrDelim);
215 OUString strTo = strFrom + strFrom;
216 aTmpStr = aTmpStr.replaceAll(strFrom, strTo);
217 rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
218 write_uInt16s_FromOUString(rOut, aTmpStr);
219 rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
220 }
221 else if ( bContextOrNotAsciiEncoding )
222 {
223 // to byte encoding
224 OString aStrEnc = OUStringToOString(aTmpStr, eCharSet);
225 // back to Unicode
226 OUString aStrDec = OStringToOUString(aStrEnc, eCharSet);
227 // search on re-decoded string
228 OUString aStrTo = aStrDelimDecoded + aStrDelimDecoded;
229 aStrDec = aStrDec.replaceAll(aStrDelimDecoded, aStrTo);
230 // write byte re-encoded
231 rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
232 rOut.WriteUnicodeOrByteText( aStrDec, eCharSet );
233 rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
234 }
235 else
236 {
237 OString aStrEnc = OUStringToOString(aTmpStr, eCharSet);
238 // search on encoded string
239 OString aStrTo = aStrDelimEncoded + aStrDelimEncoded;
240 aStrEnc = aStrEnc.replaceAll(aStrDelimEncoded, aStrTo);
241 // write byte encoded
242 rOut.WriteBytes(aStrDelimEncoded.getStr(), aStrDelimEncoded.getLength());
243 rOut.WriteBytes(aStrEnc.getStr(), aStrEnc.getLength());
244 rOut.WriteBytes(aStrDelimEncoded.getStr(), aStrDelimEncoded.getLength());
245 }
246 rOut.WriteUniOrByteChar( '\n', eCharSet );
247 }
248 }
249 aPrgrsBar.SetState( nRowCnt );
250 }
251
252 assert( aOS.isEmpty() && "aOS should be empty");
253 aOS.append(pSpecDataType_LF);
254 aOS.append(pKeyEOD);
255 aOS.append('\n');
256 rOut.WriteUnicodeOrByteText(aOS);
257 aOS.setLength(0);
258
259 // restore original value
260 rOut.SetStreamCharSet( eStreamCharSet );
261}
262
263/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SCTAB Tab() const
Definition: address.hxx:283
void PutInOrder(ScAddress &rAddress)
Definition: address.hxx:396
SCROW Row() const
Definition: address.hxx:274
SCCOL Col() const
Definition: address.hxx:279
SC_DLLPUBLIC bool GetTableArea(SCTAB nTab, SCCOL &rEndCol, SCROW &rEndRow, bool bCalcHiddens=false) const
Definition: document.cxx:1033
SC_DLLPUBLIC OUString GetInputString(SCCOL nCol, SCROW nRow, SCTAB nTab, bool bForceSystemLocale=false) const
Definition: document.cxx:3592
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1081
SC_DLLPUBLIC bool HasTable(SCTAB nTab) const
Definition: document.cxx:195
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:217
virtual void ScExportDif(SvStream &, ScDocument *, const ScAddress &rOutPos, const rtl_TextEncoding eDest) override
Definition: difexp.cxx:35
const svl::SharedString & GetString()
FormulaError GetErrCode()
void SetState(sal_uInt64 nVal, sal_uInt64 nNewRange=0)
Definition: progress.hxx:78
ScAddress aEnd
Definition: address.hxx:498
ScAddress aStart
Definition: address.hxx:497
void StartWritingUnicodeText()
std::size_t WriteBytes(const void *pData, std::size_t nSize)
bool WriteUnicodeOrByteText(std::u16string_view rStr, rtl_TextEncoding eDestCharSet)
bool WriteUniOrByteChar(sal_Unicode ch, rtl_TextEncoding eDestCharSet)
void SetStreamCharSet(rtl_TextEncoding eCharSet)
rtl_TextEncoding GetStreamCharSet() const
const OUString & getString() const
const std::u16string_view pKeyTABLE
Definition: difimp.cxx:35
const std::u16string_view pKeyVECTORS
Definition: difimp.cxx:36
const std::u16string_view pKeyTUPLES
Definition: difimp.cxx:37
const std::u16string_view pKeyDATA
Definition: difimp.cxx:38
const std::u16string_view pKeyEOD
Definition: difimp.cxx:40
const std::u16string_view pKeyBOT
Definition: difimp.cxx:39
@ CELLTYPE_EDIT
Definition: global.hxx:276
@ CELLTYPE_STRING
Definition: global.hxx:274
@ CELLTYPE_FORMULA
Definition: global.hxx:275
@ CELLTYPE_NONE
Definition: global.hxx:272
@ CELLTYPE_VALUE
Definition: global.hxx:273
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
TOOLS_DLLPUBLIC std::size_t write_uInt16s_FromOUString(SvStream &rStrm, std::u16string_view rStr, std::size_t nUnits)
This is very similar to ScCellValue, except that it references the original value instead of copying ...
Definition: cellvalue.hxx:108
ScFormulaCell * getFormula() const
Definition: cellvalue.hxx:137
OUString getString(const ScDocument *pDoc) const
Retrieve string value.
Definition: cellvalue.cxx:657
CellType getType() const
Definition: cellvalue.hxx:133
sal_uInt16 sal_Unicode
sal_Int16 SCTAB
Definition: types.hxx:22
sal_Int16 SCCOL
Definition: types.hxx:21
sal_Int32 SCROW
Definition: types.hxx:17