-
Notifications
You must be signed in to change notification settings - Fork 0
/
evidence.c
241 lines (213 loc) · 7.54 KB
/
evidence.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "defs.h"
/*
Function: initEvidenceList
Purpose: initializes an evidence list
in: a pointer to an EvidenceListType
return: a pointer to an EvidenceListType that has been initialized
*/
void initEvidenceList(EvidenceListType * evidenceList) {
evidenceList->head = NULL;
evidenceList->tail = NULL;
evidenceList->size = 0;
sem_t mutex;
sem_init(&mutex, 0, 1);
evidenceList -> mutex = mutex;
}
/*
Function: initEvidence
Purpose: initializes the evidence data with a random float in a range depending on the evidenceClassType
in: a double pointer to an EvidenceType
in: an evidenceClassType to tell what type of evidence it is
return: a pointer to an EvidenceType with initialized contents
*/
void initEvidence(EvidenceClassType evClass, EvidenceType ** evidence) {
free(*evidence);
// Allocate memory to the evidence
EvidenceType * tempEv = (EvidenceType*) calloc(1, sizeof(EvidenceType));
if (evClass == EMF) {
tempEv->value = randFloat(4.7, 5);
if (tempEv->value >= 4.9) {
tempEv->ghostliness = GHOSTLY;
} else {tempEv->ghostliness = NORMAL;}
} else if (evClass == TEMPERATURE) {
tempEv->value = randFloat(-10,1);
if (tempEv->value <= 0) {
tempEv->ghostliness = GHOSTLY;
} else {tempEv->ghostliness = NORMAL;}
} else if (evClass == FINGERPRINTS) {
tempEv->value = 1;
tempEv->ghostliness = GHOSTLY;
} else if (evClass == SOUND) {
tempEv->value = randInt(65,75);
if (tempEv->value >= 70) {
tempEv->ghostliness = GHOSTLY;
} else {tempEv->ghostliness = NORMAL;}
}
tempEv -> type = evClass;
*evidence = tempEv;
}
/*
Function: getRandEvidence
Purpose: gets a random evidence node from the evidence list
in: a double pointer to an evidenceList
in: an evidenceClassType to tell what type of evidence it is
return: a pointer to an EvidenceType chosen at random
*/
EvidenceType * getRandEvidence(EvidenceListType * evidenceList, EvidenceClassType evidenceClass) {
// Set the current evidence node to the head of the list
EvidenceNodeType * currEvidenceNode = evidenceList -> head;
// Loop through each element of the hunter's evidence linked list
while (currEvidenceNode != NULL && evidenceList -> size > 0){
// Retrieve the evidence type of the current node
if (currEvidenceNode -> evidenceData != NULL) {
EvidenceClassType currEvidenceType = currEvidenceNode -> evidenceData -> type;
// Compare against the evidence type you are trying to add
if (evidenceClass == currEvidenceType) {
EvidenceType * returnEvidence = currEvidenceNode -> evidenceData;
return returnEvidence;
}
}
// Set the current node to the next node
currEvidenceNode = currEvidenceNode -> next;
}
return NULL;
}
/*
Function: getEvidenceAtIndex
Purpose: gets the evidence data from a specific index
in: a pointer to an EvidenceListType
in: an integer that points to which index to retrieve the evidence from
return: an address to an EvidenceType at the specified index, or an error message
*/
EvidenceType * getEvidenceAtIndex(EvidenceListType * evidenceList, int index){
int evidenceListSize = evidenceList -> size; // Size of the evidence list
int i = 0; // Current index in the list
// If the index is valid:
if (index <= evidenceListSize && evidenceListSize > 0) {
// Make two temporary nodes to iterate through the linked list
struct EvidenceNodeType * currNode = evidenceList -> head;
// Keep looping till you find a matching index
while (currNode != NULL){
i++;
if (i == index) break;
currNode = currNode -> next;
}
if (i != index) printf("SOMETHING WENT WRONG WHILE SEARCHING FOR THE EVIDENCE!\n");
else return currNode -> evidenceData;
}
else printf("Invalid Index!\n");
return NULL;
}
/*
Function: removeEvidence
Purpose: removes evidence from the linked list
in: a pointer to the EvidenceListType
in: a pointer to the EvidenceType you want removed
return: a pointer to an EvidenceListType with a pointer to the evidence data stored inside of it
*/
void removeEvidence(EvidenceListType * evidenceList, EvidenceType * evidence){
EvidenceNodeType * currNode = evidenceList -> head;
EvidenceNodeType * temp;
// checks if head is null
if (currNode == NULL) {
} else { // else loops through and checks if the evidence matches
while (currNode -> next != NULL) {
if (currNode -> next -> evidenceData -> ghostliness == evidence -> ghostliness && currNode -> next -> evidenceData -> type == evidence -> type && currNode -> next -> evidenceData -> value == evidence -> value) {
evidenceList -> size--;
if (currNode -> next == evidenceList -> tail) {
cleanupEvidenceNode(currNode -> next);
currNode -> next = NULL;
evidenceList -> tail = currNode;
break;
}
temp = currNode -> next -> next;
cleanupEvidenceNode(currNode -> next);
currNode -> next = temp;
break;
}
currNode = currNode -> next;
}
}
}
/*
Function: addEvidence
Purpose: adds evidence to the linked list
in: a pointer to the EvidenceListType
in: a pointer to the EvidenceType you want stored
return: a pointer to an EvidenceListType with a pointer to the evidence data stored inside of it
*/
void addEvidence(EvidenceListType * list, EvidenceType * evidence){
//A temporary node to store the information of the new tail to be added
EvidenceNodeType * newNode = (EvidenceNodeType*)calloc(1, sizeof(EvidenceNodeType));
//The data of the new node is the evidence data
newNode -> evidenceData = evidence;
//The new tail does not have any nodes after it, so make sure to mark it as so
newNode -> next = NULL;
// If list is empty:
if (list -> head == NULL) {
list -> head = newNode;
list -> tail = newNode;
}
// If list is not empty
else {
list -> tail -> next = newNode;
list -> tail = newNode;
}
list -> size++;
}
/*
Function: cleanupEvidenceData
Purpose: cleans the data of the evidence node
in: a pointer to the EvidenceNodeType
return: a pointer to an EvidenceNodeType with no data inside of it
*/
void cleanupEvidenceData(EvidenceNodeType * evNode) {
free(evNode -> evidenceData);
}
/*
Function: cleanupEvidenceNode
Purpose: frees the evidence node from memory
in: a pointer to the EvidenceNodeType
return: free memory
*/
void cleanupEvidenceNode(EvidenceNodeType * evNode) {
free(evNode);
}
/*
Function: cleanupEvidenceList
Purpose: cleans up the entire evidence list
in: a pointer to the EvidenceListType
return: an empty evidence list
*/
void cleanupEvidenceList(EvidenceListType * evList) {
int evListSize = evList -> size;
// Make two temporary nodes to iterate through the linked list
EvidenceNodeType * currEv = evList -> head;
EvidenceNodeType * nextEv;
// Clean up every evidence in the evidence list
while (currEv != NULL){
nextEv = currEv -> next;
cleanupEvidenceData(currEv);
cleanupEvidenceNode(currEv);
currEv = nextEv;
}
free(evList);
}
/*
Function: cleanupEvidenceListNodes
Purpose: cleans up all the nodes in the evidence list
in: a pointer to the EvidenceListType
return: an evidence list with no nodes in it
*/
void cleanupEvidenceListNodes(EvidenceListType * evList) {
// Make two temporary nodes to iterate through the linked list
EvidenceNodeType * currEv = evList -> head;
EvidenceNodeType * nextEv;
// Clean up every evidence in the evidence list
for (int i = 0; i < evList -> size; i++){
nextEv = currEv -> next;
cleanupEvidenceNode(currEv);
currEv = nextEv;
}
free(evList);
}