-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfauna.h
240 lines (213 loc) · 7.12 KB
/
fauna.h
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
#define FAUNA_DATABASE "fauna.csv"
#define FAUNA_PLANTS_ARRAY_SIZE 100
#define STR_LENGTH 200
/*
Function declarations for accessing and modifying fauna database
*/
void fauna_database_and_matching(struct matched_flora *matched_flora);
void read_fauna_database();
void printFaunaArray();
char *endanger_name (enum red_list_categories endangerlvl);
void read_plants (struct fauna *fauna, char *line);
void fauna_matching (struct matched_flora *matched_flora);
struct matched_flora add_fauna_to_matched_flora(char* faunaLatinName, struct matched_flora matched_flora);
void get_plant_family_name (const char* latinName, char familyName[MAX_NAME_LENGTH]);
void print_matched_flora(struct matched_flora *matched_flora);
void fauna_database_and_matching(struct matched_flora *matched_flora) {
int i;
struct fauna emptyFauna = {"", "", 0};
/* Empties fauna array */
for (i = 0; i < HASH_ARRAY_SIZE; i++) {
fauna[i] = emptyFauna;
}
read_fauna_database();
if(DEBUG){
printf("\n[Fauna database]\n");
printFaunaArray();
}
fauna_matching(matched_flora);
if(DEBUG){
printf("\n[Matched flora and fauna]\n");
print_matched_flora(matched_flora);
}
}
void read_fauna_database() {
char line[STR_LENGTH];
int hashName;
char* latinName;
struct fauna read_fauna;
int danger_category, i;
FILE *fauna_ptr = fopen(FAUNA_DATABASE, "r");
if (fauna_ptr != NULL) {
/* We validate the first line, containing headers of the file */
fgets(line, STR_LENGTH, fauna_ptr);
if (strncmp(line, "Dansk navn,Latinsk navn,Truet,Nytteplante 1,Nytteplante 2,Nytteplante 3,Nytteplante 4,Nytteplante 5,Nytteplante 6", 113) != 0) {
printf("Headers in fauna.csv file are incorrect!\n");
exit(EXIT_FAILURE);
}
/*get lines until at the end of file*/
while (fgets(line, STR_LENGTH, fauna_ptr) != NULL) {
sscanf(line, " %[^,] , %[^,] , %i ", read_fauna.danishName, read_fauna.latinName, &danger_category);
read_fauna.endangerlvl = (enum red_list_categories)danger_category;
/*allocate space for strings and set them to be empty*/
for (i = 0; i < FAUNA_PLANTS_ARRAY_SIZE; i++) {
if ((read_fauna.plants[i] = (char *)malloc(MAX_NAME_LENGTH*sizeof(char))) == NULL) {
printf("Malloc fail");
exit(EXIT_FAILURE);
}
strcpy(read_fauna.plants[i], "");
}
read_plants(&read_fauna, line);
to_upper(read_fauna.latinName);
latinName = read_fauna.latinName;
hashName = hash(latinName);
fauna[hashName] = read_fauna;
}
}
else {
printf("Error! Can't open file\n");
}
}
void read_plants(struct fauna *fauna, char *line) {
int start_point = (int)strlen(fauna->danishName) + (int)strlen(fauna->latinName) + 4;
int i = 0;
int j = start_point;
/*runs until there are no more names on the line*/
while (line[j] != '\0') {
/*runs until the plant's name has been found*/
while (line[j] != ',' && line[j] != '\0' && line[j] != '\n' && j < STR_LENGTH) {
/*special case to fix a very wierd bug*/
if (strcmp(fauna->danishName, "Murergnavebi") == 0) {
fauna->plants[i][j - start_point + 14] = '\0';
}
fauna->plants[i][j - start_point] = line[j];
j++;
}
fauna->plants[i][j] = '\0';
to_upper(fauna->plants[i]);
j++;
start_point = j;
i++;
}
}
/*Debug option to print the fauna array*/
void printFaunaArray() {
int i, j = 0;
for (i = 0; i < HASH_ARRAY_SIZE; i++) {
if (strcmp(fauna[i].latinName, "") != 0) {
printf("%-40s | %-40s | %2s |",
fauna[i].danishName, fauna[i].latinName, endanger_name(fauna[i].endangerlvl));
while (strcmp(fauna[i].plants[j], "") != 0) {
printf(" %-20s |", fauna[i].plants[j]);
j++;
}
printf("\n");
j = 0;
}
}
}
/*returns the name of the Enum value*/
char *endanger_name (enum red_list_categories endangerlvl) {
switch (endangerlvl) {
case RE:
return "RE";
case CR:
return "CR";
case EN:
return "EN";
case VU:
return "VU";
case NT:
return "NT";
case DD:
return "DD";
case LC:
return "LC";
case NA:
return "NA";
case NE:
return "NE";
default:
return "Error";
}
}
void fauna_matching (struct matched_flora *matched_flora) {
int i, j, k;
char plantFamilyName[MAX_NAME_LENGTH];
/* Set matched_flora.fauna to all empty strings */
for (i = 0; i < MAX_NUMBER_OF_MATCHES; i++) {
for (j = 0; j < MAX_FLORA_PER_FAUNA; j++) {
strcpy(matched_flora[i].matchedFaunaLatinName[j], "");
}
}
/* For each fauna */
for (i = 0; i < HASH_ARRAY_SIZE; i++) {
if (strcmp(fauna[i].latinName, "") != 0) {
/* For each flora in fauna */
j = 0;
while (strcmp(fauna[i].plants[j], "") != 0) {
/* For each matched_flora */
k = 0;
while (strcmp(matched_flora[k].floraLatinName, "") != 0 && k < MAX_FLORA_PER_FAUNA) {
get_plant_family_name(matched_flora[k].floraLatinName, plantFamilyName);
/* If match between matched_flora.floraLatinName and fauna.plants[j] */
if (strcmp(matched_flora[k].floraLatinName, fauna[i].plants[j]) == 0) {
/* Add fauna.latinName[j] to matched_flora.matchedFaunaLatinName */
matched_flora[k] = add_fauna_to_matched_flora(fauna[i].latinName, matched_flora[k]);
}
/* Else if match between get_plant_family_name(matched_flora.floraLatinName) and fauna.plants[j] */
else if (strcmp(plantFamilyName, fauna[i].plants[j]) == 0) {
/* Add fauna.latinName[j] to matched_flora.matchedFaunaLatinName */
matched_flora[k] = add_fauna_to_matched_flora(fauna[i].latinName, matched_flora[k]);
}
k++;
}
j++;
}
}
}
}
struct matched_flora add_fauna_to_matched_flora(char* faunaLatinName, struct matched_flora matched_flora) {
int i = 0;
/*continues until a empty string is found*/
while (i <= MAX_FLORA_PER_FAUNA && (strcmp(matched_flora.matchedFaunaLatinName[i], "") != 0)) {
i++;
}
if (i == MAX_FLORA_PER_FAUNA) {
printf("Error: matched_flora out of memorry for fauna \n");
exit(EXIT_FAILURE);
}
strcpy(matched_flora.matchedFaunaLatinName[i], faunaLatinName);
return matched_flora;
}
/*gets the family name of a plants latin name*/
void get_plant_family_name (const char* latinName, char* familyName) {
int i, split;
int size = strlen(latinName);
split = size;
for (i = 0; i < size; i++) {
/*stop if a space is found*/
if (latinName[i] == ' ') {
split = i;
break;
}
}
strncpy(familyName, latinName, split);
familyName[split] = '\0';
}
/*Debug option for printing the matched flora and fauna*/
void print_matched_flora(struct matched_flora *matched_flora) {
int i, j;
for (i = 0; i < MAX_NUMBER_OF_MATCHES; i++) {
if (strcmp(matched_flora[i].floraLatinName, "") != 0) {
printf("%-40s", matched_flora[i].floraLatinName);
j = 0;
while (strcmp(matched_flora[i].matchedFaunaLatinName[j], "") != 0 && j < MAX_FLORA_PER_FAUNA) {
printf(" | %-40s", matched_flora[i].matchedFaunaLatinName[j]);
j++;
}
printf("\n");
}
}
printf("\n");
}