Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / excel / namebuff.cxx
blob5b3157a6dffeae9fa70aeb4b9b573094667107fe
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 <namebuff.hxx>
22 #include <document.hxx>
23 #include <scextopt.hxx>
24 #include <tokenarray.hxx>
26 #include <root.hxx>
27 #include <xiroot.hxx>
29 #include <osl/diagnose.h>
31 sal_uInt32 StringHashEntry::MakeHashCode( const OUString& r )
33 sal_uInt32 n = 0;
34 const sal_Unicode* pCurrent = r.getStr();
35 sal_Unicode cCurrent = *pCurrent;
37 while( cCurrent )
39 n *= 70;
40 n += static_cast<sal_uInt32>(cCurrent);
41 pCurrent++;
42 cCurrent = *pCurrent;
45 return n;
48 SharedFormulaBuffer::SharedFormulaBuffer(RootData* pRD)
49 : ExcRoot(pRD)
53 SharedFormulaBuffer::~SharedFormulaBuffer()
55 Clear();
58 void SharedFormulaBuffer::Clear()
60 maTokenArrays.clear();
63 void SharedFormulaBuffer::Store( const ScAddress& rPos, const ScTokenArray& rArray )
65 std::unique_ptr<ScTokenArray> xCode(rArray.Clone());
66 xCode->GenHash();
67 maTokenArrays.emplace(rPos, std::move(xCode));
70 const ScTokenArray* SharedFormulaBuffer::Find( const ScAddress& rRefPos ) const
72 TokenArraysType::const_iterator it = maTokenArrays.find(rRefPos);
73 if (it == maTokenArrays.end())
74 return nullptr;
76 return it->second.get();
79 sal_Int16 ExtSheetBuffer::Add( const OUString& rFPAN, const OUString& rTN, const bool bSWB )
81 maEntries.emplace_back( rFPAN, rTN, bSWB );
82 // return 1-based index of EXTERNSHEET
83 return static_cast< sal_Int16 >( maEntries.size() );
86 bool ExtSheetBuffer::GetScTabIndex( sal_uInt16 nExcIndex, sal_uInt16& rScIndex )
88 OSL_ENSURE( nExcIndex,
89 "*ExtSheetBuffer::GetScTabIndex(): Sheet-Index == 0!" );
91 if ( !nExcIndex || nExcIndex > maEntries.size() )
92 return false;
94 Cont* pCur = &maEntries[ nExcIndex - 1 ];
95 sal_uInt16& rTabNum = pCur->nTabNum;
97 if( rTabNum < 0xFFFD )
99 rScIndex = rTabNum;
100 return true;
103 if( rTabNum == 0xFFFF )
104 {// create new table
105 SCTAB nNewTabNum;
106 if( pCur->bSWB )
107 {// table is in the same workbook!
108 if( pExcRoot->pIR->GetDoc().GetTable( pCur->aTab, nNewTabNum ) )
110 rScIndex = rTabNum = static_cast<sal_uInt16>(nNewTabNum);
111 return true;
113 else
114 rTabNum = 0xFFFD;
116 else if( pExcRoot->pIR->GetDocShell() )
117 {// table is 'really' external
118 if( pExcRoot->pIR->GetExtDocOptions().GetDocSettings().mnLinkCnt == 0 )
120 OUString aURL( ScGlobal::GetAbsDocName( pCur->aFile,
121 pExcRoot->pIR->GetDocShell() ) );
122 OUString aTabName( ScGlobal::GetDocTabName( aURL, pCur->aTab ) );
123 if( pExcRoot->pIR->GetDoc().LinkExternalTab( nNewTabNum, aTabName, aURL, pCur->aTab ) )
125 rScIndex = rTabNum = static_cast<sal_uInt16>(nNewTabNum);
126 return true;
128 else
129 rTabNum = 0xFFFE; // no table is created for now -> and likely
130 // will not be created later...
132 else
133 rTabNum = 0xFFFE;
138 return false;
141 void ExtSheetBuffer::Reset()
143 maEntries.clear();
146 bool ExtName::IsOLE() const
148 return ( nFlags & 0x0002 ) != 0;
151 ExtNameBuff::ExtNameBuff( const XclImpRoot& rRoot ) :
152 XclImpRoot( rRoot )
156 void ExtNameBuff::AddDDE( sal_Int16 nRefIdx )
158 ExtName aNew( 0x0001 );
159 maExtNames[ nRefIdx ].push_back( aNew );
162 void ExtNameBuff::AddOLE( sal_Int16 nRefIdx, sal_uInt32 nStorageId )
164 ExtName aNew( 0x0002 );
165 aNew.nStorageId = nStorageId;
166 maExtNames[ nRefIdx ].push_back( aNew );
169 void ExtNameBuff::AddName( sal_Int16 nRefIdx )
171 ExtName aNew( 0x0004 );
172 maExtNames[ nRefIdx ].push_back( aNew );
175 const ExtName* ExtNameBuff::GetNameByIndex( sal_Int16 nRefIdx, sal_uInt16 nNameIdx ) const
177 OSL_ENSURE( nNameIdx > 0, "ExtNameBuff::GetNameByIndex() - invalid name index" );
178 ExtNameMap::const_iterator aIt = maExtNames.find( nRefIdx );
179 return ((aIt != maExtNames.end()) && (0 < nNameIdx) && (nNameIdx <= aIt->second.size())) ? &aIt->second[ nNameIdx - 1 ] : nullptr;
182 void ExtNameBuff::Reset()
184 maExtNames.clear();
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */