bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / ui / dbgui / asciiopt.cxx
blob827a2f0651c02ffab3e6ff69cc77d70a3fab7093
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 <rtl/tencinfo.h>
27 #include <unotools/transliterationwrapper.hxx>
28 // ause
29 #include "editutil.hxx"
31 // ============================================================================
33 static const sal_Char pStrFix[] = "FIX";
34 static const sal_Char pStrMrg[] = "MRG";
37 // ============================================================================
39 ScAsciiOptions::ScAsciiOptions() :
40 bFixedLen ( false ),
41 aFieldSeps ( OUString(';') ),
42 bMergeFieldSeps ( false ),
43 bQuotedFieldAsText(false),
44 bDetectSpecialNumber(false),
45 cTextSep ( cDefaultTextSep ),
46 eCharSet ( osl_getThreadTextEncoding() ),
47 eLang ( LANGUAGE_SYSTEM ),
48 bCharSetSystem ( false ),
49 nStartRow ( 1 ),
50 nInfoCount ( 0 ),
51 pColStart ( NULL ),
52 pColFormat ( NULL )
57 ScAsciiOptions::ScAsciiOptions(const ScAsciiOptions& rOpt) :
58 bFixedLen ( rOpt.bFixedLen ),
59 aFieldSeps ( rOpt.aFieldSeps ),
60 bMergeFieldSeps ( rOpt.bMergeFieldSeps ),
61 bQuotedFieldAsText(rOpt.bQuotedFieldAsText),
62 bDetectSpecialNumber(rOpt.bDetectSpecialNumber),
63 cTextSep ( rOpt.cTextSep ),
64 eCharSet ( rOpt.eCharSet ),
65 eLang ( rOpt.eLang ),
66 bCharSetSystem ( rOpt.bCharSetSystem ),
67 nStartRow ( rOpt.nStartRow ),
68 nInfoCount ( rOpt.nInfoCount )
70 if (nInfoCount)
72 pColStart = new sal_Int32[nInfoCount];
73 pColFormat = new sal_uInt8[nInfoCount];
74 for (sal_uInt16 i=0; i<nInfoCount; i++)
76 pColStart[i] = rOpt.pColStart[i];
77 pColFormat[i] = rOpt.pColFormat[i];
80 else
82 pColStart = NULL;
83 pColFormat = NULL;
88 ScAsciiOptions::~ScAsciiOptions()
90 delete[] pColStart;
91 delete[] pColFormat;
95 void ScAsciiOptions::SetColInfo( sal_uInt16 nCount, const sal_Int32* pStart, const sal_uInt8* pFormat )
97 delete[] pColStart;
98 delete[] pColFormat;
100 nInfoCount = nCount;
102 if (nInfoCount)
104 pColStart = new sal_Int32[nInfoCount];
105 pColFormat = new sal_uInt8[nInfoCount];
106 for (sal_uInt16 i=0; i<nInfoCount; i++)
108 pColStart[i] = pStart[i];
109 pColFormat[i] = pFormat[i];
112 else
114 pColStart = NULL;
115 pColFormat = NULL;
120 void ScAsciiOptions::SetColumnInfo( const ScCsvExpDataVec& rDataVec )
122 delete[] pColStart;
123 pColStart = NULL;
124 delete[] pColFormat;
125 pColFormat = NULL;
127 nInfoCount = static_cast< sal_uInt16 >( rDataVec.size() );
128 if( nInfoCount )
130 pColStart = new sal_Int32[ nInfoCount ];
131 pColFormat = new sal_uInt8[ nInfoCount ];
132 for( sal_uInt16 nIx = 0; nIx < nInfoCount; ++nIx )
134 pColStart[ nIx ] = rDataVec[ nIx ].mnIndex;
135 pColFormat[ nIx ] = rDataVec[ nIx ].mnType;
141 ScAsciiOptions& ScAsciiOptions::operator=( const ScAsciiOptions& rCpy )
143 SetColInfo( rCpy.nInfoCount, rCpy.pColStart, rCpy.pColFormat );
145 bFixedLen = rCpy.bFixedLen;
146 aFieldSeps = rCpy.aFieldSeps;
147 bMergeFieldSeps = rCpy.bMergeFieldSeps;
148 bQuotedFieldAsText = rCpy.bQuotedFieldAsText;
149 cTextSep = rCpy.cTextSep;
150 eCharSet = rCpy.eCharSet;
151 bCharSetSystem = rCpy.bCharSetSystem;
152 nStartRow = rCpy.nStartRow;
154 return *this;
158 sal_Bool ScAsciiOptions::operator==( const ScAsciiOptions& rCmp ) const
160 if ( bFixedLen == rCmp.bFixedLen &&
161 aFieldSeps == rCmp.aFieldSeps &&
162 bMergeFieldSeps == rCmp.bMergeFieldSeps &&
163 bQuotedFieldAsText == rCmp.bQuotedFieldAsText &&
164 cTextSep == rCmp.cTextSep &&
165 eCharSet == rCmp.eCharSet &&
166 bCharSetSystem == rCmp.bCharSetSystem &&
167 nStartRow == rCmp.nStartRow &&
168 nInfoCount == rCmp.nInfoCount )
170 OSL_ENSURE( !nInfoCount || (pColStart && pColFormat && rCmp.pColStart && rCmp.pColFormat),
171 "0-Zeiger in ScAsciiOptions" );
172 for (sal_uInt16 i=0; i<nInfoCount; i++)
173 if ( pColStart[i] != rCmp.pColStart[i] ||
174 pColFormat[i] != rCmp.pColFormat[i] )
175 return false;
177 return sal_True;
179 return false;
183 // Der Options-String darf kein Semikolon mehr enthalten (wegen Pickliste)
184 // darum ab Version 336 Komma stattdessen
188 void ScAsciiOptions::ReadFromString( const String& rString )
190 xub_StrLen nCount = comphelper::string::getTokenCount(rString, ',');
191 String aToken;
192 xub_StrLen nSub;
193 xub_StrLen i;
196 // Feld-Trenner
199 if ( nCount >= 1 )
201 bFixedLen = bMergeFieldSeps = false;
202 aFieldSeps.Erase();
204 aToken = rString.GetToken(0,',');
205 if ( aToken.EqualsAscii(pStrFix) )
206 bFixedLen = sal_True;
207 nSub = comphelper::string::getTokenCount(aToken, '/');
208 for ( i=0; i<nSub; i++ )
210 String aCode = aToken.GetToken( i, '/' );
211 if ( aCode.EqualsAscii(pStrMrg) )
212 bMergeFieldSeps = sal_True;
213 else
215 sal_Int32 nVal = aCode.ToInt32();
216 if ( nVal )
217 aFieldSeps += (sal_Unicode) nVal;
223 // Text-Trenner
226 if ( nCount >= 2 )
228 aToken = rString.GetToken(1,',');
229 sal_Int32 nVal = aToken.ToInt32();
230 cTextSep = (sal_Unicode) nVal;
234 // Zeichensatz
237 if ( nCount >= 3 )
239 aToken = rString.GetToken(2,',');
240 eCharSet = ScGlobal::GetCharsetValue( aToken );
244 // Startzeile
247 if ( nCount >= 4 )
249 aToken = rString.GetToken(3,',');
250 nStartRow = aToken.ToInt32();
254 // Spalten-Infos
257 if ( nCount >= 5 )
259 delete[] pColStart;
260 delete[] pColFormat;
262 aToken = rString.GetToken(4,',');
263 nSub = comphelper::string::getTokenCount(aToken, '/');
264 nInfoCount = nSub / 2;
265 if (nInfoCount)
267 pColStart = new sal_Int32[nInfoCount];
268 pColFormat = new sal_uInt8[nInfoCount];
269 for (sal_uInt16 nInfo=0; nInfo<nInfoCount; nInfo++)
271 pColStart[nInfo] = (sal_Int32) aToken.GetToken( 2*nInfo, '/' ).ToInt32();
272 pColFormat[nInfo] = (sal_uInt8) aToken.GetToken( 2*nInfo+1, '/' ).ToInt32();
275 else
277 pColStart = NULL;
278 pColFormat = NULL;
282 // Language
283 if (nCount >= 6)
285 aToken = rString.GetToken(5, ',');
286 eLang = static_cast<LanguageType>(aToken.ToInt32());
289 // Import quoted field as text.
290 if (nCount >= 7)
292 aToken = rString.GetToken(6, ',');
293 bQuotedFieldAsText = aToken.EqualsAscii("true") ? true : false;
296 // Detect special nubmers.
297 if (nCount >= 8)
299 aToken = rString.GetToken(7, ',');
300 bDetectSpecialNumber = aToken.EqualsAscii("true") ? true : false;
302 else
303 bDetectSpecialNumber = sal_True; // default of versions that didn't add the parameter
305 // 9th token is used for "Save as shown" in export options
306 // 10th token is used for "Save cell formulas" in export options
310 String ScAsciiOptions::WriteToString() const
312 OUString aOutStr;
315 // Feld-Trenner
318 if ( bFixedLen )
319 aOutStr += pStrFix;
320 else if ( !aFieldSeps.Len() )
321 aOutStr += "0";
322 else
324 xub_StrLen nLen = aFieldSeps.Len();
325 for (xub_StrLen i=0; i<nLen; i++)
327 if (i)
328 aOutStr += "/";
329 aOutStr += OUString::number(aFieldSeps.GetChar(i));
331 if ( bMergeFieldSeps )
333 aOutStr += "/";
334 aOutStr += pStrMrg;
338 aOutStr += "," +
339 // Text-Trenner
340 OUString::number(cTextSep) + ",";
343 // Zeichensatz
346 if ( bCharSetSystem ) // force "SYSTEM"
347 aOutStr += ScGlobal::GetCharsetString( RTL_TEXTENCODING_DONTKNOW );
348 else
349 aOutStr += ScGlobal::GetCharsetString( eCharSet );
350 aOutStr += "," +
351 // Startzeile
352 OUString::number(nStartRow) + ",";
355 // Spalten-Infos
358 OSL_ENSURE( !nInfoCount || (pColStart && pColFormat), "0-Zeiger in ScAsciiOptions" );
359 for (sal_uInt16 nInfo=0; nInfo<nInfoCount; nInfo++)
361 if (nInfo)
362 aOutStr += "/";
363 aOutStr += OUString::number(pColStart[nInfo]) +
364 "/" +
365 OUString::number(pColFormat[nInfo]);
368 // #i112025# the options string is used in macros and linked sheets,
369 // so new options must be added at the end, to remain compatible
371 aOutStr += "," +
372 // Language
373 OUString::number(eLang) + "," +
374 // Import quoted field as text.
375 OUString::boolean( bQuotedFieldAsText ) + "," +
376 // Detect special numbers.
377 OUString::boolean( bDetectSpecialNumber );
379 // 9th token is used for "Save as shown" in export options
380 // 10th token is used for "Save cell formulas" in export options
382 return aOutStr;
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */