57void loadHospitals(
void) {
59 FILE* file = fopen(
"resources/db/hospitals.txt",
"r");
61 if (errno == ENOENT) {
64 printf(
"Error opening hospitals file: %s\n", strerror(errno));
73 printf(
"Error allocating memory for hospital: %s\n", strerror(errno));
79 if (fscanf(file,
"%[^|]|%[^|]|%[^\n]\n", newHospital->
code, newHospital->
name, newHospital->
location) != 3) {
85 newHospital->
next = NULL;
91 while (temp->
next != NULL) {
94 temp->
next = newHospital;
113void saveHospitals(
void) {
115 FILE* file = fopen(
"resources/db/hospitals.txt",
"w");
117 printf(
"Error opening hospitals file: %s\n", strerror(errno));
122 while (temp != NULL) {
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");
155 if (containsPipe(name) || containsPipe(location)) {
156 printf(
"Error: Hospital name or location cannot contain a pipe character.\n");
161 char initials[4] = { 0 };
162 int initialCount = 0;
165 strncpy(nameCopy, name,
sizeof(nameCopy) - 1);
166 nameCopy[
sizeof(nameCopy) - 1] =
'\0';
168 char* token = strtok(nameCopy,
" ");
169 while (token != NULL && initialCount < 3) {
170 initials[initialCount++] = token[0];
171 token = strtok(NULL,
" ");
173 initials[initialCount] =
'\0';
179 randomSuffix = rand() % 10000;
180 snprintf(code,
sizeof(code),
"%s%04d", initials, randomSuffix);
184 while (temp != NULL) {
185 if (strcmp(temp->
code, code) == 0) {
191 }
while (codeExists);
195 printf(
"Error allocating memory for hospital: %s\n", strerror(errno));
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);
205 newHospital->
next = NULL;
211 while (temp->
next != NULL) {
214 temp->
next = newHospital;
218 return newHospital->
code;
237bool validateHospitalCode(
const char* code) {
238 if (strcmp(code,
"") == 0) {
239 printf(
"Error: Hospital code cannot be empty.\n");
243 if (containsPipe(code)) {
244 printf(
"Error: Hospital code cannot contain a pipe character.\n");
249 while (temp != NULL) {
250 if (strcmp(temp->
code, code) == 0) {
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");
286 if (!checkUsername(adminUsername)) {
287 printf(
"Error: Invalid admin username. Username can only contain lowercase letters and digits.\n");
291 if (!validateAdmin(adminUsername, adminPassword)) {
292 printf(
"Error: Invalid admin credentials.\n");
296 if (strcmp(code,
"") == 0) {
297 printf(
"Error: Hospital code cannot be empty.\n");
301 if (containsPipe(code)) {
302 printf(
"Error: Hospital code cannot contain a pipe character.\n");
306 if (!validateHospitalCode(code)) {
307 printf(
"Error: Hospital code is invalid.\n");
313 while (current != NULL) {
314 if (strcmp(current->
code, code) == 0) {
324 current = current->
next;
345char* getHospitalNameByCode(
const char* code) {
346 if (strcmp(code,
"") == 0) {
347 printf(
"Error: Hospital code cannot be empty.\n");
351 if (containsPipe(code)) {
352 printf(
"Error: Hospital code cannot contain a pipe character.\n");
356 if (!validateHospitalCode(code)) {
357 printf(
"Error: Hospital code is invalid.\n");
362 while (temp != NULL) {
363 if (strcmp(temp->
code, code) == 0) {
379void displayHospitals(
void) {
382 printf(
"No hospitals registered yet.\n");
385 printf(
"\nRegistered Hospitals:\n");
386 while (temp != NULL) {
387 printf(
"\tCode: %s\n"
392 printf(
"\t----------------------------------\n");
405void freeHospital(
void) {
407 while (current != NULL) {
409 current = current->
next;
Hospital * hospitalHead
Hospital head pointer.
Hospital manager header file.
char code[MAX_HOSPITAL_CODE_LENGTH]
char location[MAX_HOSPITAL_LOCATION_LENGTH]
char name[MAX_HOSPITAL_NAME_LENGTH]