Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / dtinfogen / infolib / etc / AttributeStore.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: AttributeStore.C /main/3 1996/08/21 15:46:08 drk $ */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <iostream.h>
29
30 #include "List.h"
31 #include "AttributeRec.h"
32 #include "AttributeStore.h"
33 #include "HashBucketEntry.h"
34
35 // Debugging macro
36 #ifdef DEBUG
37 #define DBG(level) if ( dbgLevel >= level)
38 #else
39 #define DBG(level) if (0)
40 #endif
41
42 //---------------------------------------------------------
43 // AttributeStore : Constructor
44
45 AttributeStore::AttributeStore( int sz ):NumBuckets(sz)
46 {
47
48   char *dbgStr;
49   dbgStr = getenv("OL_DEBUG");
50   dbgLevel = ( dbgStr ? atoi(dbgStr):0);
51
52   HashTable = new HashBucketEntry * [ NumBuckets ];
53   if ( !HashTable ) {
54     cerr << "(ERROR) Unable to allocate memory for HashTable\n";
55     exit(1);
56   }
57
58   for ( int i=0; i < sz; i++) {
59     HashTable[i] = NULL;
60   }
61   
62 }
63
64 //---------------------------------------------------------
65 // AttributeStore : Destructor
66
67 AttributeStore::~AttributeStore()
68 {
69   for ( int i=0; i < NumBuckets; i++ ) {
70     delete HashTable[i];
71   }
72 }
73
74 //---------------------------------------------------------
75 // AttributeStore::hash()
76 int AttributeStore::hash( char *HashKeyStr )
77 {
78   DBG(10) cerr << "(DEBUG) Invoking hash function with HashKeyStr = "
79                << HashKeyStr << endl;
80   
81   int sum=0;
82
83   // This is a simple hash function
84   for ( char *ptr=HashKeyStr; *ptr; ptr++ ) {
85     sum += *ptr;
86   }
87
88   sum = sum % NumBuckets;
89
90   DBG(10) cerr << "(DEBUG) returning value for hash function = "
91                << sum << endl;
92   return ( sum );
93 }
94   
95     
96 //---------------------------------------------------------
97 // AttributeStore::lookup
98 char *AttributeStore::lookup( Token *ThisToken, char *aName )
99 {
100   int HashValue;
101   AttributeRec *aRecPtr;
102   
103   HashValue=hash( aName );
104   aRecPtr = HashTable[ HashValue ]->lookup( aName );
105
106   return ( aRecPtr->GetAttributeValue( ThisToken ) );
107 }
108
109 //---------------------------------------------------------
110 // AttributeStore::add
111
112 int AttributeStore::insert ( AttributeRec *att )
113 {
114
115   int HashValue;
116   HashBucketEntry *BucketEntry;
117
118   HashValue = hash ( att->name );
119   
120   BucketEntry = HashTable [ HashValue ];
121   
122   if ( BucketEntry->lookup ( att->name ) ) {
123     return(0);
124   }
125   else {
126     BucketEntry->insert( att );
127     return(1);
128   }
129
130 }
131
132
133