Debian Codegraph @

basic/source/sbx/sbxvar.cxx

raw
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
22 #include <tools/stream.hxx>
23 #include <svl/SfxBroadcaster.hxx>
24
25 #include <basic/sbx.hxx>
26 #include <runtime.hxx>
27 #include "sbxres.hxx"
28 #include "sbxconv.hxx"
29 #include <sbunoobj.hxx>
30 #include <rtl/ustrbuf.hxx>
31 #include <sal/log.hxx>
32 #include <global.hxx>
33 #include <unotools/transliterationwrapper.hxx>
34
35 #include <com/sun/star/uno/XInterface.hpp>
36 #include <utility>
37 #include <filefmt.hxx>
38 using namespace com::sun::star::uno;
39
40 // SbxVariable
41
42 SbxVariable::SbxVariable()
43 {
44 }
45
46 SbxVariable::SbxVariable( const SbxVariable& r )
47 : SvRefBase( r ),
48 SbxValue( r ),
49 m_aDeclareClassName( r.m_aDeclareClassName ),
50 m_xComListener( r.m_xComListener),
51 mpPar( r.mpPar ),
52 pInfo( r.pInfo )
53 {
54 #if HAVE_FEATURE_SCRIPTING
55 if( r.m_xComListener.is() )
56 {
57 registerComListenerVariableForBasic( this, r.m_pComListenerParentBasic );
58 }
59 #endif
60 if( r.CanRead() )
61 {
62 pParent = r.pParent;
63 nUserData = r.nUserData;
64 maName = r.maName;
65 nHash = r.nHash;
66 }
67 }
68
69 SbxEnsureParentVariable::SbxEnsureParentVariable(const SbxVariable& r)
70 : SbxVariable(r)
71 , xParent(const_cast<SbxVariable&>(r).GetParent())
72 {
73 assert(GetParent() == xParent.get());
74 }
75
76 void SbxEnsureParentVariable::SetParent(SbxObject* p)
77 {
78 assert(GetParent() == xParent.get());
79 SbxVariable::SetParent(p);
80 xParent = SbxObjectRef(p);
81 assert(GetParent() == xParent.get());
82 }
83
84 SbxVariable::SbxVariable( SbxDataType t ) : SbxValue( t )
85 {
86 }
87
88 SbxVariable::~SbxVariable()
89 {
90 #if HAVE_FEATURE_SCRIPTING
91 if( IsSet( SbxFlagBits::DimAsNew ))
92 {
93 removeDimAsNewRecoverItem( this );
94 }
95 #endif
96 mpBroadcaster.reset();
97 }
98
99 // Broadcasting
100
101 SfxBroadcaster& SbxVariable::GetBroadcaster()
102 {
103 if( !mpBroadcaster )
104 {
105 mpBroadcaster.reset( new SfxBroadcaster );
106 }
107 return *mpBroadcaster;
108 }
109
110 SbxArray* SbxVariable::GetParameters() const
111 {
112 return mpPar.get();
113 }
114
115
116 // Perhaps some day one could cut the parameter 0.
117 // Then the copying will be dropped...
118
119 void SbxVariable::Broadcast( SfxHintId nHintId )
120 {
121 if( !mpBroadcaster || IsSet( SbxFlagBits::NoBroadcast ) )
122 return;
123
124 // Because the method could be called from outside, check the
125 // rights here again
126 if( nHintId == SfxHintId::BasicDataWanted )
127 {
128 if( !CanRead() )
129 {
130 return;
131 }
132 }
133 if( nHintId == SfxHintId::BasicDataChanged )
134 {
135 if( !CanWrite() )
136 {
137 return;
138 }
139 }
140
141 //fdo#86843 Add a ref during the following block to guard against
142 //getting deleted before completing this method
143 SbxVariableRef aBroadcastGuard(this);
144
145 // Avoid further broadcasting
146 std::unique_ptr<SfxBroadcaster> pSave = std::move(mpBroadcaster);
147 SbxFlagBits nSaveFlags = GetFlags();
148 SetFlag( SbxFlagBits::ReadWrite );
149 if( mpPar.is() )
150 {
151 // Register this as element 0, but don't change over the parent!
152 mpPar->GetRef(0) = this;
153 }
154 pSave->Broadcast( SbxHint( nHintId, this ) );
155 mpBroadcaster = std::move(pSave);
156 SetFlags( nSaveFlags );
157 }
158
159 SbxInfo* SbxVariable::GetInfo()
160 {
161 if( !pInfo.is() )
162 {
163 Broadcast( SfxHintId::BasicInfoWanted );
164 if( pInfo.is() )
165 {
166 SetModified( true );
167 }
168 }
169 return pInfo.get();
170 }
171
172 void SbxVariable::SetInfo( SbxInfo* p )
173 {
174 pInfo = p;
175 }
176
177 void SbxVariable::SetParameters( SbxArray* p )
178 {
179 mpPar = p;
180 }
181
182
183 // Name of the variables
184
185 // static
186 OUString SbxVariable::NameToCaseInsensitiveName(const OUString& rName)
187 {
188 return SbGlobal::GetTransliteration().transliterate(rName, 0, rName.getLength());
189 }
190
191 void SbxVariable::SetName( const OUString& rName )
192 {
193 maName = rName;
194 nHash = MakeHashCode( rName );
195 maNameCI.clear();
196 }
197
198 const OUString& SbxVariable::GetName( SbxNameType t ) const
199 {
200 static const char cSuffixes[] = " %&!#@ $";
201 if( t == SbxNameType::NONE )
202 {
203 return maName;
204 }
205 if (t == SbxNameType::CaseInsensitive)
206 {
207 if (maNameCI.isEmpty() && !maName.isEmpty())
208 maNameCI = NameToCaseInsensitiveName(maName);
209 return maNameCI;
210 }
211 // Request parameter-information (not for objects)
212 const_cast<SbxVariable*>(this)->GetInfo();
213 // Append nothing, if it is a simple property (no empty brackets)
214 if (!pInfo.is() || (pInfo->m_Params.empty() && GetClass() == SbxClassType::Property))
215 {
216 return maName;
217 }
218 OUStringBuffer aTmp( maName );
219 if( t == SbxNameType::ShortTypes )
220 {
221 sal_Unicode cType = ' ';
222 // short type? Then fetch it, possible this is 0.
223 SbxDataType et = GetType();
224 if( et <= SbxSTRING )
225 {
226 assert(et >= 0 && size_t(et) < std::size(cSuffixes) - 1);
227 cType = cSuffixes[ et ];
228 }
229 if( cType != ' ' )
230 {
231 aTmp.append(cType);
232 }
233 }
234 aTmp.append("(");
235
236 bool first = true;
237 for (auto const& i : pInfo->m_Params)
238 {
239 int nt = i->eType & 0x0FFF;
240 if (!first)
241 {
242 aTmp.append(",");
243 }
244 first = false;
245 if( i->nFlags & SbxFlagBits::Optional )
246 {
247 aTmp.append( GetSbxRes( StringId::Optional ) );
248 }
249 if( i->eType & SbxBYREF )
250 {
251 aTmp.append( GetSbxRes( StringId::ByRef ) );
252 }
253 aTmp.append( i->aName );
254 sal_Unicode cType = ' ';
255 // short type? Then fetch it, possible this is 0.
256 if( t == SbxNameType::ShortTypes )
257 {
258 if( nt <= SbxSTRING )
259 {
260 cType = cSuffixes[ nt ];
261 }
262 }
263 if( cType != ' ' )
264 {
265 aTmp.append(cType);
266 if( i->eType & SbxARRAY )
267 {
268 aTmp.append("()");
269 }
270 }
271 else
272 {
273 if( i->eType & SbxARRAY )
274 {
275 aTmp.append("()");
276 }
277 // long type?
278 aTmp.append(GetSbxRes( StringId::As ));
279 if( nt < 32 )
280 {
281 aTmp.append(GetSbxRes( static_cast<StringId>( static_cast<int>( StringId::Types ) + nt ) ));
282 }
283 else
284 {
285 aTmp.append(GetSbxRes( StringId::Any ));
286 }
287 }
288 }
289 aTmp.append(")");
290 const_cast<SbxVariable*>(this)->aToolString = aTmp.makeStringAndClear();
291 return aToolString;
292 }
293
294 // Operators
295
296 SbxVariable& SbxVariable::operator=( const SbxVariable& r )
297 {
298 if (this != &r)
299 {
300 SbxValue::operator=( r );
301 // tdf#144353 - copy information about a missing parameter. See SbiRuntime::SetIsMissing.
302 // We cannot unconditionally assign the data about a variable because we would overwrite
303 // the information about parameters (name, type, flags, and ids). For instance, in the case
304 // where a method will be initialized with a literal.
305 if (!pInfo)
306 pInfo = r.pInfo;
307 m_aDeclareClassName = r.m_aDeclareClassName;
308 m_xComListener = r.m_xComListener;
309 m_pComListenerParentBasic = r.m_pComListenerParentBasic;
310 #if HAVE_FEATURE_SCRIPTING
311 if( m_xComListener.is() )
312 {
313 registerComListenerVariableForBasic( this, m_pComListenerParentBasic );
314 }
315 #endif
316 }
317 return *this;
318 }
319
320 // Conversion
321
322 SbxDataType SbxVariable::GetType() const
323 {
324 if( aData.eType == SbxOBJECT )
325 {
326 return aData.pObj ? aData.pObj->GetType() : SbxOBJECT;
327 }
328 else if( aData.eType == SbxVARIANT )
329 {
330 return aData.pObj ? aData.pObj->GetType() : SbxVARIANT;
331 }
332 else
333 {
334 return aData.eType;
335 }
336 }
337
338 SbxClassType SbxVariable::GetClass() const
339 {
340 return SbxClassType::Variable;
341 }
342
343 void SbxVariable::SetModified( bool b )
344 {
345 if( IsSet( SbxFlagBits::NoModify ) )
346 {
347 return;
348 }
349 SbxBase::SetModified( b );
350 if( pParent && pParent != this ) //??? HotFix: Recursion out here MM
351 {
352 pParent->SetModified( b );
353 }
354 }
355
356 void SbxVariable::SetParent( SbxObject* p )
357 {
358 #ifdef DBG_UTIL
359 // Will the parent of a SbxObject be set?
360 if (p && dynamic_cast<SbxObject*>(this))
361 {
362 // then this had to be a child of the new parent
363 bool bFound = false;
364 SbxArray *pChildren = p->GetObjects();
365 if ( pChildren )
366 {
367 for (sal_uInt32 nIdx = 0; !bFound && nIdx < pChildren->Count(); ++nIdx)
368 {
369 bFound = (this == pChildren->Get(nIdx));
370 }
371 }
372 SAL_INFO_IF(
373 !bFound, "basic.sbx",
374 "dangling: [" << GetName() << "].SetParent([" << p->GetName()
375 << "])");
376 }
377 #endif
378
379 pParent = p;
380 }
381
382 const OUString& SbxVariable::GetDeclareClassName() const
383 {
384 return m_aDeclareClassName;
385 }
386
387 void SbxVariable::SetDeclareClassName( const OUString& rDeclareClassName )
388 {
389 m_aDeclareClassName = rDeclareClassName;
390 }
391
392 void SbxVariable::SetComListener( const css::uno::Reference< css::uno::XInterface >& xComListener,
393 StarBASIC* pParentBasic )
394 {
395 m_xComListener = xComListener;
396 m_pComListenerParentBasic = pParentBasic;
397 #if HAVE_FEATURE_SCRIPTING
398 registerComListenerVariableForBasic( this, pParentBasic );
399 #endif
400 }
401
402 void SbxVariable::ClearComListener()
403 {
404 m_xComListener.clear();
405 }
406
407
408 // Loading/Saving
409
410 bool SbxVariable::LoadData( SvStream& rStrm, sal_uInt16 nVer )
411 {
412 sal_uInt8 cMark;
413 rStrm.ReadUChar( cMark );
414 if( cMark == 0xFF )
415 {
416 if( !SbxValue::LoadData( rStrm, nVer ) )
417 {
418 return false;
419 }
420 maName = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
421 RTL_TEXTENCODING_ASCII_US);
422 sal_uInt32 nTemp;
423 rStrm.ReadUInt32( nTemp );
424 nUserData = nTemp;
425 }
426 else
427 {
428 sal_uInt16 nType;
429 rStrm.SeekRel( -1 );
430 rStrm.ReadUInt16( nType );
431 maName = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
432 RTL_TEXTENCODING_ASCII_US);
433 sal_uInt32 nTemp;
434 rStrm.ReadUInt32( nTemp );
435 nUserData = nTemp;
436 // correction: old methods have instead of SbxNULL now SbxEMPTY
437 if( nType == SbxNULL && GetClass() == SbxClassType::Method )
438 {
439 nType = SbxEMPTY;
440 }
441 SbxValues aTmp;
442 OUString aTmpString;
443 OUString aVal;
444 aTmp.eType = aData.eType = static_cast<SbxDataType>(nType);
445 aTmp.pOUString = &aVal;
446 switch( nType )
447 {
448 case SbxBOOL:
449 case SbxERROR:
450 case SbxINTEGER:
451 rStrm.ReadInt16( aTmp.nInteger ); break;
452 case SbxLONG:
453 rStrm.ReadInt32( aTmp.nLong ); break;
454 case SbxSINGLE:
455 {
456 // Floats as ASCII
457 aTmpString = read_uInt16_lenPrefixed_uInt8s_ToOUString(
458 rStrm, RTL_TEXTENCODING_ASCII_US);
459 double d;
460 SbxDataType t;
461 if( ImpScan( aTmpString, d, t, nullptr ) != ERRCODE_NONE || t == SbxDOUBLE )
462 {
463 aTmp.nSingle = 0;
464 return false;
465 }
466 aTmp.nSingle = static_cast<float>(d);
467 break;
468 }
469 case SbxDATE:
470 case SbxDOUBLE:
471 {
472 // Floats as ASCII
473 aTmpString = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
474 RTL_TEXTENCODING_ASCII_US);
475 SbxDataType t;
476 if( ImpScan( aTmpString, aTmp.nDouble, t, nullptr ) != ERRCODE_NONE )
477 {
478 aTmp.nDouble = 0;
479 return false;
480 }
481 break;
482 }
483 case SbxSTRING:
484 aVal = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
485 RTL_TEXTENCODING_ASCII_US);
486 break;
487 case SbxEMPTY:
488 case SbxNULL:
489 break;
490 default:
491 aData.eType = SbxNULL;
492 SAL_WARN( "basic.sbx", "Loaded a non-supported data type" );
493 return false;
494 }
495 // putt value
496 if( nType != SbxNULL && nType != SbxEMPTY && !Put( aTmp ) )
497 {
498 return false;
499 }
500 }
501 rStrm.ReadUChar( cMark );
502 // cMark is also a version number!
503 // 1: initial version
504 // 2: with nUserData
505 if( cMark )
506 {
507 if( cMark > 2 )
508 {
509 return false;
510 }
511 pInfo = new SbxInfo;
512 pInfo->LoadData( rStrm, static_cast<sal_uInt16>(cMark) );
513 }
514 Broadcast( SfxHintId::BasicDataChanged );
515 nHash = MakeHashCode( maName );
516 SetModified( true );
517 return true;
518 }
519
520 std::pair<bool, sal_uInt32> SbxVariable::StoreData( SvStream& rStrm ) const
521 {
522 rStrm.WriteUChar( 0xFF ); // Marker
523 bool bValStore;
524 if( dynamic_cast<const SbxMethod *>(this) != nullptr )
525 {
526 // #50200 Avoid that objects , which during the runtime
527 // as return-value are saved in the method as a value were saved
528 SbxVariable* pThis = const_cast<SbxVariable*>(this);
529 SbxFlagBits nSaveFlags = GetFlags();
530 pThis->SetFlag( SbxFlagBits::Write );
531 pThis->SbxValue::Clear();
532 pThis->SetFlags( nSaveFlags );
533
534 // So that the method will not be executed in any case!
535 // CAST, to avoid const!
536 pThis->SetFlag( SbxFlagBits::NoBroadcast );
537 bValStore = SbxValue::StoreData( rStrm ).first;
538 pThis->ResetFlag( SbxFlagBits::NoBroadcast );
539 }
540 else
541 {
542 bValStore = SbxValue::StoreData( rStrm ).first;
543 }
544 if( !bValStore )
545 {
546 return { false, 0 };
547 }
548 write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, maName,
549 RTL_TEXTENCODING_ASCII_US);
550 rStrm.WriteUInt32( nUserData );
551 if( pInfo.is() )
552 {
553 rStrm.WriteUChar( 2 ); // Version 2: with UserData!
554 pInfo->StoreData( rStrm );
555 }
556 else
557 {
558 rStrm.WriteUChar( 0 );
559 }
560 return { true, B_IMG_VERSION_12 };
561 }
562
563 // SbxInfo
564
565 SbxInfo::SbxInfo()
566 : nHelpId(0)
567 {}
568
569 SbxInfo::SbxInfo( OUString a, sal_uInt32 n )
570 : aHelpFile(std::move( a )), nHelpId( n )
571 {}
572
573 SbxHint::~SbxHint() = default;
574
575 void SbxVariable::Dump( SvStream& rStrm, bool bFill )
576 {
577 OString aBNameStr(OUStringToOString(GetName( SbxNameType::ShortTypes ), RTL_TEXTENCODING_ASCII_US));
578 rStrm.WriteOString( "Variable( " )
579 .WriteOString( OString::number(reinterpret_cast<sal_IntPtr>(this)) ).WriteOString( "==" )
580 .WriteOString( aBNameStr );
581 OString aBParentNameStr(OUStringToOString(GetParent()->GetName(), RTL_TEXTENCODING_ASCII_US));
582 if ( GetParent() )
583 {
584 rStrm.WriteOString( " in parent '" ).WriteOString( aBParentNameStr ).WriteOString( "'" );
585 }
586 else
587 {
588 rStrm.WriteOString( " no parent" );
589 }
590 rStrm.WriteOString( " ) " );
591
592 // output also the object at object-vars
593 if ( GetValues_Impl().eType == SbxOBJECT &&
594 GetValues_Impl().pObj &&
595 GetValues_Impl().pObj != this &&
596 GetValues_Impl().pObj != GetParent() )
597 {
598 rStrm.WriteOString( " contains " );
599 static_cast<SbxObject*>(GetValues_Impl().pObj)->Dump( rStrm, bFill );
600 }
601 else
602 {
603 rStrm << endl;
604 }
605 }
606
607 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */