summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmsymboltable.cpp
blob: 5cb72b947c5e5d8735d5eb8a0b42db1162437668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2000-2001 by Andreas Zehender
    email                : zehender@kde.org
**************************************************************************

**************************************************************************
*                                                                        *
*  This program is free software; you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation; either version 2 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
**************************************************************************/

#include "pmsymboltable.h"
#include "pmdeclare.h"
#include "pmdebug.h"

#include <string.h>

PMSymbol::PMSymbol( const TQString& id, PMDeclare* o )
{
   setId( id );
   m_type = Object;
   m_pObj = o;
   m_pVal = 0;
   m_pRenamedSymbol = 0;
}

PMSymbol::PMSymbol( const TQString& id, const PMValue& v )
{
   setId( id );
   m_type = Value;
   m_pObj = 0;
   m_pVal = new PMValue( v );
   m_pRenamedSymbol = 0;
}

PMSymbol::~PMSymbol( )
{
   if( m_pVal )
      delete m_pVal;
}

void PMSymbol::setId( const TQString& id )
{
   m_id = id.left( MaxIDLength );
}

PMDeclare* PMSymbol::object( ) const
{
   if( m_type == Object )
      return m_pObj;
   kdError( PMArea ) << "Symbol is not an object\n";
   return 0;
}

PMValue PMSymbol::value( ) const
{
   if( m_type == Value )
      return *m_pVal;
   kdError( PMArea ) << "Symbol is not a value\n";
   return PMValue( );
}


PMSymbolTable::PMSymbolTable( )
      : TQDict<PMSymbol>( 1009 ), m_lastID( 47 )
{
   setAutoDelete( true );
   m_lastID.setAutoDelete( true );
}

PMSymbolTable::~PMSymbolTable( )
{
   clear( );
}

TQString PMSymbolTable::findNewID( const TQString& prefix )
{
   PMSymbol* symbol;
   TQString testID;
   unsigned int number;
   
   int* lastNumber = m_lastID.find( prefix );
   if( lastNumber )
      number = *lastNumber + 1;
   else
      number = 0;

   // find next free id
   do
   {
      testID = prefix + TQString( "%1" ).tqarg( number );
      symbol = find( testID );
      if( symbol )
         number++;
   }
   while( symbol );

   if( lastNumber )
      *lastNumber = number;
   else
      m_lastID.insert( prefix, new int( number ) );

   return testID;
}

PMSymbol* PMSymbolTable::findNewID( const TQString& prefix, PMDeclare* obj )
{
   TQString newID = findNewID( prefix );
   obj->setID( newID );

   PMSymbol* s = new PMSymbol( newID, obj );
//   insert( newID, s );
   
   return s;
}