-
Notifications
You must be signed in to change notification settings - Fork 22
/
cHash.cpp
160 lines (149 loc) · 3.24 KB
/
cHash.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
*
* Copyright (C) 2011-2012 Amr Thabet <amr.thabet[at]student.alx.edu.eg>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to Amr Thabet
* amr.thabet[at]student.alx.edu.eg
*
*/
#include "Packetyzer.h"
using namespace Packetyzer::Elements;
cHash::cHash()
{
nItems = 0;
HashArray = 0;
};
cHash::~cHash()
{
for (UINT i=0; i<nItems; i++)
{
delete HashArray[i].Name;
delete HashArray[i].Value;
}
free(HashArray);
};
void cHash::AddItem(cString Name,cString Value)
{
if (nItems == 0)
{
HashArray = (HASH_STRUCT*)malloc(sizeof(HASH_STRUCT)+1);
memset(HashArray,0,sizeof(HASH_STRUCT)+1);
HashArray[0].Name = new cString(Name);
HashArray[0].Value = new cString(Value);
nItems = 1;
}
else
{
HASH_STRUCT* NewArray = (HASH_STRUCT*)malloc(sizeof(HASH_STRUCT)*(nItems+1));
memset(NewArray,0,sizeof(HASH_STRUCT)*(nItems+1));
memcpy(NewArray,HashArray,sizeof(HASH_STRUCT)*nItems);
NewArray[nItems].Name = new cString(Name);
NewArray[nItems].Value = new cString(Value);
nItems++;
free(HashArray);
HashArray = NewArray;
}
}
void cHash::RemoveItem(DWORD id)
{
if (id >= nItems) return;
if (nItems == 1)
{
free(HashArray);
nItems = 0;
return;
}
HASH_STRUCT* NewArray = (HASH_STRUCT*)malloc(sizeof(HASH_STRUCT)*(nItems-1));
memset(NewArray,0,sizeof(HASH_STRUCT)*(nItems-1));
if (id > 0)memcpy(NewArray,HashArray,sizeof(HASH_STRUCT)*id);
memcpy(&NewArray[id],&HashArray[id+1],sizeof(HASH_STRUCT)*(nItems-id-1));
nItems--;
free(HashArray);
HashArray = NewArray;
}
void cHash::RemoveItem(cString Name,int id)
{
for (DWORD i=0;i<nItems;i++)
{
if (*HashArray[i].Name == Name)
{
if (id == 0)
{
RemoveItem(i);
return;
}
id--;
}
}
}
void cHash::ClearItems()
{
nItems = 0;
free(HashArray);
}
cString cHash::GetValue(cString Name,int id)
{
for (DWORD i=0;i<nItems;i++)
{
if (*HashArray[i].Name == Name)
{
if (id == 0)
{
return *HashArray[i].Value;
}
id--;
}
}
return * new cString("");
}
cString cHash::operator[](cString Name)
{
return GetValue(Name);
}
cString cHash::operator[](DWORD id)
{
if (id < nItems)return *HashArray[id].Value;
return cString("");
}
cString cHash::GetKey(DWORD id)
{
if (id < nItems)return *HashArray[id].Name;
return cString("");
}
cString cHash::GetValue(DWORD id)
{
if (id < nItems)return *HashArray[id].Value;
return cString("");
}
bool cHash::IsFound(cString Name)
{
for (DWORD i=0;i<nItems;i++)
{
if (*HashArray[i].Name == Name)return true;
}
return false;
}
DWORD cHash::GetNumberOfItems()
{
return nItems;
}
DWORD cHash::GetNumberOfItems(cString Name)
{
int nThisItem = 0;
for (DWORD i=0; i < nItems; i++)
{
if (*HashArray[i].Name == Name)nThisItem++;
}
return nThisItem;
}