fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / rsc / source / tools / rscdef.cxx
blob7daa2f0ec531c7148a2fdf754cbc261c02a90984
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 .
21 // Programmuebergreifende Includes.
22 #include <rscdef.hxx>
24 bool RscId::bNames = true;
26 void RscId::SetNames( bool bSet )
28 bNames = bSet;
31 sal_Int32 RscId::GetNumber() const
33 sal_Int32 lVal;
34 aExp.Evaluate( &lVal );
35 return lVal;
38 void RscId::Create( const RscExpType & rExpType )
40 aExp = rExpType;
41 if( aExp.IsDefinition() )
42 aExp.aExp.pDef->IncRef();
43 else if( aExp.IsExpression() )
45 sal_Int32 lValue;
47 aExp.Evaluate( &lValue );
48 aExp.SetLong( lValue );
52 void RscId::Destroy()
54 if( aExp.IsDefinition() )
55 aExp.aExp.pDef->DecRef();
56 aExp.cType = RSCEXP_NOTHING;
59 RscId::RscId( const RscId& rRscId )
61 aExp = rRscId.aExp;
62 if( aExp.IsDefinition() )
63 aExp.aExp.pDef->IncRef();
66 RscId::RscId( RscDefine * pDef )
68 RscExpType aExpType;
70 aExpType.aExp.pDef = pDef;
71 aExpType.cType = RSCEXP_DEF;
72 aExpType.cUnused = false;
73 Create( aExpType );
76 RscId& RscId::operator = ( const RscId& rRscId )
78 if( rRscId.aExp.IsDefinition() )
79 rRscId.aExp.aExp.pDef->IncRef();
80 Destroy();
81 aExp = rRscId.aExp;
82 return *this;
85 bool RscId::operator == ( const RscId& rRscId ) const
87 return GetNumber() == rRscId.GetNumber();
90 bool RscId::operator < ( const RscId& rRscId ) const
92 return GetNumber() < rRscId.GetNumber();
95 bool RscId::operator > ( const RscId& rRscId ) const
97 return GetNumber() > rRscId.GetNumber();
100 RscId::operator sal_Int32() const
102 return GetNumber();
105 OString RscId::GetName() const
107 OStringBuffer aStr;
109 if ( !aExp.IsNothing() )
111 if( bNames )
112 aExp.AppendMacro(aStr);
113 else
114 aStr.append(GetNumber());
117 return aStr.makeStringAndClear();
120 RscDefine::RscDefine( sal_uLong lKey, const OString& rDefName, sal_Int32 lDefId )
121 : StringNode( rDefName )
123 nRefCount = 0;
124 lFileKey = lKey;
125 lId = lDefId;
126 pExp = NULL;
129 RscDefine::RscDefine( sal_uLong lKey, const OString& rDefName,
130 RscExpression * pExpression )
131 : StringNode( rDefName )
132 , lId(0)
134 nRefCount = 0;
135 lFileKey = lKey;
136 pExpression->Evaluate( &lId );
137 pExp = pExpression;
140 RscDefine::~RscDefine()
142 delete pExp;
143 if( nRefCount )
144 RscExit( 14 );
147 void RscDefine::DecRef()
149 nRefCount--;
150 if( 0 == nRefCount )
152 delete this;
156 void RscDefine::DefineToNumber()
158 if( pExp )
159 delete pExp;
160 pExp = NULL;
161 SetName(OString::number(lId));
164 bool RscDefine::Evaluate()
166 bool bRet = true;
168 if( pExp )
169 bRet = !pExp->Evaluate( &lId );
171 return bRet;
174 RscDefine * RscDefine::Search( const char * pStr )
176 return static_cast<RscDefine *>(StringNode::Search( pStr ));
179 OString RscDefine::GetMacro()
181 if( pExp )
182 return pExp->GetMacro();
183 return OString::number(lId);
186 RscDefine * RscDefineList::New( sal_uLong lFileKey, const OString& rDefName,
187 sal_Int32 lDefId, size_t lPos )
189 RscDefine * pDef;
191 pDef = new RscDefine( lFileKey, rDefName, lDefId );
192 pDef->IncRef();
193 if ( lPos < maList.size() )
195 RscSubDefList::iterator it = maList.begin();
196 ::std::advance( it, lPos );
197 maList.insert( it, pDef );
199 else
201 maList.push_back( pDef );
203 return pDef;
206 RscDefine * RscDefineList::New( sal_uLong lFileKey, const OString& rDefName,
207 RscExpression * pExpression, size_t lPos )
209 RscDefine * pDef;
211 pDef = new RscDefine( lFileKey, rDefName, pExpression );
212 pDef->IncRef();
213 if ( lPos < maList.size() )
215 RscSubDefList::iterator it = maList.begin();
216 ::std::advance( it, lPos );
217 maList.insert( it, pDef );
219 else
221 maList.push_back( pDef );
223 return pDef;
226 bool RscDefineList::Remove()
228 if ( maList.empty() )
229 return false;
231 maList[ 0 ]->DefineToNumber();
232 maList[ 0 ]->DecRef();
233 maList.erase( maList.begin() );
234 return true;
237 void RscDefineList::WriteAll( FILE * fOutput )
239 for ( size_t i = 0, n = maList.size(); i < n; ++i )
241 RscDefine* pDefEle = maList[ i ];
242 fprintf( fOutput, "#define %s %s\n",
243 pDefEle->GetName().getStr(),
244 pDefEle->GetMacro().getStr()
249 bool RscExpType::Evaluate( sal_Int32 * plValue ) const
251 if( IsDefinition() )
253 aExp.pDef->Evaluate();
254 // Eventuellen Fehler ignorieren
255 *plValue = aExp.pDef->GetNumber();
257 else if( IsExpression() )
258 return aExp.pExp->Evaluate( plValue );
259 else if( IsNothing() )
260 *plValue = 0;
261 else
262 *plValue = GetLong();
264 return true;
267 void RscExpType::AppendMacro(OStringBuffer& rStr) const
269 if( IsDefinition() )
270 rStr.append(aExp.pDef->GetName());
271 else if( IsExpression() )
272 rStr.append(aExp.pExp->GetMacro());
273 else if( IsNumber() )
274 rStr.append(GetLong());
278 RscExpression::RscExpression( RscExpType aLE, char cOp, RscExpType aRE )
280 aLeftExp = aLE;
281 cOperation = cOp;
282 aRightExp = aRE;
283 if( aLeftExp.IsDefinition() )
284 aLeftExp.aExp.pDef->IncRef();
285 if( aRightExp.IsDefinition() )
286 aRightExp.aExp.pDef->IncRef();
289 RscExpression::~RscExpression()
291 if( aLeftExp.IsDefinition() )
292 aLeftExp.aExp.pDef->DecRef();
293 else if( aLeftExp.IsExpression() )
294 delete aLeftExp.aExp.pExp;
296 if( aRightExp.IsDefinition() )
297 aRightExp.aExp.pDef->DecRef();
298 else if( aRightExp.IsExpression() )
299 delete aRightExp.aExp.pExp;
302 bool RscExpression::Evaluate( sal_Int32 * plValue )
304 sal_Int32 lLeft;
305 sal_Int32 lRight;
307 // linken und rechten Zweig auswerten
308 if( aLeftExp.Evaluate( &lLeft ) && aRightExp.Evaluate( &lRight ) )
310 if( cOperation == '&' )
311 *plValue = lLeft & lRight;
312 else if( cOperation == '|' )
313 *plValue = lLeft | lRight;
314 else if( cOperation == '+' )
315 *plValue = lLeft + lRight;
316 else if( cOperation == '-' )
317 *plValue = lLeft - lRight;
318 else if( cOperation == '*' )
319 *plValue = lLeft * lRight;
320 else if( cOperation == 'r' )
321 *plValue = lLeft >> lRight;
322 else if( cOperation == 'l' )
323 *plValue = lLeft << lRight;
324 else
326 if( 0L == lRight )
327 return false;
328 *plValue = lLeft / lRight;
330 return true;
332 return false;
335 OString RscExpression::GetMacro()
337 OStringBuffer aLeft;
339 // Ausgabeoptimierung
340 if( aLeftExp.IsNothing() )
342 if ( '-' == cOperation )
344 aLeft.append('(');
345 aLeft.append('-');
347 aRightExp.AppendMacro(aLeft);
348 if( '-' == cOperation )
350 aLeft.append(')');
353 else if( aRightExp.IsNothing() )
354 aLeftExp.AppendMacro(aLeft);
355 else
357 aLeft.append('(');
358 // linken Zweig auswerten
359 aLeftExp.AppendMacro(aLeft);
361 aLeft.append(cOperation);
363 aLeft.append('(');
364 // rechten Zweig auswerten
365 aRightExp.AppendMacro(aLeft);
366 aLeft.append(')');
368 aLeft.append(')');
371 return aLeft.makeStringAndClear();
374 RscFile :: RscFile()
376 bLoaded = false;
377 bIncFile = false;
378 bDirty = false;
379 bScanned = false;
382 RscFile :: ~RscFile()
384 for ( size_t i = 0, n = aDepLst.size(); i < n; ++i )
385 delete aDepLst[ i ];
386 aDepLst.clear();
388 //von hinten nach vorne ist besser wegen der Abhaengigkeiten
389 //Objekte zerstoeren sich, wenn Referenzzaehler NULL
390 while( aDefLst.Remove() ) ;
393 bool RscFile::Depend( sal_uLong lDepend, sal_uLong lFree )
395 for ( size_t i = aDepLst.size(); i > 0; )
397 RscDepend * pDep = aDepLst[ --i ];
398 if( pDep->GetFileKey() == lDepend )
400 for ( size_t j = i ? --i : 0; j > 0; )
402 pDep = aDepLst[ --j ];
403 if( pDep->GetFileKey() == lFree )
404 return true;
406 return false;
409 return true;
412 bool RscFile :: InsertDependFile( sal_uLong lIncFile, size_t lPos )
414 for ( size_t i = 0, n = aDepLst.size(); i < n; ++i )
416 RscDepend* pDep = aDepLst[ i ];
417 if( pDep->GetFileKey() == lIncFile )
418 return true;
421 // Current-Zeiger steht auf letztem Element
422 if( lPos >= aDepLst.size() )
423 { //letztes Element muss immer letztes bleiben
424 // Abhaengigkeit vor der letzten Position eintragen
425 aDepLst.push_back( new RscDepend( lIncFile ) );
427 else
429 RscDependList::iterator it = aDepLst.begin();
430 ::std::advance( it, lPos );
431 aDepLst.insert( it, new RscDepend( lIncFile ) );
433 return true;
436 RscDefTree::~RscDefTree()
438 Remove();
441 void RscDefTree::Remove()
443 while( pDefRoot )
445 RscDefine * pDef = pDefRoot;
446 pDefRoot = static_cast<RscDefine *>(pDefRoot->Remove( pDefRoot ));
447 pDef->DecRef();
451 RscDefine * RscDefTree::Search( const char * pName )
453 if( pDefRoot )
454 return pDefRoot->Search( pName );
455 return NULL;
458 void RscDefTree::Insert( RscDefine * pDef )
460 if( pDefRoot )
461 pDefRoot->Insert( pDef );
462 else
463 pDefRoot = pDef;
464 pDef->IncRef();
467 void RscDefTree::Remove( RscDefine * pDef )
469 if( pDefRoot )
471 //falls pDef == pDefRoot
472 pDefRoot = static_cast<RscDefine *>(pDefRoot->Remove( pDef ));
474 pDef->DecRef();
477 bool RscDefTree::Evaluate( RscDefine * pDef )
479 if( pDef )
481 if( !Evaluate( static_cast<RscDefine *>(pDef->Left()) ) )
482 return false;
483 if( !Evaluate( static_cast<RscDefine *>(pDef->Right()) ) )
484 return false;
486 return true;
489 RscFileTab::RscFileTab()
493 RscFileTab :: ~RscFileTab()
496 aDefTree.Remove();
498 sal_uIntPtr aIndex = LastIndex();
499 while( aIndex != UNIQUEINDEX_ENTRY_NOTFOUND )
501 delete Remove( aIndex );
502 aIndex = LastIndex();
506 sal_uLong RscFileTab :: Find( const OString& rName )
508 sal_uIntPtr aIndex = FirstIndex();
509 while( aIndex != UNIQUEINDEX_ENTRY_NOTFOUND && (Get(aIndex)->aFileName != rName) )
510 aIndex = NextIndex(aIndex);
512 if( aIndex != UNIQUEINDEX_ENTRY_NOTFOUND )
513 return aIndex;
514 else
515 return NOFILE_INDEX;
518 RscDefine * RscFileTab::FindDef( const char * pName )
520 return aDefTree.Search( pName );
523 /* This method gives back true when lDepend
524 exists and is behind lFree, or when lDepend does not exist. */
525 bool RscFileTab::Depend( sal_uLong lDepend, sal_uLong lFree )
527 if( lDepend == lFree )
528 return true;
530 sal_uIntPtr aIndex = FirstIndex();
531 while( aIndex != UNIQUEINDEX_ENTRY_NOTFOUND )
533 RscFile * pFile = Get(aIndex);
534 if( !pFile->IsIncFile() )
536 if( !pFile->Depend( lDepend, lFree ) )
537 return false;
539 aIndex = NextIndex(aIndex);
542 return true;
545 bool RscFileTab::TestDef( sal_uLong lFileKey, size_t lPos,
546 const RscDefine * pDefDec )
548 if( lFileKey == pDefDec->GetFileKey() )
550 RscFile * pFile = GetFile( pDefDec->GetFileKey() );
551 if( pFile && (lPos <= pFile->aDefLst.GetPos( const_cast<RscDefine *>(pDefDec) ))
552 && (lPos != ULONG_MAX ) )
554 return false;
557 else if( !Depend( lFileKey, pDefDec->GetFileKey() ) )
558 return false;
560 return TestDef( lFileKey, lPos, pDefDec->pExp );
563 bool RscFileTab::TestDef( sal_uLong lFileKey, size_t lPos,
564 const RscExpression * pExpDec )
566 if( !pExpDec )
567 return true;
569 if( pExpDec->aLeftExp.IsExpression() )
570 if( !TestDef( lFileKey, lPos, pExpDec->aLeftExp.aExp.pExp ) )
571 return false;
573 if( pExpDec->aLeftExp.IsDefinition() )
574 if( !TestDef( lFileKey, lPos, pExpDec->aLeftExp.aExp.pDef ) )
575 return false;
577 if( pExpDec->aRightExp.IsExpression() )
578 if( !TestDef( lFileKey, lPos, pExpDec->aRightExp.aExp.pExp ) )
579 return false;
581 if( pExpDec->aRightExp.IsDefinition() )
582 if( !TestDef( lFileKey, lPos, pExpDec->aRightExp.aExp.pDef ) )
583 return false;
585 return true;
588 RscDefine * RscFileTab::NewDef( sal_uLong lFileKey, const OString& rDefName,
589 sal_Int32 lId, sal_uLong lPos )
591 RscDefine * pDef = FindDef( rDefName );
593 if( !pDef )
595 RscFile * pFile = GetFile( lFileKey );
597 if( pFile )
599 pDef = pFile->aDefLst.New( lFileKey, rDefName, lId, lPos );
600 aDefTree.Insert( pDef );
603 else
604 pDef = NULL;
606 return pDef;
609 RscDefine * RscFileTab::NewDef( sal_uLong lFileKey, const OString& rDefName,
610 RscExpression * pExp, sal_uLong lPos )
612 RscDefine * pDef = FindDef( rDefName );
614 if( !pDef )
616 //Macros in den Expressions sind definiert ?
617 if( TestDef( lFileKey, lPos, pExp ) )
619 RscFile * pFile = GetFile( lFileKey );
621 if( pFile )
623 pDef = pFile->aDefLst.New( lFileKey, rDefName, pExp, lPos );
624 aDefTree.Insert( pDef );
628 else
629 pDef = NULL;
631 if( !pDef )
633 // pExp wird immer Eigentum und muss, wenn es nicht benoetigt wird
634 // geloescht werden
635 delete pExp;
637 return pDef;
640 void RscFileTab :: DeleteFileContext( sal_uLong lFileKey )
642 RscFile * pFName;
644 pFName = GetFile( lFileKey );
645 if( pFName )
647 for ( size_t i = 0, n = pFName->aDefLst.maList.size(); i < n; ++i )
649 RscDefine * pDef = pFName->aDefLst.maList[ i ];
650 aDefTree.Remove( pDef );
653 while( pFName->aDefLst.Remove() ) ;
657 sal_uLong RscFileTab :: NewCodeFile( const OString& rName )
659 sal_uLong lKey = Find( rName );
660 if( UNIQUEINDEX_ENTRY_NOTFOUND == lKey )
662 RscFile * pFName = new RscFile();
663 pFName->aFileName = rName;
664 pFName->aPathName = rName;
665 lKey = Insert( pFName );
666 pFName->InsertDependFile( lKey, ULONG_MAX );
668 return lKey;
671 sal_uLong RscFileTab :: NewIncFile(const OString& rName,
672 const OString& rPath)
674 sal_uLong lKey = Find( rName );
675 if( UNIQUEINDEX_ENTRY_NOTFOUND == lKey )
677 RscFile * pFName = new RscFile();
678 pFName->aFileName = rName;
679 pFName->aPathName = rPath;
680 pFName->SetIncFlag();
681 lKey = Insert( pFName );
682 pFName->InsertDependFile( lKey, ULONG_MAX );
684 return lKey;
687 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */