Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / StyleSheet / SymTab.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 // $XConsortium: SymTab.cc /main/4 1996/06/11 17:09:34 cde-hal $
24 #include "SymTab.h"
25 SymbolName::SymbolName(const char *name)
26 : CC_String(name)
27 {
28 }
29
30 unsigned int SymbolName::operator==(const SymbolName &string)
31 {
32   return !compareTo(string, exact);
33 }
34
35 unsigned
36 SymbolTable::hashsym(const SymbolName &string) 
37 {
38   return string.hash();
39 }
40
41
42
43 SymbolTable::SymbolTable()
44 : hashTable<SymbolName, unsigned int>(hashsym),
45   f_IDsAssigned(0)
46 {
47   f_wildCardId = intern("?", true).id();
48   f_unlimitedWildCardId = intern("*", true).id();
49 }
50
51 SymbolTable::~SymbolTable()
52 {
53   gSymTab = 0;
54   // cleanup after ourselves 
55   clearAndDestroy();
56 }
57
58
59 const Symbol 
60 SymbolTable::intern(const char *name, unsigned int assignId)
61 {
62 //cerr << "intern(): name =<" << name << "> " << "this=" << (void*)this << "\n";
63
64   SymbolName sym_name(name);
65
66   unsigned int *id;
67
68   const SymbolName *strptr = findKeyAndValue(&sym_name, id);
69 //cerr << "strptr =" << (void*)strptr << "\n";
70   if (!strptr)
71     {
72       SymbolName *newname = new SymbolName(name);
73       id = new unsigned int((assignId) ? ++f_IDsAssigned : 0);
74
75       insertKeyAndValue(newname, id);
76       strptr = newname;
77     }
78   
79 /*
80 #ifdef DEBUG
81   {
82     hashTableIterator<SymbolName,unsigned int> next(*this);
83
84     cout << "intern(" << name << ")" << endl;
85     while (++next)
86       cout << "\tKey: " << *next.key() << "\tValue: " << *next.value() << endl;
87   }
88 #endif
89 */
90 //cerr << "Final strptr used =" << (void*)strptr << "\n";
91
92   return Symbol(strptr, *id);
93 }
94
95 // /////////////////////////////////////////////////////////////////////////
96 // class Symbol
97 // /////////////////////////////////////////////////////////////////////////
98
99 Symbol::Symbol(const SymbolName *name, unsigned int x)
100 : f_name(name), f_id(x)
101 {
102 }
103
104 Symbol::Symbol(const Symbol &sym)
105 : f_name(sym.f_name), f_id(sym.f_id)
106 {
107 }
108
109
110 Symbol
111 Symbol::operator=(const Symbol &other)
112 {
113   f_name = other.f_name; return *this ;
114 }
115
116 const char *
117 Symbol::name() const
118 {
119   return *f_name ;
120 }
121
122 unsigned int
123 Symbol::operator==(const Symbol &sym) const
124 {
125   return sym.f_name == f_name ;
126 }
127
128 // /////////////////////////////////////////////////////////////////////////
129 // Printing
130 // /////////////////////////////////////////////////////////////////////////
131
132
133
134 ostream &operator<<(ostream &o, const Symbol &s)
135 {
136   return s.print(o);
137 }
138 ostream &operator<<(ostream &o, const SymbolTable &st)
139 {
140   return st.print(o);
141 }
142
143 ostream &
144 SymbolTable::print(ostream &o) const
145 {
146   hashTableIterator<SymbolName, unsigned int>
147     next(*(hashTable<SymbolName, unsigned int>*)this);
148   
149   o << '<' << endl;
150
151   while(++next)
152     o << next.key() << endl;
153
154   o << '>' << endl;
155   
156   return o ;
157
158 }
159
160 ostream &
161 Symbol::print(ostream &o) const
162 {
163 #ifdef DEBUG
164   f_name->print(o);
165   return o << '(' << id() << ')';
166 #else
167   return f_name -> print(o);
168 #endif
169 }
170
171 ostream &
172 SymbolName::print(ostream &o) const
173 {
174   return o << data();
175 }
176