Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "namebuff.hxx"
22 :
23 : #include <string.h>
24 :
25 : #include "rangenam.hxx"
26 : #include "document.hxx"
27 : #include "compiler.hxx"
28 : #include "scextopt.hxx"
29 :
30 : #include "root.hxx"
31 : #include "tokstack.hxx"
32 : #include "xltools.hxx"
33 : #include "xiroot.hxx"
34 :
35 :
36 14 : sal_uInt32 StringHashEntry::MakeHashCode( const String& r )
37 : {
38 14 : register sal_uInt32 n = 0;
39 14 : const sal_Unicode* pAkt = r.GetBuffer();
40 14 : register sal_Unicode cAkt = *pAkt;
41 :
42 112 : while( cAkt )
43 : {
44 84 : n *= 70;
45 84 : n += ( sal_uInt32 ) cAkt;
46 84 : pAkt++;
47 84 : cAkt = *pAkt;
48 : }
49 :
50 14 : return n;
51 : }
52 :
53 :
54 :
55 :
56 207 : NameBuffer::~NameBuffer()
57 : {
58 69 : std::vector<StringHashEntry*>::iterator pIter;
59 83 : for ( pIter = maHashes.begin(); pIter != maHashes.end(); ++pIter )
60 14 : delete *pIter;
61 138 : }
62 :
63 :
64 : //void NameBuffer::operator <<( const SpString &rNewString )
65 14 : void NameBuffer::operator <<( const String &rNewString )
66 : {
67 : OSL_ENSURE( maHashes.size() + nBase < 0xFFFF,
68 : "*NameBuffer::GetLastIndex(): Ich hab' die Nase voll!" );
69 :
70 14 : maHashes.push_back( new StringHashEntry( rNewString ) );
71 14 : }
72 :
73 :
74 : #if OSL_DEBUG_LEVEL > 0
75 : sal_uInt16 nShrCnt;
76 : #endif
77 :
78 :
79 110 : size_t ShrfmlaBuffer::ScAddressHashFunc::operator() (const ScAddress &addr) const
80 : {
81 : // Use something simple, it is just a hash.
82 110 : return static_cast< sal_uInt16 >( addr.Row() ) | (static_cast< sal_uInt8 >( addr.Col() ) << 16);
83 : }
84 :
85 : const size_t nBase = 16384; // Range~ und Shared~ Dingens mit jeweils der Haelfte Ids
86 46 : ShrfmlaBuffer::ShrfmlaBuffer( RootData* pRD ) :
87 : ExcRoot( pRD ),
88 46 : mnCurrIdx (nBase)
89 : {
90 : #if OSL_DEBUG_LEVEL > 0
91 : nShrCnt = 0;
92 : #endif
93 46 : }
94 :
95 92 : ShrfmlaBuffer::~ShrfmlaBuffer()
96 : {
97 92 : }
98 :
99 156 : void ShrfmlaBuffer::Clear()
100 : {
101 156 : index_hash.clear();
102 : // do not clear index_list, index calculation depends on complete list size...
103 : // do not change mnCurrIdx
104 156 : }
105 :
106 13 : void ShrfmlaBuffer::Store( const ScRange& rRange, const ScTokenArray& rToken )
107 : {
108 13 : String aName( CreateName( rRange.aStart ) );
109 :
110 : OSL_ENSURE( mnCurrIdx <= 0xFFFF, "*ShrfmlaBuffer::Store(): I'm getting sick...!" );
111 :
112 13 : ScRangeData* pData = new ScRangeData( pExcRoot->pIR->GetDocPtr(), aName, rToken, rRange.aStart, RT_SHARED );
113 13 : const ScAddress& rMaxPos = pExcRoot->pIR->GetMaxPos();
114 13 : pData->SetMaxCol(rMaxPos.Col());
115 13 : pData->SetMaxRow(rMaxPos.Row());
116 13 : pData->SetIndex( static_cast< sal_uInt16 >( mnCurrIdx ) );
117 13 : pExcRoot->pIR->GetNamedRanges().insert( pData );
118 13 : index_hash[rRange.aStart] = static_cast< sal_uInt16 >( mnCurrIdx );
119 13 : index_list.push_front (rRange);
120 13 : ++mnCurrIdx;
121 13 : }
122 :
123 :
124 97 : sal_uInt16 ShrfmlaBuffer::Find( const ScAddress & aAddr ) const
125 : {
126 97 : ShrfmlaHash::const_iterator hash = index_hash.find (aAddr);
127 97 : if (hash != index_hash.end())
128 97 : return hash->second;
129 :
130 : // It was not hashed on the top left corner ? do a brute force search
131 0 : unsigned int ind = nBase;
132 0 : for (ShrfmlaList::const_iterator ptr = index_list.end(); ptr != index_list.begin() ; ind++)
133 0 : if ((--ptr)->In (aAddr))
134 0 : return static_cast< sal_uInt16 >( ind );
135 0 : return static_cast< sal_uInt16 >( mnCurrIdx );
136 : }
137 :
138 :
139 : #define SHRFMLA_BASENAME "SHARED_FORMULA_"
140 :
141 13 : String ShrfmlaBuffer::CreateName( const ScRange& r )
142 : {
143 26 : OUString aName = SHRFMLA_BASENAME +
144 52 : OUString::number( r.aStart.Col() ) + "_" +
145 52 : OUString::number( r.aStart.Row() ) + "_" +
146 52 : OUString::number( r.aEnd.Col() ) + "_" +
147 52 : OUString::number( r.aEnd.Row() ) + "_" +
148 39 : OUString::number( r.aStart.Tab() );
149 :
150 13 : return aName;
151 : }
152 :
153 0 : sal_Int16 ExtSheetBuffer::Add( const String& rFPAN, const String& rTN, const sal_Bool bSWB )
154 : {
155 0 : maEntries.push_back( Cont( rFPAN, rTN, bSWB ) );
156 : // return 1-based index of EXTERNSHEET
157 0 : return static_cast< sal_Int16 >( maEntries.size() );
158 : }
159 :
160 :
161 0 : sal_Bool ExtSheetBuffer::GetScTabIndex( sal_uInt16 nExcIndex, sal_uInt16& rScIndex )
162 : {
163 : OSL_ENSURE( nExcIndex,
164 : "*ExtSheetBuffer::GetScTabIndex(): Sheet-Index == 0!" );
165 :
166 0 : if ( !nExcIndex || nExcIndex > maEntries.size() )
167 0 : return false;
168 :
169 0 : Cont* pCur = &maEntries[ nExcIndex - 1 ];
170 0 : sal_uInt16& rTabNum = pCur->nTabNum;
171 :
172 0 : if( rTabNum < 0xFFFD )
173 : {
174 0 : rScIndex = rTabNum;
175 0 : return sal_True;
176 : }
177 :
178 0 : if( rTabNum == 0xFFFF )
179 : {// neue Tabelle erzeugen
180 : SCTAB nNewTabNum;
181 0 : if( pCur->bSWB )
182 : {// Tabelle ist im selben Workbook!
183 0 : if( pExcRoot->pIR->GetDoc().GetTable( pCur->aTab, nNewTabNum ) )
184 : {
185 0 : rScIndex = rTabNum = static_cast<sal_uInt16>(nNewTabNum);
186 0 : return sal_True;
187 : }
188 : else
189 0 : rTabNum = 0xFFFD;
190 : }
191 0 : else if( pExcRoot->pIR->GetDocShell() )
192 : {// Tabelle ist 'echt' extern
193 0 : if( pExcRoot->pIR->GetExtDocOptions().GetDocSettings().mnLinkCnt == 0 )
194 : {
195 : String aURL( ScGlobal::GetAbsDocName( pCur->aFile,
196 0 : pExcRoot->pIR->GetDocShell() ) );
197 0 : String aTabName( ScGlobal::GetDocTabName( aURL, pCur->aTab ) );
198 0 : if( pExcRoot->pIR->GetDoc().LinkExternalTab( nNewTabNum, aTabName, aURL, pCur->aTab ) )
199 : {
200 0 : rScIndex = rTabNum = static_cast<sal_uInt16>(nNewTabNum);
201 0 : return sal_True;
202 : }
203 : else
204 0 : rTabNum = 0xFFFE; // Tabelle einmal nicht angelegt -> wird
205 : // wohl auch nicht mehr gehen...
206 : }
207 : else
208 0 : rTabNum = 0xFFFE;
209 :
210 : }
211 : }
212 :
213 0 : return false;
214 : }
215 :
216 :
217 0 : sal_Bool ExtSheetBuffer::IsLink( const sal_uInt16 nExcIndex ) const
218 : {
219 : OSL_ENSURE( nExcIndex > 0, "*ExtSheetBuffer::IsLink(): Index has to be >0!" );
220 :
221 0 : if (!nExcIndex || nExcIndex > maEntries.size() )
222 0 : return false;
223 :
224 0 : return maEntries[ nExcIndex -1 ].bLink;
225 : }
226 :
227 :
228 0 : sal_Bool ExtSheetBuffer::GetLink( const sal_uInt16 nExcIndex, String& rAppl, String& rDoc ) const
229 : {
230 : OSL_ENSURE( nExcIndex > 0, "*ExtSheetBuffer::GetLink(): Index has to be >0!" );
231 :
232 0 : if (!nExcIndex || nExcIndex > maEntries.size() )
233 0 : return false;
234 :
235 0 : const Cont &rRet = maEntries[ nExcIndex -1 ];
236 :
237 0 : rAppl = rRet.aFile;
238 0 : rDoc = rRet.aTab;
239 :
240 0 : return true;
241 : }
242 :
243 :
244 156 : void ExtSheetBuffer::Reset( void )
245 : {
246 156 : maEntries.clear();
247 156 : }
248 :
249 :
250 :
251 :
252 0 : sal_Bool ExtName::IsDDE( void ) const
253 : {
254 0 : return ( nFlags & 0x0001 ) != 0;
255 : }
256 :
257 :
258 0 : sal_Bool ExtName::IsOLE( void ) const
259 : {
260 0 : return ( nFlags & 0x0002 ) != 0;
261 : }
262 :
263 :
264 46 : ExtNameBuff::ExtNameBuff( const XclImpRoot& rRoot ) :
265 46 : XclImpRoot( rRoot )
266 : {
267 46 : }
268 :
269 :
270 0 : void ExtNameBuff::AddDDE( const String& rName, sal_Int16 nRefIdx )
271 : {
272 0 : ExtName aNew( rName, 0x0001 );
273 0 : maExtNames[ nRefIdx ].push_back( aNew );
274 0 : }
275 :
276 :
277 0 : void ExtNameBuff::AddOLE( const String& rName, sal_Int16 nRefIdx, sal_uInt32 nStorageId )
278 : {
279 0 : ExtName aNew( rName, 0x0002 );
280 0 : aNew.nStorageId = nStorageId;
281 0 : maExtNames[ nRefIdx ].push_back( aNew );
282 0 : }
283 :
284 :
285 0 : void ExtNameBuff::AddName( const String& rName, sal_Int16 nRefIdx )
286 : {
287 0 : ExtName aNew( GetScAddInName( rName ), 0x0004 );
288 0 : maExtNames[ nRefIdx ].push_back( aNew );
289 0 : }
290 :
291 :
292 0 : const ExtName* ExtNameBuff::GetNameByIndex( sal_Int16 nRefIdx, sal_uInt16 nNameIdx ) const
293 : {
294 : OSL_ENSURE( nNameIdx > 0, "ExtNameBuff::GetNameByIndex() - invalid name index" );
295 0 : ExtNameMap::const_iterator aIt = maExtNames.find( nRefIdx );
296 0 : return ((aIt != maExtNames.end()) && (0 < nNameIdx) && (nNameIdx <= aIt->second.size())) ? &aIt->second[ nNameIdx - 1 ] : 0;
297 : }
298 :
299 :
300 0 : void ExtNameBuff::Reset( void )
301 : {
302 0 : maExtNames.clear();
303 0 : }
304 :
305 :
306 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|