-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhostmap.cpp
More file actions
129 lines (105 loc) · 3.47 KB
/
hostmap.cpp
File metadata and controls
129 lines (105 loc) · 3.47 KB
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
122
123
124
125
126
127
128
129
#include "csapp.h"
//HostEntry Class functions definition
HostEntry::HostEntry(char *hostname, bool cache_enable)
{
strcpy(this->hostname, hostname);
this->cache_enabled = cache_enable;
this->next = NULL;
}
//Host_chain_list is a linked list. The hash table is an array of linked lists. Array actually contains pointers to linked lists.
Host_chain_list::Host_chain_list()
{
this->delete_success = true;
this->hostname_not_found = false;
this->head = NULL;
this->tail = NULL;
}
void Host_chain_list::insert(HostEntry *h)
{
if(this->head == NULL)
{
this->head = h;
this->tail = h;
}
else
{
this->tail->next = h;
this->tail = h;
}
}
HostEntry* Host_chain_list::search(char *hostname)
{
HostEntry *host_entry_ptr = this->head;
while( host_entry_ptr )
{
if( ! strcasecmp(host_entry_ptr->hostname, hostname) )
return host_entry_ptr;
else
host_entry_ptr = host_entry_ptr->next;
}
return NULL;
}
bool Host_chain_list::remove(char *hostname)
{
HostEntry *host_entry_ptr = this->head;
while(host_entry_ptr->hostname != NULL)
{
if( ! strcasecmp(host_entry_ptr->hostname, hostname))
return delete_success;
else
host_entry_ptr = host_entry_ptr->next;
}
return hostname_not_found;
}
//------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------
HostMap::HostMap()
{
no_of_hostnames = 0;
host_chain_lists = new Host_chain_list[HASH_TABLE_SIZE]; // making an array of pointers to Host_chain_list objects
}
int HostMap::HashFunc(const char *str)
{
int c;
int hash = 5381;
while (c = *(str++)) // test expression returns false when c = '\0'. The null character equals zero in value on the ascii table.
hash = ((hash << 5) + hash) + c; // (hash * 33) + c
hash = hash % HASH_TABLE_SIZE;
hash = abs(hash);
if( !(hash >= 0 && hash < HASH_TABLE_SIZE) )
{
printf("\nhas function error : hash value out of range despite trying to contain the range in the has function\n");
return -1;
}
return hash;
}
HostEntry* HostMap::insert(char *hostname, bool cache_enable) // return type is set so only for testing.
{
unsigned int hash = HashFunc(hostname);
if( !(hash >= 0 && hash < HASH_TABLE_SIZE) )
{
printf("\nhas function error : hash value out of range\n");
return NULL;
}
HostEntry* host_entry_ptr = new HostEntry(hostname, cache_enable);
this->host_chain_lists[hash].insert(host_entry_ptr);
this->no_of_hostnames++;
return host_entry_ptr;
}
HostEntry* HostMap::search(char *hostname) //this return type here is i think crucial for the code that is trying to get to the hostinfoptr.
{
int hash = HashFunc(hostname);
HostEntry* host_entry_ptr = host_chain_lists[hash].search(hostname);
return host_entry_ptr;
}
bool HostMap::remove(char *hostname)
{
int hash = HashFunc(hostname);
bool whether_deleted = host_chain_lists[hash].remove(hostname);
this->no_of_hostnames--;
return whether_deleted;
}
HostMap::~HostMap()
{
delete[] host_chain_lists;
}