fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / dbgui / asciiopt.cxx
blob931febaabe20b603f7e999b9880304e096c549ea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "global.hxx"
21 #include "scresid.hxx"
22 #include "impex.hxx"
23 #include "asciiopt.hxx"
24 #include "asciiopt.hrc"
25 #include <comphelper/string.hxx>
26 #include <osl/thread.h>
27 #include <rtl/tencinfo.h>
28 #include <unotools/transliterationwrapper.hxx>
29 #include "editutil.hxx"
31 static const sal_Char pStrFix[] = "FIX";
32 static const sal_Char pStrMrg[] = "MRG";
34 ScAsciiOptions::ScAsciiOptions() :
35 bFixedLen ( false ),
36 aFieldSeps ( OUString(';') ),
37 bMergeFieldSeps ( false ),
38 bQuotedFieldAsText(false),
39 bDetectSpecialNumber(false),
40 cTextSep ( cDefaultTextSep ),
41 eCharSet ( osl_getThreadTextEncoding() ),
42 eLang ( LANGUAGE_SYSTEM ),
43 bCharSetSystem ( false ),
44 nStartRow ( 1 ),
45 nInfoCount ( 0 ),
46 pColStart ( NULL ),
47 pColFormat ( NULL )
51 ScAsciiOptions::ScAsciiOptions(const ScAsciiOptions& rOpt) :
52 bFixedLen ( rOpt.bFixedLen ),
53 aFieldSeps ( rOpt.aFieldSeps ),
54 bMergeFieldSeps ( rOpt.bMergeFieldSeps ),
55 bQuotedFieldAsText(rOpt.bQuotedFieldAsText),
56 bDetectSpecialNumber(rOpt.bDetectSpecialNumber),
57 cTextSep ( rOpt.cTextSep ),
58 eCharSet ( rOpt.eCharSet ),
59 eLang ( rOpt.eLang ),
60 bCharSetSystem ( rOpt.bCharSetSystem ),
61 nStartRow ( rOpt.nStartRow ),
62 nInfoCount ( rOpt.nInfoCount )
64 if (nInfoCount)
66 pColStart = new sal_Int32[nInfoCount];
67 pColFormat = new sal_uInt8[nInfoCount];
68 for (sal_uInt16 i=0; i<nInfoCount; i++)
70 pColStart[i] = rOpt.pColStart[i];
71 pColFormat[i] = rOpt.pColFormat[i];
74 else
76 pColStart = NULL;
77 pColFormat = NULL;
81 ScAsciiOptions::~ScAsciiOptions()
83 delete[] pColStart;
84 delete[] pColFormat;
87 void ScAsciiOptions::SetColInfo( sal_uInt16 nCount, const sal_Int32* pStart, const sal_uInt8* pFormat )
89 delete[] pColStart;
90 delete[] pColFormat;
92 nInfoCount = nCount;
94 if (nInfoCount)
96 pColStart = new sal_Int32[nInfoCount];
97 pColFormat = new sal_uInt8[nInfoCount];
98 for (sal_uInt16 i=0; i<nInfoCount; i++)
100 pColStart[i] = pStart[i];
101 pColFormat[i] = pFormat[i];
104 else
106 pColStart = NULL;
107 pColFormat = NULL;
111 void ScAsciiOptions::SetColumnInfo( const ScCsvExpDataVec& rDataVec )
113 delete[] pColStart;
114 pColStart = NULL;
115 delete[] pColFormat;
116 pColFormat = NULL;
118 nInfoCount = static_cast< sal_uInt16 >( rDataVec.size() );
119 if( nInfoCount )
121 pColStart = new sal_Int32[ nInfoCount ];
122 pColFormat = new sal_uInt8[ nInfoCount ];
123 for( sal_uInt16 nIx = 0; nIx < nInfoCount; ++nIx )
125 pColStart[ nIx ] = rDataVec[ nIx ].mnIndex;
126 pColFormat[ nIx ] = rDataVec[ nIx ].mnType;
131 ScAsciiOptions& ScAsciiOptions::operator=( const ScAsciiOptions& rCpy )
133 SetColInfo( rCpy.nInfoCount, rCpy.pColStart, rCpy.pColFormat );
135 bFixedLen = rCpy.bFixedLen;
136 aFieldSeps = rCpy.aFieldSeps;
137 bMergeFieldSeps = rCpy.bMergeFieldSeps;
138 bQuotedFieldAsText = rCpy.bQuotedFieldAsText;
139 cTextSep = rCpy.cTextSep;
140 eCharSet = rCpy.eCharSet;
141 bCharSetSystem = rCpy.bCharSetSystem;
142 nStartRow = rCpy.nStartRow;
144 return *this;
147 bool ScAsciiOptions::operator==( const ScAsciiOptions& rCmp ) const
149 if ( bFixedLen == rCmp.bFixedLen &&
150 aFieldSeps == rCmp.aFieldSeps &&
151 bMergeFieldSeps == rCmp.bMergeFieldSeps &&
152 bQuotedFieldAsText == rCmp.bQuotedFieldAsText &&
153 cTextSep == rCmp.cTextSep &&
154 eCharSet == rCmp.eCharSet &&
155 bCharSetSystem == rCmp.bCharSetSystem &&
156 nStartRow == rCmp.nStartRow &&
157 nInfoCount == rCmp.nInfoCount )
159 OSL_ENSURE( !nInfoCount || (pColStart && pColFormat && rCmp.pColStart && rCmp.pColFormat),
160 "NULL pointer in ScAsciiOptions::operator==() column info" );
161 for (sal_uInt16 i=0; i<nInfoCount; i++)
162 if ( pColStart[i] != rCmp.pColStart[i] ||
163 pColFormat[i] != rCmp.pColFormat[i] )
164 return false;
166 return true;
168 return false;
171 static OUString lcl_decodeSepString( const OUString & rSepNums, bool & o_bMergeFieldSeps )
173 OUString aFieldSeps;
174 sal_Int32 nSub = comphelper::string::getTokenCount( rSepNums, '/');
175 for (sal_Int32 i=0; i<nSub; ++i)
177 OUString aCode = rSepNums.getToken( i, '/' );
178 if ( aCode == pStrMrg )
179 o_bMergeFieldSeps = true;
180 else
182 sal_Int32 nVal = aCode.toInt32();
183 if ( nVal )
184 aFieldSeps += OUString((sal_Unicode) nVal);
187 return aFieldSeps;
190 // The options string must not contain semicolons (because of the pick list),
191 // use comma as separator.
193 void ScAsciiOptions::ReadFromString( const OUString& rString )
195 sal_Int32 nCount = comphelper::string::getTokenCount(rString, ',');
196 OUString aToken;
198 // Field separator.
199 if ( nCount >= 1 )
201 bFixedLen = bMergeFieldSeps = false;
203 aToken = rString.getToken(0,',');
204 if ( aToken == pStrFix )
205 bFixedLen = true;
206 aFieldSeps = lcl_decodeSepString( aToken, bMergeFieldSeps);
209 // Text separator.
210 if ( nCount >= 2 )
212 aToken = rString.getToken(1,',');
213 sal_Int32 nVal = aToken.toInt32();
214 cTextSep = (sal_Unicode) nVal;
217 // Text encoding.
218 if ( nCount >= 3 )
220 aToken = rString.getToken(2,',');
221 eCharSet = ScGlobal::GetCharsetValue( aToken );
224 // Number of start row.
225 if ( nCount >= 4 )
227 aToken = rString.getToken(3,',');
228 nStartRow = aToken.toInt32();
231 // Column info.
232 if ( nCount >= 5 )
234 delete[] pColStart;
235 delete[] pColFormat;
237 aToken = rString.getToken(4,',');
238 sal_Int32 nSub = comphelper::string::getTokenCount(aToken, '/');
239 nInfoCount = nSub / 2;
240 if (nInfoCount)
242 pColStart = new sal_Int32[nInfoCount];
243 pColFormat = new sal_uInt8[nInfoCount];
244 for (sal_uInt16 nInfo=0; nInfo<nInfoCount; nInfo++)
246 pColStart[nInfo] = (sal_Int32) aToken.getToken( 2*nInfo, '/' ).toInt32();
247 pColFormat[nInfo] = (sal_uInt8) aToken.getToken( 2*nInfo+1, '/' ).toInt32();
250 else
252 pColStart = NULL;
253 pColFormat = NULL;
257 // Language
258 if (nCount >= 6)
260 aToken = rString.getToken(5, ',');
261 eLang = static_cast<LanguageType>(aToken.toInt32());
264 // Import quoted field as text.
265 if (nCount >= 7)
267 aToken = rString.getToken(6, ',');
268 bQuotedFieldAsText = aToken == "true";
271 // Detect special numbers.
272 if (nCount >= 8)
274 aToken = rString.getToken(7, ',');
275 bDetectSpecialNumber = aToken == "true";
277 else
278 bDetectSpecialNumber = true; // default of versions that didn't add the parameter
280 // 9th token is used for "Save as shown" in export options
281 // 10th token is used for "Save cell formulas" in export options
284 OUString ScAsciiOptions::WriteToString() const
286 OUString aOutStr;
288 // Field separator.
289 if ( bFixedLen )
290 aOutStr += pStrFix;
291 else if ( aFieldSeps.isEmpty() )
292 aOutStr += "0";
293 else
295 sal_Int32 nLen = aFieldSeps.getLength();
296 for (sal_Int32 i=0; i<nLen; i++)
298 if (i)
299 aOutStr += "/";
300 aOutStr += OUString::number(aFieldSeps[i]);
302 if ( bMergeFieldSeps )
304 aOutStr += "/";
305 aOutStr += pStrMrg;
309 // Text delimiter.
310 aOutStr += "," + OUString::number(cTextSep) + ",";
312 // Text encoding.
313 if ( bCharSetSystem ) // force "SYSTEM"
314 aOutStr += ScGlobal::GetCharsetString( RTL_TEXTENCODING_DONTKNOW );
315 else
316 aOutStr += ScGlobal::GetCharsetString( eCharSet );
318 // Number of start row.
319 aOutStr += "," + OUString::number(nStartRow) + ",";
321 // Column info.
322 OSL_ENSURE( !nInfoCount || (pColStart && pColFormat), "NULL pointer in ScAsciiOptions column info" );
323 for (sal_uInt16 nInfo=0; nInfo<nInfoCount; nInfo++)
325 if (nInfo)
326 aOutStr += "/";
327 aOutStr += OUString::number(pColStart[nInfo]) +
328 "/" +
329 OUString::number(pColFormat[nInfo]);
332 // #i112025# the options string is used in macros and linked sheets,
333 // so new options must be added at the end, to remain compatible
335 aOutStr += "," +
336 // Language
337 OUString::number(eLang) + "," +
338 // Import quoted field as text.
339 OUString::boolean( bQuotedFieldAsText ) + "," +
340 // Detect special numbers.
341 OUString::boolean( bDetectSpecialNumber );
343 // 9th token is used for "Save as shown" in export options
344 // 10th token is used for "Save cell formulas" in export options
346 return aOutStr;
349 // static
350 sal_Unicode ScAsciiOptions::GetWeightedFieldSep( const OUString & rFieldSeps, bool bDecodeNumbers )
352 bool bMergeFieldSeps = false;
353 OUString aFieldSeps( bDecodeNumbers ? lcl_decodeSepString( rFieldSeps, bMergeFieldSeps) : rFieldSeps);
354 if (aFieldSeps.isEmpty())
356 return 0;
358 else if (aFieldSeps.getLength() == 1)
359 return aFieldSeps[0];
360 else
362 // There can be only one separator for output. See also fdo#53449
363 if (aFieldSeps.indexOf(',') != -1)
364 return ',';
365 else if (aFieldSeps.indexOf('\t') != -1)
366 return '\t';
367 else if (aFieldSeps.indexOf(';') != -1)
368 return ';';
369 else if (aFieldSeps.indexOf(' ') != -1)
370 return ' ';
371 else
372 return aFieldSeps[0];
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */