Files
libreoffice/sc/source/ui/dbgui/asciiopt.cxx

377 lines
11 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
re-base on ALv2 code. Includes: Patches contributed by Herbert Duerr i#118735 prevent endless loop if vlookup/hlookup doesn't find anything http://svn.apache.org/viewvc?view=revision&revision=1239673 Patches contributed by Andre Fischer remove lp_solver http://svn.apache.org/viewvc?view=revision&revision=1199180 i#118160: Added external CoinMP library. http://svn.apache.org/viewvc?view=revision&revision=1233909 Patches contributed by Armin Le-Grand i#118485 - Styles for OLEs are not saved. http://svn.apache.org/viewvc?view=revision&revision=1182166 i#118524: apply patch, followup fixes to 118485 http://svn.apache.org/viewvc?view=revision&revision=1186077 Patches contributed by lihuiibm i#108860 - Fix range validation. http://svn.apache.org/viewvc?view=revision&revision=1242846 i#118954 Chart data will lost after copy to different file http://svn.apache.org/viewvc?view=revision&revision=1301345 Patches contributed by Ariel Constenla-Haile Fix Linux build breaker: extra qualification on member http://svn.apache.org/viewvc?view=revision&revision=1301591 i#118696 - i#118697 - Fix some Sheet Tab Color API issues http://svn.apache.org/viewvc?view=revision&revision=1225428 i#118697 - Fix uninitialized variable http://svn.apache.org/viewvc?view=revision&revision=1225859 i#118771 - ScUndoImportTab should preserve tab background color http://svn.apache.org/viewvc?view=revision&revision=1230356 i#118921 - Repaint linked sheet tab background color after updating link http://svn.apache.org/viewvc?view=revision&revision=1245177 i#118927 - Undo/Redo "Update Link" does not reset sheet tab color http://svn.apache.org/viewvc?view=revision&revision=1245241 i#118747 - Copy tab color when transferring sheets across documents http://svn.apache.org/viewvc?view=revision&revision=1230355 Patch contributed by Oliver Rainer-Wittman i#118012 - methods <ScBroadcastAreaSlot::AreaBroadcast(..)> and <ScBroadcastAreaSlot::AreaBroadcastInRange(..)> adapt stl-container iteration in order to avoid destroyed iterators during iteration. http://svn.apache.org/viewvc?view=revision&revision=1297916 Patches contributed by Mathias Bauer gnumake4 work variously http://svn.apache.org/viewvc?view=revision&revision=1394707 http://svn.apache.org/viewvc?view=revision&revision=1394326 http://svn.apache.org/viewvc?view=revision&revision=1396797 http://svn.apache.org/viewvc?view=revision&revision=1397315 Patch contributed by Daniel Rentz calc69: #i116936# fix VBA symbol Cells http://svn.apache.org/viewvc?view=revision&revision=1172135 Patches contributed by leiw: i#118546 CPU 100% on switched off AutoCalculate with Conditional Formatting on date values http://svn.apache.org/viewvc?view=revision&revision=1301380 Re-add new function documentation. Many various cleanups. Add missing calc66: #o11817313# also look at formula result number format, remove redundant binaries.
2012-11-30 12:23:25 +00:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-09-18 16:07:07 +00:00
#include "global.hxx"
#include "scresid.hxx"
#include "impex.hxx"
#include "asciiopt.hxx"
#include "asciiopt.hrc"
#include <comphelper/string.hxx>
#include <osl/thread.h>
2000-12-20 11:18:06 +00:00
#include <rtl/tencinfo.h>
#include <unotools/transliterationwrapper.hxx>
#include "editutil.hxx"
2000-09-18 16:07:07 +00:00
2010-12-06 10:36:07 +00:00
static const sal_Char pStrFix[] = "FIX";
static const sal_Char pStrMrg[] = "MRG";
2000-09-18 16:07:07 +00:00
ScAsciiOptions::ScAsciiOptions() :
bFixedLen ( false ),
aFieldSeps ( OUString(';') ),
bMergeFieldSeps ( false ),
bQuotedFieldAsText(false),
bDetectSpecialNumber(false),
cTextSep ( cDefaultTextSep ),
eCharSet ( osl_getThreadTextEncoding() ),
eLang ( LANGUAGE_SYSTEM ),
bCharSetSystem ( false ),
2000-09-18 16:07:07 +00:00
nStartRow ( 1 ),
nInfoCount ( 0 ),
pColStart ( NULL ),
pColFormat ( NULL )
{
}
ScAsciiOptions::ScAsciiOptions(const ScAsciiOptions& rOpt) :
bFixedLen ( rOpt.bFixedLen ),
aFieldSeps ( rOpt.aFieldSeps ),
bMergeFieldSeps ( rOpt.bMergeFieldSeps ),
bQuotedFieldAsText(rOpt.bQuotedFieldAsText),
bDetectSpecialNumber(rOpt.bDetectSpecialNumber),
2000-09-18 16:07:07 +00:00
cTextSep ( rOpt.cTextSep ),
eCharSet ( rOpt.eCharSet ),
eLang ( rOpt.eLang ),
2000-12-20 11:18:06 +00:00
bCharSetSystem ( rOpt.bCharSetSystem ),
2000-09-18 16:07:07 +00:00
nStartRow ( rOpt.nStartRow ),
nInfoCount ( rOpt.nInfoCount )
{
if (nInfoCount)
{
pColStart = new sal_Int32[nInfoCount];
pColFormat = new sal_uInt8[nInfoCount];
for (sal_uInt16 i=0; i<nInfoCount; i++)
2000-09-18 16:07:07 +00:00
{
pColStart[i] = rOpt.pColStart[i];
pColFormat[i] = rOpt.pColFormat[i];
}
}
else
{
pColStart = NULL;
pColFormat = NULL;
}
}
ScAsciiOptions::~ScAsciiOptions()
{
delete[] pColStart;
delete[] pColFormat;
}
void ScAsciiOptions::SetColInfo( sal_uInt16 nCount, const sal_Int32* pStart, const sal_uInt8* pFormat )
2000-09-18 16:07:07 +00:00
{
delete[] pColStart;
delete[] pColFormat;
nInfoCount = nCount;
if (nInfoCount)
{
pColStart = new sal_Int32[nInfoCount];
pColFormat = new sal_uInt8[nInfoCount];
for (sal_uInt16 i=0; i<nInfoCount; i++)
2000-09-18 16:07:07 +00:00
{
pColStart[i] = pStart[i];
pColFormat[i] = pFormat[i];
}
}
else
{
pColStart = NULL;
pColFormat = NULL;
}
}
2002-08-15 08:29:12 +00:00
void ScAsciiOptions::SetColumnInfo( const ScCsvExpDataVec& rDataVec )
{
delete[] pColStart;
pColStart = NULL;
delete[] pColFormat;
pColFormat = NULL;
2002-08-15 08:29:12 +00:00
nInfoCount = static_cast< sal_uInt16 >( rDataVec.size() );
if( nInfoCount )
{
pColStart = new sal_Int32[ nInfoCount ];
pColFormat = new sal_uInt8[ nInfoCount ];
for( sal_uInt16 nIx = 0; nIx < nInfoCount; ++nIx )
{
2002-08-15 08:29:12 +00:00
pColStart[ nIx ] = rDataVec[ nIx ].mnIndex;
pColFormat[ nIx ] = rDataVec[ nIx ].mnType;
}
}
}
2000-09-18 16:07:07 +00:00
ScAsciiOptions& ScAsciiOptions::operator=( const ScAsciiOptions& rCpy )
{
SetColInfo( rCpy.nInfoCount, rCpy.pColStart, rCpy.pColFormat );
bFixedLen = rCpy.bFixedLen;
aFieldSeps = rCpy.aFieldSeps;
bMergeFieldSeps = rCpy.bMergeFieldSeps;
bQuotedFieldAsText = rCpy.bQuotedFieldAsText;
2000-09-18 16:07:07 +00:00
cTextSep = rCpy.cTextSep;
eCharSet = rCpy.eCharSet;
2000-12-20 11:18:06 +00:00
bCharSetSystem = rCpy.bCharSetSystem;
2000-09-18 16:07:07 +00:00
nStartRow = rCpy.nStartRow;
return *this;
}
bool ScAsciiOptions::operator==( const ScAsciiOptions& rCmp ) const
2000-09-18 16:07:07 +00:00
{
if ( bFixedLen == rCmp.bFixedLen &&
aFieldSeps == rCmp.aFieldSeps &&
bMergeFieldSeps == rCmp.bMergeFieldSeps &&
bQuotedFieldAsText == rCmp.bQuotedFieldAsText &&
2000-09-18 16:07:07 +00:00
cTextSep == rCmp.cTextSep &&
eCharSet == rCmp.eCharSet &&
2000-12-20 11:18:06 +00:00
bCharSetSystem == rCmp.bCharSetSystem &&
2000-09-18 16:07:07 +00:00
nStartRow == rCmp.nStartRow &&
nInfoCount == rCmp.nInfoCount )
{
OSL_ENSURE( !nInfoCount || (pColStart && pColFormat && rCmp.pColStart && rCmp.pColFormat),
"NULL pointer in ScAsciiOptions::operator==() column info" );
for (sal_uInt16 i=0; i<nInfoCount; i++)
2000-09-18 16:07:07 +00:00
if ( pColStart[i] != rCmp.pColStart[i] ||
pColFormat[i] != rCmp.pColFormat[i] )
return false;
2000-09-18 16:07:07 +00:00
return true;
2000-09-18 16:07:07 +00:00
}
return false;
2000-09-18 16:07:07 +00:00
}
static OUString lcl_decodeSepString( const OUString & rSepNums, bool & o_bMergeFieldSeps )
{
OUString aFieldSeps;
sal_Int32 nSub = comphelper::string::getTokenCount( rSepNums, '/');
for (sal_Int32 i=0; i<nSub; ++i)
{
OUString aCode = rSepNums.getToken( i, '/' );
if ( aCode == pStrMrg )
o_bMergeFieldSeps = true;
else
{
sal_Int32 nVal = aCode.toInt32();
if ( nVal )
aFieldSeps += OUString((sal_Unicode) nVal);
}
}
return aFieldSeps;
}
// The options string must not contain semicolons (because of the pick list),
// use comma as separator.
2000-09-18 16:07:07 +00:00
void ScAsciiOptions::ReadFromString( const OUString& rString )
2000-09-18 16:07:07 +00:00
{
sal_Int32 nCount = comphelper::string::getTokenCount(rString, ',');
OUString aToken;
2000-09-18 16:07:07 +00:00
// Field separator.
2000-09-18 16:07:07 +00:00
if ( nCount >= 1 )
{
bFixedLen = bMergeFieldSeps = false;
2000-09-18 16:07:07 +00:00
aToken = rString.getToken(0,',');
if ( aToken == pStrFix )
bFixedLen = true;
aFieldSeps = lcl_decodeSepString( aToken, bMergeFieldSeps);
2000-09-18 16:07:07 +00:00
}
// Text separator.
2000-09-18 16:07:07 +00:00
if ( nCount >= 2 )
{
aToken = rString.getToken(1,',');
sal_Int32 nVal = aToken.toInt32();
2000-09-18 16:07:07 +00:00
cTextSep = (sal_Unicode) nVal;
}
// Text encoding.
2000-09-18 16:07:07 +00:00
if ( nCount >= 3 )
{
aToken = rString.getToken(2,',');
2000-12-20 11:18:06 +00:00
eCharSet = ScGlobal::GetCharsetValue( aToken );
2000-09-18 16:07:07 +00:00
}
// Number of start row.
if ( nCount >= 4 )
2000-09-18 16:07:07 +00:00
{
aToken = rString.getToken(3,',');
nStartRow = aToken.toInt32();
2000-09-18 16:07:07 +00:00
}
// Column info.
if ( nCount >= 5 )
2000-09-18 16:07:07 +00:00
{
delete[] pColStart;
delete[] pColFormat;
aToken = rString.getToken(4,',');
sal_Int32 nSub = comphelper::string::getTokenCount(aToken, '/');
2000-09-18 16:07:07 +00:00
nInfoCount = nSub / 2;
if (nInfoCount)
{
pColStart = new sal_Int32[nInfoCount];
pColFormat = new sal_uInt8[nInfoCount];
for (sal_uInt16 nInfo=0; nInfo<nInfoCount; nInfo++)
2000-09-18 16:07:07 +00:00
{
pColStart[nInfo] = (sal_Int32) aToken.getToken( 2*nInfo, '/' ).toInt32();
pColFormat[nInfo] = (sal_uInt8) aToken.getToken( 2*nInfo+1, '/' ).toInt32();
2000-09-18 16:07:07 +00:00
}
}
else
{
pColStart = NULL;
pColFormat = NULL;
}
}
// Language
if (nCount >= 6)
{
aToken = rString.getToken(5, ',');
eLang = static_cast<LanguageType>(aToken.toInt32());
}
// Import quoted field as text.
if (nCount >= 7)
{
aToken = rString.getToken(6, ',');
bQuotedFieldAsText = aToken == "true";
}
// Detect special numbers.
if (nCount >= 8)
{
aToken = rString.getToken(7, ',');
bDetectSpecialNumber = aToken == "true";
}
else
bDetectSpecialNumber = true; // default of versions that didn't add the parameter
// 9th token is used for "Save as shown" in export options
// 10th token is used for "Save cell formulas" in export options
2000-09-18 16:07:07 +00:00
}
OUString ScAsciiOptions::WriteToString() const
2000-09-18 16:07:07 +00:00
{
OUString aOutStr;
2000-09-18 16:07:07 +00:00
// Field separator.
2000-09-18 16:07:07 +00:00
if ( bFixedLen )
aOutStr += pStrFix;
else if ( aFieldSeps.isEmpty() )
aOutStr += "0";
2000-09-18 16:07:07 +00:00
else
{
sal_Int32 nLen = aFieldSeps.getLength();
for (sal_Int32 i=0; i<nLen; i++)
2000-09-18 16:07:07 +00:00
{
if (i)
aOutStr += "/";
aOutStr += OUString::number(aFieldSeps[i]);
2000-09-18 16:07:07 +00:00
}
if ( bMergeFieldSeps )
{
aOutStr += "/";
aOutStr += pStrMrg;
2000-09-18 16:07:07 +00:00
}
}
// Text delimiter.
aOutStr += "," + OUString::number(cTextSep) + ",";
2000-09-18 16:07:07 +00:00
// Text encoding.
2000-12-20 11:18:06 +00:00
if ( bCharSetSystem ) // force "SYSTEM"
aOutStr += ScGlobal::GetCharsetString( RTL_TEXTENCODING_DONTKNOW );
else
aOutStr += ScGlobal::GetCharsetString( eCharSet );
2000-09-18 16:07:07 +00:00
// Number of start row.
aOutStr += "," + OUString::number(nStartRow) + ",";
2000-09-18 16:07:07 +00:00
// Column info.
OSL_ENSURE( !nInfoCount || (pColStart && pColFormat), "NULL pointer in ScAsciiOptions column info" );
for (sal_uInt16 nInfo=0; nInfo<nInfoCount; nInfo++)
2000-09-18 16:07:07 +00:00
{
if (nInfo)
aOutStr += "/";
aOutStr += OUString::number(pColStart[nInfo]) +
"/" +
OUString::number(pColFormat[nInfo]);
2000-09-18 16:07:07 +00:00
}
// #i112025# the options string is used in macros and linked sheets,
// so new options must be added at the end, to remain compatible
aOutStr += "," +
// Language
OUString::number(eLang) + "," +
// Import quoted field as text.
OUString::boolean( bQuotedFieldAsText ) + "," +
// Detect special numbers.
OUString::boolean( bDetectSpecialNumber );
// 9th token is used for "Save as shown" in export options
// 10th token is used for "Save cell formulas" in export options
2000-09-18 16:07:07 +00:00
return aOutStr;
}
// static
sal_Unicode ScAsciiOptions::GetWeightedFieldSep( const OUString & rFieldSeps, bool bDecodeNumbers )
{
bool bMergeFieldSeps = false;
OUString aFieldSeps( bDecodeNumbers ? lcl_decodeSepString( rFieldSeps, bMergeFieldSeps) : rFieldSeps);
if (aFieldSeps.isEmpty())
{
return 0;
}
else if (aFieldSeps.getLength() == 1)
return aFieldSeps[0];
else
{
// There can be only one separator for output. See also fdo#53449
if (aFieldSeps.indexOf(',') != -1)
return ',';
else if (aFieldSeps.indexOf('\t') != -1)
return '\t';
else if (aFieldSeps.indexOf(';') != -1)
return ';';
else if (aFieldSeps.indexOf(' ') != -1)
return ' ';
else
return aFieldSeps[0];
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */