1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (c) 1983-2009 Kevin Rosenberg
9 ** This program is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License (version 2) as
11 ** published by the Free Software Foundation.
13 ** This program is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ******************************************************************************/
27 #include "ctsupport.h"
31 KeywordCodeEntry::KeywordCodeEntry (const char* const pszKeyword, int iCode)
32 : m_iCode (iCode), m_pNext(NULL)
34 int nLength = strlen (pszKeyword);
35 char* pszCopy = new char [ nLength + 1];
36 for (int i = 0; i < nLength; i++)
37 pszCopy[i] = tolower (pszKeyword[i]);
40 m_strKeyword = pszCopy;
47 KeywordCodeEntry::matchesKeyword (const char* const pszCompare) const
49 int nLength = strlen (pszCompare);
50 char* pszCopy = new char [ nLength + 1];
51 for (int i = 0; i < nLength; i++)
52 pszCopy[i] = tolower (pszCompare[i]);
56 if (m_strKeyword.compare (pszCompare) == 0)
69 KeywordCodeHashTable::initTable ()
73 for (i = 0; i < HASHSIZE; i++)
74 m_hashTable[i] = NULL;
78 // free all memory allocated to table, then clear table
81 KeywordCodeHashTable::freeTable ()
84 KeywordCodeEntry *p, *np;
86 for (i = 0; i < HASHSIZE; i++) {
98 // form hash value of string s
100 KeywordCodeHashTable::hash (const char* s)
105 hashval += tolower(*s);
109 return (hashval % HASHSIZE);
113 /* Look for s in hash table */
115 KeywordCodeHashTable::lookup (const char* const pszLookup)
117 KeywordCodeEntry *found = NULL;
118 for (KeywordCodeEntry* np = m_hashTable[ hash( pszLookup ) ]; np != NULL; np = np->getNext())
119 if (np->matchesKeyword (pszLookup)) {
120 found = np; // found it
128 KeywordCodeHashTable::installKeywordCode (const char* const pszKeyword, int iCode)
130 KeywordCodeEntry *np = lookup (pszKeyword);
132 if (np == NULL) { // not found
133 np = new KeywordCodeEntry (pszKeyword, iCode);
134 int hashval = hash (np->getKeyword());
135 np->setNext (m_hashTable[ hashval ]);
136 m_hashTable[hashval] = np;
137 } else // already defined