1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #ifndef SC_NAMEBUFF_HXX
25 #define SC_NAMEBUFF_HXX
26
27 #include <tools/debug.hxx>
28 #include <tools/string.hxx>
29 #include "compiler.hxx"
30 #include "root.hxx"
31 #include "xiroot.hxx"
32
33 #include "rangenam.hxx"
34 #include <unordered_map>
35 #include <list>
36
37 class ScDocument;
38 class ScTokenArray;
39 class NameBuffer;
40
41
42
43
44 class StringHashEntry
45 {
46 private:
47 friend class NameBuffer;
48 String aString;
49 sal_uInt32 nHash;
50
51 static sal_uInt32 MakeHashCode( const String& );
52 public:
53 inline StringHashEntry( const String& );
54 inline StringHashEntry( void );
55 inline void operator =( const sal_Char* );
56 inline void operator =( const String& );
57 inline void operator =( const StringHashEntry& );
58 inline sal_Bool operator ==( const StringHashEntry& ) const;
59 };
60
61
StringHashEntry(void)62 inline StringHashEntry::StringHashEntry( void )
63 {
64 }
65
66
StringHashEntry(const String & r)67 inline StringHashEntry::StringHashEntry( const String& r ) : aString( r )
68 {
69 nHash = MakeHashCode( r );
70 }
71
72
operator =(const sal_Char * p)73 inline void StringHashEntry::operator =( const sal_Char* p )
74 {
75 aString.AssignAscii( p );
76 nHash = MakeHashCode( aString );
77 }
78
79
operator =(const String & r)80 inline void StringHashEntry::operator =( const String& r )
81 {
82 aString = r;
83 nHash = MakeHashCode( r );
84 }
85
86
operator =(const StringHashEntry & r)87 inline void StringHashEntry::operator =( const StringHashEntry& r )
88 {
89 nHash = r.nHash;
90 aString = r.aString;
91 }
92
93
operator ==(const StringHashEntry & r) const94 inline sal_Bool StringHashEntry::operator ==( const StringHashEntry& r ) const
95 {
96 return ( nHash == r.nHash && aString == r.aString );
97 }
98
99
100
101 class NameBuffer : private List, public ExcRoot
102 {
103 private:
104 sal_uInt16 nBase; // Index-Basis
105 public:
106 // inline NameBuffer( void ); //#94039# prevent empty rootdata
107 inline NameBuffer( RootData* );
108 inline NameBuffer( RootData*, sal_uInt16 nNewBase );
109
110 virtual ~NameBuffer();
111 inline const String* Get( sal_uInt16 nIndex );
112 inline sal_uInt16 GetLastIndex( void );
113 inline void SetBase( sal_uInt16 nNewBase = 0 );
114 void operator <<( const String& rNewString );
115 };
116
117 //#94039# prevent empty rootdata
118 //inline NameBuffer::NameBuffer( void )
119 //{
120 // nBase = 0;
121 //}
122
123
NameBuffer(RootData * p)124 inline NameBuffer::NameBuffer( RootData* p ) : ExcRoot( p )
125 {
126 nBase = 0;
127 }
128
129
NameBuffer(RootData * p,sal_uInt16 nNewBase)130 inline NameBuffer::NameBuffer( RootData* p, sal_uInt16 nNewBase ) : ExcRoot( p )
131 {
132 nBase = nNewBase;
133 }
134
135
Get(sal_uInt16 n)136 inline const String* NameBuffer::Get( sal_uInt16 n )
137 {
138 if( n < nBase )
139 return NULL;
140 else
141 {
142 StringHashEntry* pObj = ( StringHashEntry* ) List::GetObject( n );
143
144 if( pObj )
145 return &pObj->aString;
146 else
147 return NULL;
148 }
149 }
150
151
GetLastIndex(void)152 inline sal_uInt16 NameBuffer::GetLastIndex( void )
153 {
154 DBG_ASSERT( Count() + nBase <= 0xFFFF, "*NameBuffer::GetLastIndex(): Ich hab' die Nase voll!" );
155
156 return ( sal_uInt16 ) ( Count() + nBase );
157 }
158
159
SetBase(sal_uInt16 nNewBase)160 inline void NameBuffer::SetBase( sal_uInt16 nNewBase )
161 {
162 nBase = nNewBase;
163 }
164
165
166
167
168 class ShrfmlaBuffer : public ExcRoot
169 {
170 struct ScAddressHashFunc
171 {
172 typedef const ScAddress & argument_type;
173 typedef size_t result_type;
174 size_t operator() (const ScAddress &addr) const;
175 };
176 typedef std::unordered_map <ScAddress, sal_uInt16, ScAddressHashFunc> ShrfmlaHash;
177 typedef std::list <ScRange> ShrfmlaList;
178
179 ShrfmlaHash index_hash;
180 ShrfmlaList index_list;
181 size_t mnCurrIdx;
182
183 public:
184 ShrfmlaBuffer( RootData* pRD );
185 virtual ~ShrfmlaBuffer();
186 void Clear();
187 void Store( const ScRange& rRange, const ScTokenArray& );
188 sal_uInt16 Find (const ScAddress & rAddress ) const;
189
190 static String CreateName( const ScRange& );
191 };
192
193
194
195
196 class RangeNameBufferWK3 : private List
197 {
198 private:
199 struct ENTRY
200 {
201 StringHashEntry aStrHashEntry;
202 ScComplexRefData aScComplexRefDataRel;
203 String aScAbsName;
204 sal_uInt16 nAbsInd; // == 0 -> noch keine Abs-Name!
205 sal_uInt16 nRelInd;
206 sal_Bool bSingleRef;
ENTRYRangeNameBufferWK3::ENTRY207 ENTRY( const String& rName, const String& rScName, const ScComplexRefData& rCRD ) :
208 aStrHashEntry( rName ),
209 aScComplexRefDataRel( rCRD ),
210 aScAbsName( rScName )
211 {
212 nAbsInd = 0;
213 aScAbsName.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "_ABS" ) );
214 }
215 };
216
217 ScTokenArray* pScTokenArray;
218 sal_uInt16 nIntCount;
219 public:
220 RangeNameBufferWK3( void );
221 virtual ~RangeNameBufferWK3();
222 void Add( const String& rName, const ScComplexRefData& rCRD );
223 inline void Add( const String& rName, const ScRange& aScRange );
224 sal_Bool FindRel( const String& rRef, sal_uInt16& rIndex );
225 sal_Bool FindAbs( const String& rRef, sal_uInt16& rIndex );
226 };
227
228
Add(const String & rName,const ScRange & aScRange)229 inline void RangeNameBufferWK3::Add( const String& rName, const ScRange& aScRange )
230 {
231 ScComplexRefData aCRD;
232 ScSingleRefData* pSRD;
233 const ScAddress* pScAddr;
234
235 pSRD = &aCRD.Ref1;
236 pScAddr = &aScRange.aStart;
237 pSRD->SetFlag3D( sal_True );
238 pSRD->nCol = pScAddr->Col();
239 pSRD->nRow = pScAddr->Row();
240 pSRD->nTab = pScAddr->Tab();
241
242 // zunaechst ALLE Refs nur absolut
243 pSRD->SetColRel( sal_False );
244 pSRD->SetRowRel( sal_False );
245 pSRD->SetTabRel( sal_False );
246
247 pSRD = &aCRD.Ref2;
248 pScAddr = &aScRange.aEnd;
249 pSRD->SetFlag3D( sal_True );
250 pSRD->nCol = pScAddr->Col();
251 pSRD->nRow = pScAddr->Row();
252 pSRD->nTab = pScAddr->Tab();
253
254 // zunaechst ALLE Refs nur absolut
255 pSRD->SetColRel( sal_False );
256 pSRD->SetRowRel( sal_False );
257 pSRD->SetTabRel( sal_False );
258
259 Add( rName, aCRD );
260 }
261
262
263
264
265 class ExtSheetBuffer : private List, public ExcRoot
266 {
267 private:
268 struct Cont
269 {
270 String aFile;
271 String aTab;
272 sal_uInt16 nTabNum; // 0xFFFF -> noch nicht angelegt
273 // 0xFFFE -> versucht anzulegen, ging aber schief
274 // 0xFFFD -> soll im selben Workbook sein, findet's aber nicht
275 sal_Bool bSWB;
276 sal_Bool bLink;
ContExtSheetBuffer::Cont277 Cont( const String& rFilePathAndName, const String& rTabName ) :
278 aFile( rFilePathAndName ),
279 aTab( rTabName )
280 {
281 nTabNum = 0xFFFF; // -> Tabelle noch nicht erzeugt
282 bSWB = bLink = sal_False;
283 }
ContExtSheetBuffer::Cont284 Cont( const String& rFilePathAndName, const String& rTabName,
285 const sal_Bool bSameWB ) :
286 aFile( rFilePathAndName ),
287 aTab( rTabName )
288 {
289 nTabNum = 0xFFFF; // -> Tabelle noch nicht erzeugt
290 bSWB = bSameWB;
291 bLink = sal_False;
292 }
293 };
294 public:
295 inline ExtSheetBuffer( RootData* );
296 virtual ~ExtSheetBuffer();
297
298 sal_Int16 Add( const String& rFilePathAndName,
299 const String& rTabName, const sal_Bool bSameWorkbook = sal_False );
300
301 sal_Bool GetScTabIndex( sal_uInt16 nExcSheetIndex, sal_uInt16& rIn_LastTab_Out_ScIndex );
302 sal_Bool IsLink( const sal_uInt16 nExcSheetIndex ) const;
303 sal_Bool GetLink( const sal_uInt16 nExcSheetIndex, String &rAppl, String &rDoc ) const;
304
305 void Reset( void );
306 };
307
308
ExtSheetBuffer(RootData * p)309 inline ExtSheetBuffer::ExtSheetBuffer( RootData* p ) : ExcRoot( p )
310 {
311 }
312
313
314
315
316 struct ExtName
317 {
318 String aName;
319 sal_uInt32 nStorageId;
320 sal_uInt16 nFlags;
321
ExtNameExtName322 inline ExtName( const String& r, sal_uInt16 n ) : aName( r ), nStorageId( 0 ), nFlags( n ) {}
323
324 sal_Bool IsDDE( void ) const;
325 sal_Bool IsOLE( void ) const;
326 };
327
328
329
330
331 class ExtNameBuff : protected XclImpRoot
332 {
333 public:
334 explicit ExtNameBuff( const XclImpRoot& rRoot );
335
336 void AddDDE( const String& rName, sal_Int16 nRefIdx );
337 void AddOLE( const String& rName, sal_Int16 nRefIdx, sal_uInt32 nStorageId );
338 void AddName( const String& rName, sal_Int16 nRefIdx );
339
340 const ExtName* GetNameByIndex( sal_Int16 nRefIdx, sal_uInt16 nNameIdx ) const;
341
342 void Reset();
343
344 private:
345 typedef ::std::vector< ExtName > ExtNameVec;
346 typedef ::std::map< sal_Int16, ExtNameVec > ExtNameMap;
347
348 ExtNameMap maExtNames;
349 };
350
351
352 #endif
353