LibreOffice Module basic (master) 1
sbxarray.cxx
Go to the documentation of this file.
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#include <config_features.h>
21#include <o3tl/safeint.hxx>
22#include <tools/debug.hxx>
23#include <tools/stream.hxx>
24#include <basic/sbx.hxx>
25#include <runtime.hxx>
26
27#include <cstddef>
28#include <optional>
29#include <filefmt.hxx>
30
32{
34 std::optional<OUString> maAlias;
35};
36
37
38// SbxArray
39
41{
42 eType = t;
43 if( t != SbxVARIANT )
45}
46
48{
49 if( &rArray != this )
50 {
51 eType = rArray.eType;
52 Clear();
53 for( const auto& rpSrcRef : rArray.mVarEntries )
54 {
55 SbxVariableRef pSrc_ = rpSrcRef.mpVar;
56 if( !pSrc_.is() )
57 continue;
58
59 if( eType != SbxVARIANT )
60 {
61 // Convert no objects
62 if( eType != SbxOBJECT || pSrc_->GetClass() != SbxClassType::Object )
63 {
64 pSrc_->Convert(eType);
65 }
66 }
67 mVarEntries.push_back( rpSrcRef );
68 }
69 }
70 return *this;
71}
72
74{
75}
76
78{
79 return static_cast<SbxDataType>( eType | SbxARRAY );
80}
81
83{
84 mVarEntries.clear();
85}
86
87sal_uInt32 SbxArray::Count() const
88{
89 return mVarEntries.size();
90}
91
93{
94 // If necessary extend the array
95 DBG_ASSERT( nIdx <= SBX_MAXINDEX32, "SBX: Array-Index > SBX_MAXINDEX32" );
96 // Very Hot Fix
97 if( nIdx > SBX_MAXINDEX32 )
98 {
99 SetError( ERRCODE_BASIC_OUT_OF_RANGE );
100 nIdx = 0;
101 }
102 if ( mVarEntries.size() <= nIdx )
103 mVarEntries.resize(nIdx+1);
104
105 return mVarEntries[nIdx].mpVar;
106}
107
108SbxVariable* SbxArray::Get( sal_uInt32 nIdx )
109{
110 if( !CanRead() )
111 {
112 SetError( ERRCODE_BASIC_PROP_WRITEONLY );
113 return nullptr;
114 }
115 SbxVariableRef& rRef = GetRef( nIdx );
116
117 if ( !rRef.is() )
118 rRef = new SbxVariable( eType );
120 return rRef.get();
121}
122
123void SbxArray::Put( SbxVariable* pVar, sal_uInt32 nIdx )
124{
125 if( !CanWrite() )
126 SetError( ERRCODE_BASIC_PROP_READONLY );
127 else
128 {
129 if( pVar )
130 if( eType != SbxVARIANT )
131 // Convert no objects
132 if( eType != SbxOBJECT || pVar->GetClass() != SbxClassType::Object )
133 pVar->Convert( eType );
134 SbxVariableRef& rRef = GetRef( nIdx );
135 // tdf#122250. It is possible that I hold the last reference to myself, so check, otherwise I might
136 // call SetFlag on myself after I have died.
137 bool removingMyself = rRef && rRef->GetParameters() == this && GetRefCount() == 1;
138 if( rRef.get() != pVar )
139 {
140 rRef = pVar;
141 if (!removingMyself)
142 SetFlag( SbxFlagBits::Modified );
143 }
144 }
145}
146
147OUString SbxArray::GetAlias( sal_uInt32 nIdx )
148{
149 if( !CanRead() )
150 {
151 SetError( ERRCODE_BASIC_PROP_WRITEONLY );
152 return OUString();
153 }
154 SbxVarEntry& rRef = reinterpret_cast<SbxVarEntry&>(GetRef( nIdx ));
155
156 if (!rRef.maAlias)
157 return OUString();
158
159 return *rRef.maAlias;
160}
161
162void SbxArray::PutAlias( const OUString& rAlias, sal_uInt32 nIdx )
163{
164 if( !CanWrite() )
165 {
166 SetError( ERRCODE_BASIC_PROP_READONLY );
167 }
168 else
169 {
170 SbxVarEntry& rRef = reinterpret_cast<SbxVarEntry&>( GetRef( nIdx ) );
171 rRef.maAlias = rAlias;
172 }
173}
174
175void SbxArray::Insert( SbxVariable* pVar, sal_uInt32 nIdx )
176{
177 DBG_ASSERT( mVarEntries.size() <= SBX_MAXINDEX32, "SBX: Array gets too big" );
178 if( mVarEntries.size() > SBX_MAXINDEX32 )
179 {
180 return;
181 }
182 SbxVarEntry p;
183 p.mpVar = pVar;
184 size_t nSize = mVarEntries.size();
185 if( nIdx > nSize )
186 {
187 nIdx = nSize;
188 }
189 if( eType != SbxVARIANT && pVar )
190 {
191 p.mpVar->Convert(eType);
192 }
193 if( nIdx == nSize )
194 {
195 mVarEntries.push_back( p );
196 }
197 else
198 {
199 mVarEntries.insert( mVarEntries.begin() + nIdx, p );
200 }
201 SetFlag( SbxFlagBits::Modified );
202}
203
204void SbxArray::Remove( sal_uInt32 nIdx )
205{
206 if( nIdx < mVarEntries.size() )
207 {
208 mVarEntries.erase( mVarEntries.begin() + nIdx );
209 SetFlag( SbxFlagBits::Modified );
210 }
211}
212
213void SbxArray::Remove( SbxVariable const * pVar )
214{
215 if( pVar )
216 {
217 for( size_t i = 0; i < mVarEntries.size(); i++ )
218 {
219 if (mVarEntries[i].mpVar.get() == pVar)
220 {
221 Remove( i ); break;
222 }
223 }
224 }
225}
226
227// Taking over of the data from the passed array, at which
228// the variable of the same name will be overwritten.
229
230void SbxArray::Merge( SbxArray* p )
231{
232 if (!p)
233 return;
234
235 for (auto& rEntry1: p->mVarEntries)
236 {
237 if (!rEntry1.mpVar.is())
238 continue;
239
240 OUString aName = rEntry1.mpVar->GetName();
241 sal_uInt16 nHash = rEntry1.mpVar->GetHashCode();
242
243 // Is the element by the same name already inside?
244 // Then overwrite!
245 for (auto& rEntry2: mVarEntries)
246 {
247 if (!rEntry2.mpVar.is())
248 continue;
249
250 if (rEntry2.mpVar->GetHashCode() == nHash &&
251 rEntry2.mpVar->GetName().equalsIgnoreAsciiCase(aName))
252 {
253 // Take this element and clear the original.
254 rEntry2.mpVar = rEntry1.mpVar;
255 rEntry1.mpVar.clear();
256 break;
257 }
258 }
259
260 if (rEntry1.mpVar.is())
261 {
262 // We don't have element with the same name. Add a new entry.
263 SbxVarEntry aNewEntry;
264 aNewEntry.mpVar = rEntry1.mpVar;
265 if (rEntry1.maAlias)
266 aNewEntry.maAlias = *rEntry1.maAlias;
267 mVarEntries.push_back(aNewEntry);
268 }
269 }
270}
271
272// Search of an element by his name and type. If an element is an object,
273// it will also be scanned...
274
275SbxVariable* SbxArray::Find( const OUString& rName, SbxClassType t )
276{
277 SbxVariable* p = nullptr;
278 if( mVarEntries.empty() )
279 return nullptr;
280 bool bExtSearch = IsSet( SbxFlagBits::ExtSearch );
281 sal_uInt16 nHash = SbxVariable::MakeHashCode( rName );
282 const OUString aNameCI = SbxVariable::NameToCaseInsensitiveName(rName);
283 for (auto& rEntry : mVarEntries)
284 {
285 if (!rEntry.mpVar.is() || !rEntry.mpVar->IsVisible())
286 continue;
287
288 // The very secure search works as well, if there is no hashcode!
289 sal_uInt16 nVarHash = rEntry.mpVar->GetHashCode();
290 // tdf#148358 - compare the names case-insensitive
291 if ( (!nVarHash || nVarHash == nHash)
292 && (t == SbxClassType::DontCare || rEntry.mpVar->GetClass() == t)
293 && (rEntry.mpVar->GetName(SbxNameType::CaseInsensitive) == aNameCI))
294 {
295 p = rEntry.mpVar.get();
296 p->ResetFlag(SbxFlagBits::ExtFound);
297 break;
298 }
299
300 // Did we have an array/object with extended search?
301 if (bExtSearch && rEntry.mpVar->IsSet(SbxFlagBits::ExtSearch))
302 {
303 switch (rEntry.mpVar->GetClass())
304 {
305 case SbxClassType::Object:
306 {
307 // Objects are not allowed to scan their parent.
308 SbxFlagBits nOld = rEntry.mpVar->GetFlags();
309 rEntry.mpVar->ResetFlag(SbxFlagBits::GlobalSearch);
310 p = static_cast<SbxObject&>(*rEntry.mpVar).Find(rName, t);
311 rEntry.mpVar->SetFlags(nOld);
312 }
313 break;
314 case SbxClassType::Array:
315 // Casting SbxVariable to SbxArray? Really?
316 p = reinterpret_cast<SbxArray&>(*rEntry.mpVar).Find(rName, t);
317 break;
318 default:
319 ;
320 }
321
322 if (p)
323 {
324 p->SetFlag(SbxFlagBits::ExtFound);
325 break;
326 }
327 }
328 }
329 return p;
330}
331
332bool SbxArray::LoadData( SvStream& rStrm, sal_uInt16 /*nVer*/ )
333{
334 sal_uInt16 nElem;
335 Clear();
336 bool bRes = true;
337 SbxFlagBits f = nFlags;
338 nFlags |= SbxFlagBits::Write;
339 rStrm.ReadUInt16( nElem );
340 nElem &= 0x7FFF;
341 for( sal_uInt32 n = 0; n < nElem; n++ )
342 {
343 sal_uInt16 nIdx;
344 rStrm.ReadUInt16( nIdx );
345 SbxVariableRef pVar = static_cast<SbxVariable*>(Load( rStrm ).get());
346 if( pVar )
347 {
348 SbxVariableRef& rRef = GetRef( nIdx );
349 rRef = pVar;
350 }
351 else
352 {
353 bRes = false;
354 break;
355 }
356 }
357 nFlags = f;
358 return bRes;
359}
360
361std::pair<bool, sal_uInt32> SbxArray::StoreData( SvStream& rStrm ) const
362{
363 sal_uInt32 nElem = 0;
364 // Which elements are even defined?
365 for( auto& rEntry: mVarEntries )
366 {
367 if (rEntry.mpVar.is() && !(rEntry.mpVar->GetFlags() & SbxFlagBits::DontStore))
368 nElem++;
369 }
370 rStrm.WriteUInt16( nElem );
371
372 sal_uInt32 nVersion = B_IMG_VERSION_12;
373 for( size_t n = 0; n < mVarEntries.size(); n++ )
374 {
375 const SbxVarEntry& rEntry = mVarEntries[n];
376 if (rEntry.mpVar.is() && !(rEntry.mpVar->GetFlags() & SbxFlagBits::DontStore))
377 {
378 rStrm.WriteUInt16( n );
379 const auto& [bSuccess, nVersionModule] = rEntry.mpVar->Store(rStrm);
380 if (!bSuccess)
381 return { false, 0 };
382 else if (nVersionModule > nVersion)
383 {
384 nVersion = nVersionModule;
385 }
386 }
387 }
388 return { true, nVersion };
389}
390
391// #100883 Method to set method directly to parameter array
392void SbxArray::PutDirect( SbxVariable* pVar, sal_uInt32 nIdx )
393{
394 SbxVariableRef& rRef = GetRef( nIdx );
395 rRef = pVar;
396}
397
398
399// SbxArray
400
401SbxDimArray::SbxDimArray( SbxDataType t ) : SbxArray( t ), mbHasFixedSize( false )
402{
403}
404
405SbxDimArray& SbxDimArray::operator=( const SbxDimArray& rArray )
406{
407 if( &rArray != this )
408 {
409 SbxArray::operator=( static_cast<const SbxArray&>(rArray) );
410 m_vDimensions = rArray.m_vDimensions;
411 mbHasFixedSize = rArray.mbHasFixedSize;
412 }
413 return *this;
414}
415
416SbxDimArray::~SbxDimArray()
417{
418}
419
420void SbxDimArray::Clear()
421{
422 m_vDimensions.clear();
423 SbxArray::Clear();
424}
425
426// Add a dimension
427
428void SbxDimArray::AddDimImpl( sal_Int32 lb, sal_Int32 ub, bool bAllowSize0 )
429{
430 ErrCode eRes = ERRCODE_NONE;
431 if( ub < lb && !bAllowSize0 )
432 {
433 eRes = ERRCODE_BASIC_OUT_OF_RANGE;
434 ub = lb;
435 }
436 SbxDim d;
437 d.nLbound = lb;
438 d.nUbound = ub;
439 d.nSize = ub - lb + 1;
440 m_vDimensions.push_back(d);
441 if( eRes )
442 SetError( eRes );
443}
444
445void SbxDimArray::AddDim( sal_Int32 lb, sal_Int32 ub )
446{
447 AddDimImpl( lb, ub, false );
448}
449
450void SbxDimArray::unoAddDim( sal_Int32 lb, sal_Int32 ub )
451{
452 AddDimImpl( lb, ub, true );
453}
454
455
456// Readout dimension data
457
458bool SbxDimArray::GetDim( sal_Int32 n, sal_Int32& rlb, sal_Int32& rub ) const
459{
460 if( n < 1 || o3tl::make_unsigned(n) > m_vDimensions.size() )
461 {
462 SetError( ERRCODE_BASIC_OUT_OF_RANGE );
463 rub = rlb = 0;
464 return false;
465 }
466 SbxDim d = m_vDimensions[n - 1];
467 rub = d.nUbound;
468 rlb = d.nLbound;
469 return true;
470}
471
472// Element-Ptr with the help of an index list
473
474sal_uInt32 SbxDimArray::Offset( const sal_Int32* pIdx )
475{
476 sal_uInt32 nPos = 0;
477 for( const auto& rDimension : m_vDimensions )
478 {
479 sal_Int32 nIdx = *pIdx++;
480 if( nIdx < rDimension.nLbound || nIdx > rDimension.nUbound )
481 {
482 nPos = sal_uInt32(SBX_MAXINDEX32) + 1; break;
483 }
484 nPos = nPos * rDimension.nSize + nIdx - rDimension.nLbound;
485 }
486 if( m_vDimensions.empty() || nPos > SBX_MAXINDEX32 )
487 {
488 SetError( ERRCODE_BASIC_OUT_OF_RANGE );
489 nPos = 0;
490 }
491 return nPos;
492}
493
494SbxVariable* SbxDimArray::Get( const sal_Int32* pIdx )
495{
496 return SbxArray::Get( Offset( pIdx ) );
497}
498
499void SbxDimArray::Put( SbxVariable* p, const sal_Int32* pIdx )
500{
501 SbxArray::Put( p, Offset( pIdx ) );
502}
503
504// Element-Number with the help of Parameter-Array
505sal_uInt32 SbxDimArray::Offset( SbxArray* pPar )
506{
507#if HAVE_FEATURE_SCRIPTING
508 if (m_vDimensions.empty() || !pPar ||
509 ((m_vDimensions.size() != sal::static_int_cast<size_t>(pPar->Count() - 1))
510 && SbiRuntime::isVBAEnabled()))
511 {
512 SetError( ERRCODE_BASIC_OUT_OF_RANGE );
513 return 0;
514 }
515#endif
516 sal_uInt32 nPos = 0;
517 sal_uInt32 nOff = 1; // Non element 0!
518 for (auto const& vDimension : m_vDimensions)
519 {
520 sal_Int32 nIdx = pPar->Get( nOff++ )->GetLong();
521 if( nIdx < vDimension.nLbound || nIdx > vDimension.nUbound )
522 {
523 nPos = sal_uInt32(SBX_MAXINDEX32)+1;
524 break;
525 }
526 nPos = nPos * vDimension.nSize + nIdx - vDimension.nLbound;
527 if (IsError())
528 break;
529 }
530 if( nPos > o3tl::make_unsigned(SBX_MAXINDEX32) )
531 {
532 SetError( ERRCODE_BASIC_OUT_OF_RANGE );
533 nPos = 0;
534 }
535 return nPos;
536}
537
538SbxVariable* SbxDimArray::Get( SbxArray* pPar )
539{
540 return SbxArray::Get( Offset( pPar ) );
541}
542
543bool SbxDimArray::LoadData( SvStream& rStrm, sal_uInt16 nVer )
544{
545 short nTmp(0);
546 rStrm.ReadInt16(nTmp);
547
548 if (nTmp > 0)
549 {
550 auto nDimension = o3tl::make_unsigned(nTmp);
551
552 const size_t nMinRecordSize = 4;
553 const size_t nMaxPossibleRecords = rStrm.remainingSize() / nMinRecordSize;
554 if (nDimension > nMaxPossibleRecords)
555 {
556 SAL_WARN("basic", "SbxDimArray::LoadData more entries claimed than stream could contain");
557 return false;
558 }
559
560 for (decltype(nDimension) i = 0; i < nDimension && rStrm.GetError() == ERRCODE_NONE; ++i)
561 {
562 sal_Int16 lb(0), ub(0);
563 rStrm.ReadInt16( lb ).ReadInt16( ub );
564 AddDim( lb, ub );
565 }
566 }
567 return SbxArray::LoadData( rStrm, nVer );
568}
569
570std::pair<bool, sal_uInt32> SbxDimArray::StoreData( SvStream& rStrm ) const
571{
572 assert(m_vDimensions.size() <= sal::static_int_cast<size_t>(std::numeric_limits<sal_Int16>::max()));
573 rStrm.WriteInt16( m_vDimensions.size() );
574 for( std::size_t i = 1; i <= m_vDimensions.size(); i++ )
575 {
576 sal_Int32 lb32, ub32;
577 GetDim(i, lb32, ub32);
578 assert(lb32 >= -SBX_MAXINDEX && ub32 <= SBX_MAXINDEX);
579 rStrm.WriteInt16(lb32).WriteInt16(ub32);
580 }
581 return SbxArray::StoreData( rStrm );
582}
583
584/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
Definition: sbx.hxx:95
SbxArray & operator=(const SbxArray &)
Definition: sbxarray.cxx:47
virtual SbxDataType GetType() const override
Definition: sbxarray.cxx:77
sal_uInt32 Count() const
Definition: sbxarray.cxx:87
SbxVariableRef & GetRef(sal_uInt32)
Definition: sbxarray.cxx:92
SbxArray(SbxDataType=SbxVARIANT)
Definition: sbxarray.cxx:40
virtual void Clear() override
Definition: sbxarray.cxx:82
std::vector< SbxVarEntry > mVarEntries
Definition: sbx.hxx:102
virtual ~SbxArray() override
Definition: sbxarray.cxx:73
SbxDataType eType
Definition: sbx.hxx:103
void SetFlag(SbxFlagBits n)
Definition: sbxcore.hxx:108
virtual bool LoadData(SvStream &, sal_uInt16) override
Definition: sbxarray.cxx:543
bool is() const
#define DBG_ASSERT(sCon, aError)
SbxDataType
Definition: sbxdef.hxx:37
@ SbxOBJECT
Definition: sbxdef.hxx:47
@ SbxARRAY
Definition: sbxdef.hxx:80
@ SbxVARIANT
Definition: sbxdef.hxx:51
constexpr auto SBX_MAXINDEX32
Definition: sbxdef.hxx:212
SbxVariableRef mpVar
Definition: sbxarray.cxx:33
std::optional< OUString > maAlias
Definition: sbxarray.cxx:34