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 pDoc->GetName( nTab, aString );
109 aOS.append(OUString::Concat(pKeyTABLE)
110 + "\n0,1\n\""
111 + aString
112 + "\"\n");
113 rOut.WriteUnicodeOrByteText(aOS);
114 aOS.setLength(0);
115
116 // VECTORS
117 aOS.append(OUString::Concat(pKeyVECTORS)
118 + "\n0,"
119 + OUString::number(static_cast<sal_Int32>(nNumCols))
120 + "\n"
121 + p2DoubleQuotes_LF);
122 rOut.WriteUnicodeOrByteText(aOS);
123 aOS.setLength(0);
124
125 // TUPLES
126 aOS.append(OUString::Concat(pKeyTUPLES)
127 + "\n0,"
128 + OUString::number(static_cast<sal_Int32>(nNumRows))
129 + "\n"
130 + p2DoubleQuotes_LF);
131 rOut.WriteUnicodeOrByteText(aOS);
132 aOS.setLength(0);
133
134 // DATA
135 aOS.append(OUString::Concat(pKeyDATA)
136 + "\n0,0\n"
137 + p2DoubleQuotes_LF);
138 rOut.WriteUnicodeOrByteText(aOS);
139 aOS.setLength(0);
140
141 SCCOL nColCnt;
142 SCROW nRowCnt;
143
144 for( nRowCnt = rRange.aStart.Row() ; nRowCnt <= nEndRow ; nRowCnt++ )
145 {
146 assert( aOS.isEmpty() && "aOS should be empty");
147 aOS.append(OUString::Concat(pSpecDataType_LF)
148 + pKeyBOT
149 + "\n");
150 rOut.WriteUnicodeOrByteText(aOS);
151 aOS.setLength(0);
152 for( nColCnt = rRange.aStart.Col() ; nColCnt <= nEndCol ; nColCnt++ )
153 {
154 assert( aOS.isEmpty() && "aOS should be empty");
155 bool bWriteStringData = false;
156 ScRefCellValue aCell(*pDoc, ScAddress(nColCnt, nRowCnt, nTab));
157
158 switch (aCell.getType())
159 {
160 case CELLTYPE_NONE:
161 aOS.append(pEmptyData);
162 break;
163 case CELLTYPE_VALUE:
164 aString = pDoc->GetInputString( nColCnt, nRowCnt, nTab );
165 aOS.append(pNumData + aString + "\nV\n");
166 break;
167 case CELLTYPE_EDIT:
168 case CELLTYPE_STRING:
169 aString = aCell.getString(pDoc);
170 bWriteStringData = true;
171 break;
172 case CELLTYPE_FORMULA:
173 if (aCell.getFormula()->GetErrCode() != FormulaError::NONE)
174 aOS.append(pNumDataERROR);
175 else if (aCell.getFormula()->IsValue())
176 {
177 aString = pDoc->GetInputString( nColCnt, nRowCnt, nTab );
178 aOS.append(pNumData + aString + "\nV\n");
179 }
180 else
181 {
182 aString = aCell.getFormula()->GetString().getString();
183 bWriteStringData = true;
184 }
185
186 break;
187 default:;
188 }
189
190 if ( !bWriteStringData )
191 {
192 rOut.WriteUnicodeOrByteText(aOS);
193 aOS.setLength(0);
194 }
195 else
196 {
197 // for an explanation why this complicated, see
198 // sc/source/ui/docshell/docsh.cxx:ScDocShell::AsciiSave()
199 // In fact we should create a common method if this would be
200 // needed just one more time...
201 assert( aOS.isEmpty() && "aOS should be empty");
202 OUString aTmpStr = aString;
203 aOS.append(pStringData);
204 rOut.WriteUnicodeOrByteText(aOS, eCharSet);
205 aOS.setLength(0);
206 if ( eCharSet == RTL_TEXTENCODING_UNICODE )
207 {
208 // the goal is to replace cStrDelim by cStrDelim+cStrDelim
209 OUString strFrom(cStrDelim);
210 OUString strTo = strFrom + strFrom;
211 aTmpStr = aTmpStr.replaceAll(strFrom, strTo);
212 rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
213 write_uInt16s_FromOUString(rOut, aTmpStr);
214 rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
215 }
216 else if ( bContextOrNotAsciiEncoding )
217 {
218 // to byte encoding
219 OString aStrEnc = OUStringToOString(aTmpStr, eCharSet);
220 // back to Unicode
221 OUString aStrDec = OStringToOUString(aStrEnc, eCharSet);
222 // search on re-decoded string
223 OUString aStrTo = aStrDelimDecoded + aStrDelimDecoded;
224 aStrDec = aStrDec.replaceAll(aStrDelimDecoded, aStrTo);
225 // write byte re-encoded
226 rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
227 rOut.WriteUnicodeOrByteText( aStrDec, eCharSet );
228 rOut.WriteUniOrByteChar( cStrDelim, eCharSet );
229 }
230 else
231 {
232 OString aStrEnc = OUStringToOString(aTmpStr, eCharSet);
233 // search on encoded string
234 OString aStrTo = aStrDelimEncoded + aStrDelimEncoded;
235 aStrEnc = aStrEnc.replaceAll(aStrDelimEncoded, aStrTo);
236 // write byte encoded
237 rOut.WriteBytes(aStrDelimEncoded.getStr(), aStrDelimEncoded.getLength());
238 rOut.WriteBytes(aStrEnc.getStr(), aStrEnc.getLength());
239 rOut.WriteBytes(aStrDelimEncoded.getStr(), aStrDelimEncoded.getLength());
240 }
241 rOut.WriteUniOrByteChar( '\n', eCharSet );
242 }
243 }
244 aPrgrsBar.SetState( nRowCnt );
245 }
246
247 assert( aOS.isEmpty() && "aOS should be empty");
248 aOS.append(OUString::Concat(pSpecDataType_LF)
249 + pKeyEOD
250 + "\n");
251 rOut.WriteUnicodeOrByteText(aOS);
252 aOS.setLength(0);
253
254 // restore original value
255 rOut.SetStreamCharSet( eStreamCharSet );
256}
257
258/* 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:1008
SC_DLLPUBLIC OUString GetInputString(SCCOL nCol, SCROW nRow, SCTAB nTab, bool bForceSystemLocale=false) const
Definition: document.cxx:3549
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1083
SC_DLLPUBLIC bool HasTable(SCTAB nTab) const
Definition: document.cxx:2502
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:204
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:277
@ CELLTYPE_STRING
Definition: global.hxx:275
@ CELLTYPE_FORMULA
Definition: global.hxx:276
@ CELLTYPE_NONE
Definition: global.hxx:273
@ CELLTYPE_VALUE
Definition: global.hxx:274
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