CrimsonCare
CrimsonCare is a C project designed to provide a robust solution for blood management.
 
Loading...
Searching...
No Matches
hospital_manager.c
Go to the documentation of this file.
1
32#include "../include/misc.h"
33
39
57void loadHospitals(void) {
58 errno = 0;
59 FILE* file = fopen("resources/db/hospitals.txt", "r");
60 if (!file) {
61 if (errno == ENOENT) {
62 return;
63 } else {
64 printf("Error opening hospitals file: %s\n", strerror(errno));
65 freeHospital();
66 return;
67 }
68 }
69
70 while (1) {
71 Hospital* newHospital = (Hospital*)malloc(sizeof(Hospital));
72 if (!newHospital) {
73 printf("Error allocating memory for hospital: %s\n", strerror(errno));
74 fclose(file);
75 freeHospital();
76 return;
77 }
78
79 if (fscanf(file, "%[^|]|%[^|]|%[^\n]\n", newHospital->code, newHospital->name, newHospital->location) != 3) {
80 free(newHospital);
81 fclose(file);
82 break;
83 }
84
85 newHospital->next = NULL;
86
87 if (hospitalHead == NULL) {
88 hospitalHead = newHospital;
89 } else {
90 Hospital* temp = hospitalHead;
91 while (temp->next != NULL) {
92 temp = temp->next;
93 }
94 temp->next = newHospital;
95 }
96 }
97
98 fclose(file);
99}
100
113void saveHospitals(void) {
114 errno = 0;
115 FILE* file = fopen("resources/db/hospitals.txt", "w");
116 if (!file) {
117 printf("Error opening hospitals file: %s\n", strerror(errno));
118 return;
119 }
120
121 Hospital* temp = hospitalHead;
122 while (temp != NULL) {
123 fprintf(file, "%s|%s|%s\n", temp->code, temp->name, temp->location);
124 temp = temp->next;
125 }
126 fclose(file);
127}
128
149char* addHospital(const char* name, const char* location) {
150 if (strcmp(name, "") == 0 || strcmp(location, "") == 0) {
151 printf("Error: Hospital name or location cannot be empty.\n");
152 return NULL;
153 }
154
155 if (containsPipe(name) || containsPipe(location)) {
156 printf("Error: Hospital name or location cannot contain a pipe character.\n");
157 return NULL;
158 }
159
160 char code[8];
161 char initials[4] = { 0 };
162 int initialCount = 0;
163
164 char nameCopy[100];
165 strncpy(nameCopy, name, sizeof(nameCopy) - 1);
166 nameCopy[sizeof(nameCopy) - 1] = '\0';
167
168 char* token = strtok(nameCopy, " ");
169 while (token != NULL && initialCount < 3) {
170 initials[initialCount++] = token[0];
171 token = strtok(NULL, " ");
172 }
173 initials[initialCount] = '\0';
174
175 bool codeExists;
176 int randomSuffix;
177 do {
178 srand(time(NULL));
179 randomSuffix = rand() % 10000;
180 snprintf(code, sizeof(code), "%s%04d", initials, randomSuffix);
181
182 codeExists = false;
183 Hospital* temp = hospitalHead;
184 while (temp != NULL) {
185 if (strcmp(temp->code, code) == 0) {
186 codeExists = true;
187 break;
188 }
189 temp = temp->next;
190 }
191 } while (codeExists);
192
193 Hospital* newHospital = (Hospital*)malloc(sizeof(Hospital));
194 if (!newHospital) {
195 printf("Error allocating memory for hospital: %s\n", strerror(errno));
196 return NULL;
197 }
198
199 strncpy(newHospital->code, code, sizeof(newHospital->code) - 1);
200 newHospital->code[sizeof(newHospital->code) - 1] = '\0';
201 strncpy(newHospital->name, name, sizeof(newHospital->name) - 1);
202 newHospital->name[sizeof(newHospital->name) - 1] = '\0';
203 strncpy(newHospital->location, location, sizeof(newHospital->location) - 1);
204 newHospital->location[sizeof(newHospital->location) - 1] = '\0';
205 newHospital->next = NULL;
206
207 if (hospitalHead == NULL) {
208 hospitalHead = newHospital;
209 } else {
210 Hospital* temp = hospitalHead;
211 while (temp->next != NULL) {
212 temp = temp->next;
213 }
214 temp->next = newHospital;
215 }
216
217 saveHospitals();
218 return newHospital->code;
219}
220
237bool validateHospitalCode(const char* code) {
238 if (strcmp(code, "") == 0) {
239 printf("Error: Hospital code cannot be empty.\n");
240 return false;
241 }
242
243 if (containsPipe(code)) {
244 printf("Error: Hospital code cannot contain a pipe character.\n");
245 return false;
246 }
247
248 Hospital* temp = hospitalHead;
249 while (temp != NULL) {
250 if (strcmp(temp->code, code) == 0) {
251 return true;
252 }
253 temp = temp->next;
254 }
255 return false;
256}
257
280bool deleteHospital(const char* code, const char* adminUsername, const char* adminPassword) {
281 if (strcmp(adminUsername, "") == 0 || strcmp(adminPassword, "") == 0) {
282 printf("Error: Admin credentials cannot be empty.\n");
283 return false;
284 }
285
286 if (!checkUsername(adminUsername)) {
287 printf("Error: Invalid admin username. Username can only contain lowercase letters and digits.\n");
288 return false;
289 }
290
291 if (!validateAdmin(adminUsername, adminPassword)) {
292 printf("Error: Invalid admin credentials.\n");
293 return false;
294 }
295
296 if (strcmp(code, "") == 0) {
297 printf("Error: Hospital code cannot be empty.\n");
298 return false;
299 }
300
301 if (containsPipe(code)) {
302 printf("Error: Hospital code cannot contain a pipe character.\n");
303 return false;
304 }
305
306 if (!validateHospitalCode(code)) {
307 printf("Error: Hospital code is invalid.\n");
308 return false;
309 }
310
311 Hospital* current = hospitalHead;
312 Hospital* prev = NULL;
313 while (current != NULL) {
314 if (strcmp(current->code, code) == 0) {
315 if (prev == NULL) {
316 hospitalHead = current->next;
317 } else {
318 prev->next = current->next;
319 }
320 saveHospitals();
321 return true;
322 }
323 prev = current;
324 current = current->next;
325 }
326 return false;
327}
328
345char* getHospitalNameByCode(const char* code) {
346 if (strcmp(code, "") == 0) {
347 printf("Error: Hospital code cannot be empty.\n");
348 return NULL;
349 }
350
351 if (containsPipe(code)) {
352 printf("Error: Hospital code cannot contain a pipe character.\n");
353 return NULL;
354 }
355
356 if (!validateHospitalCode(code)) {
357 printf("Error: Hospital code is invalid.\n");
358 return NULL;
359 }
360
361 Hospital* temp = hospitalHead;
362 while (temp != NULL) {
363 if (strcmp(temp->code, code) == 0) {
364 return temp->name;
365 }
366 temp = temp->next;
367 }
368 return NULL;
369}
370
379void displayHospitals(void) {
380 Hospital* temp = hospitalHead;
381 if (temp == NULL) {
382 printf("No hospitals registered yet.\n");
383 return;
384 }
385 printf("\nRegistered Hospitals:\n");
386 while (temp != NULL) {
387 printf("\tCode: %s\n"
388 "\tName: %s\n"
389 "\tLocation: %s\n", temp->code, temp->name, temp->location);
390 temp = temp->next;
391 if (temp != NULL) {
392 printf("\t----------------------------------\n");
393 }
394 }
395}
396
405void freeHospital(void) {
406 Hospital* current = hospitalHead;
407 while (current != NULL) {
408 Hospital* temp = current;
409 current = current->next;
410 free(temp);
411 }
412 hospitalHead = NULL;
413}
Hospital * hospitalHead
Hospital head pointer.
Hospital manager header file.
Misc header file.
Hospital structure.
struct Hospital * next
char code[MAX_HOSPITAL_CODE_LENGTH]
char location[MAX_HOSPITAL_LOCATION_LENGTH]
char name[MAX_HOSPITAL_NAME_LENGTH]