use insert function instead of for loop
[LibreOffice.git] / sc / source / filter / excel / namebuff.cxx
blobd64e77ccd8cfdcc01b89b642dbb1e45e1f31b72e
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 <docsh.hxx>
24 #include <scextopt.hxx>
25 #include <tokenarray.hxx>
27 #include <root.hxx>
28 #include <xiroot.hxx>
30 #include <osl/diagnose.h>
32 sal_uInt32 StringHashEntry::MakeHashCode( const OUString& r )
34 sal_uInt32 n = 0;
35 const sal_Unicode* pCurrent = r.getStr();
36 sal_Unicode cCurrent = *pCurrent;
38 while( cCurrent )
40 n *= 70;
41 n += static_cast<sal_uInt32>(cCurrent);
42 pCurrent++;
43 cCurrent = *pCurrent;
46 return n;
49 SharedFormulaBuffer::SharedFormulaBuffer(RootData* pRD)
50 : ExcRoot(pRD)
54 SharedFormulaBuffer::~SharedFormulaBuffer()
56 Clear();
59 void SharedFormulaBuffer::Clear()
61 maTokenArrays.clear();
64 void SharedFormulaBuffer::Store( const ScAddress& rPos, const ScTokenArray& rArray )
66 ScTokenArray aCode(rArray.CloneValue());
67 aCode.GenHash();
68 maTokenArrays.emplace(rPos, std::move(aCode));
71 const ScTokenArray* SharedFormulaBuffer::Find( const ScAddress& rRefPos ) const
73 TokenArraysType::const_iterator it = maTokenArrays.find(rRefPos);
74 if (it == maTokenArrays.end())
75 return nullptr;
77 return &it->second;
80 sal_Int16 ExtSheetBuffer::Add( const OUString& rFPAN, const OUString& rTN, const bool bSWB )
82 maEntries.emplace_back( rFPAN, rTN, bSWB );
83 // return 1-based index of EXTERNSHEET
84 return static_cast< sal_Int16 >( maEntries.size() );
87 bool ExtSheetBuffer::GetScTabIndex( sal_uInt16 nExcIndex, sal_uInt16& rScIndex )
89 OSL_ENSURE( nExcIndex,
90 "*ExtSheetBuffer::GetScTabIndex(): Sheet-Index == 0!" );
92 if ( !nExcIndex || nExcIndex > maEntries.size() )
93 return false;
95 Cont* pCur = &maEntries[ nExcIndex - 1 ];
96 sal_uInt16& rTabNum = pCur->nTabNum;
98 if( rTabNum < 0xFFFD )
100 rScIndex = rTabNum;
101 return true;
104 if( rTabNum == 0xFFFF )
105 {// create new table
106 SCTAB nNewTabNum;
107 if( pCur->bSWB )
108 {// table is in the same workbook!
109 if( pExcRoot->pIR->GetDoc().GetTable( pCur->aTab, nNewTabNum ) )
111 rScIndex = rTabNum = static_cast<sal_uInt16>(nNewTabNum);
112 return true;
114 else
115 rTabNum = 0xFFFD;
117 else if( pExcRoot->pIR->GetDocShell() )
118 {// table is 'really' external
119 if( pExcRoot->pIR->GetExtDocOptions().GetDocSettings().mnLinkCnt == 0 )
121 OUString aURL( ScGlobal::GetAbsDocName( pCur->aFile,
122 pExcRoot->pIR->GetDocShell() ) );
123 OUString aTabName( ScGlobal::GetDocTabName( aURL, pCur->aTab ) );
124 if( pExcRoot->pIR->GetDoc().LinkExternalTab( nNewTabNum, aTabName, aURL, pCur->aTab ) )
126 rScIndex = rTabNum = static_cast<sal_uInt16>(nNewTabNum);
127 return true;
129 else
130 rTabNum = 0xFFFE; // no table is created for now -> and likely
131 // will not be created later...
133 else
134 rTabNum = 0xFFFE;
139 return false;
142 void ExtSheetBuffer::Reset()
144 maEntries.clear();
147 bool ExtName::IsOLE() const
149 return ( nFlags & 0x0002 ) != 0;
152 ExtNameBuff::ExtNameBuff( const XclImpRoot& rRoot ) :
153 XclImpRoot( rRoot )
157 void ExtNameBuff::AddDDE( sal_Int16 nRefIdx )
159 ExtName aNew( 0x0001 );
160 maExtNames[ nRefIdx ].push_back( aNew );
163 void ExtNameBuff::AddOLE( sal_Int16 nRefIdx, sal_uInt32 nStorageId )
165 ExtName aNew( 0x0002 );
166 aNew.nStorageId = nStorageId;
167 maExtNames[ nRefIdx ].push_back( aNew );
170 void ExtNameBuff::AddName( sal_Int16 nRefIdx )
172 ExtName aNew( 0x0004 );
173 maExtNames[ nRefIdx ].push_back( aNew );
176 const ExtName* ExtNameBuff::GetNameByIndex( sal_Int16 nRefIdx, sal_uInt16 nNameIdx ) const
178 OSL_ENSURE( nNameIdx > 0, "ExtNameBuff::GetNameByIndex() - invalid name index" );
179 ExtNameMap::const_iterator aIt = maExtNames.find( nRefIdx );
180 return ((aIt != maExtNames.end()) && (0 < nNameIdx) && (nNameIdx <= aIt->second.size())) ? &aIt->second[ nNameIdx - 1 ] : nullptr;
183 void ExtNameBuff::Reset()
185 maExtNames.clear();
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */