// email_management_system.c // Tammy Zhong // Implementation file for email management system functions // Modified by Angela Finlayson // (TODO) #include #include #include #include "email_management_system.h" // Function prototypes // ADD YOUR OWN FUNCTION PROTOTYPES HERE struct email *create_email(char sender[], char subject[], double size, enum email_type type, int priority, struct email *next); void free_emails(struct email *head); // Function definitions // Creates a new empty email folder //(TODO) struct folder *create_folder(char name[]) { struct folder *new_folder = malloc(sizeof(struct folder)); strcpy(new_folder->name, name); new_folder->emails = NULL; return new_folder; } // Add a new email to the folder at the head // Similar to previous lecture code insert_head // (TODO) void insert_email_at_head(struct folder *email_folder, char sender[], char subject[], double size, enum email_type type, int priority) { struct email *new_email = create_email(sender, subject, size, type, priority, NULL); new_email->next = email_folder->emails; email_folder->emails = new_email; } // Given a string, look for email where the subject is this string // (TODO) struct email *search_email(struct folder *email_folder, char subject[]) { struct email *current = email_folder->emails; while (current != NULL) { if (strcmp(current->subject, subject) == 0) { return current; } current = current->next; } return NULL; } // Removes all emails from a folder // clears and delete associated memory of folder and emails in it void clear_folder(struct folder *email_folder) { free_emails(email_folder->emails); free(email_folder); } // Remove email(s) with certain priority (e.g. filter out low priority emails) void delete_email_of_priority(struct folder *email_folder, int priority) { struct email *previous = NULL; struct email *current = email_folder->emails; while (current != NULL) { if (current->priority == priority) { if (previous == NULL) { email_folder->emails = email_folder->emails->next; free(current); current = email_folder->emails; } else { previous->next = current->next; free(current); current = previous->next; } } else { previous = current; current = current->next; } } } // Append emails from folder_2 after folder_1 and returns them in the one folder // Assumption made here assumes not same subject email in folder_1 and folder_2 struct folder *merge_folders(char merged_folder_name[], struct folder *folder_1, struct folder *folder_2) { struct folder *new_folder = create_folder(merged_folder_name); if (folder_1->emails == NULL) { new_folder->emails = folder_2->emails; } else { //Find the last node of the first list struct email *current = folder_1->emails; while (current->next != NULL) { current = current->next; } current->next = folder_2->emails; new_folder->emails = folder_1->emails; } free(folder_1); free(folder_2); return new_folder; } // Split emails in a given folder into 3 folders based on email type // keeping original ordering // assumption: inbox, sent and draft are folders already malloc-ed and initialised void split_folder(struct folder *given_folder, struct folder *inbox, struct folder *sent, struct folder *draft) { // TODO: implement this function printf("split_folder not yet implemented.\n"); exit(1); } // CHALLENGE Returns number of emails in a given folder // (TODO: Improve this) int count_emails(struct folder *email_folder) { int counter = 0; struct email *current = email_folder->emails; while (current != NULL) { counter++; current = current->next; } return counter; } // CHALLENGE at home for fun: Sort emails in folder based on size // (slightly out of scope of COMP1511 - touches on sorting algorithms) // order doesn't matter if two emails of same size void sort_emails(struct folder *email_folder) { // TODO: implement this function printf("sort_emails not yet implemented.\n"); exit(1); } // Your functions here (include function comments): // struct email *create_email(char sender[], char subject[], double size, enum email_type type, int priority, struct email *next) { struct email *new_email = malloc(sizeof(struct email)); strcpy(new_email->sender, sender); strcpy(new_email->subject, subject); new_email->size = size; new_email->type = type; new_email->priority = priority; new_email->next = next; return new_email; } void free_emails(struct email *head) { struct email *current = head; while (current != NULL) { head = head->next; free(current); current = head; } } //////////////////////////////////////////////////////////////////////////////// // Provided Code // //////////////////////////////////////////////////////////////////////////////// // Print/display all emails in folder // Similar to previous lecture code print_list void print_emails(struct folder *email_folder) { printf("Folder Name: %s\n", email_folder->name); struct email *current = email_folder->emails; if (current == NULL) { printf("There are no emails in this folder.\n"); } while (current != NULL) { printf("---\n"); print_single_email(current); current = current->next; } } // Prints out details of a single email void print_single_email(struct email *email) { if (email == NULL) { printf("Email is NULL.\n"); return; } printf("Sender: %s\n", email->sender); printf("Subject: %s\n", email->subject); printf("Size: %lfmb\n", email->size); printf("Type: "); if (email->type == RECEIVED) { printf("RECEIVED\n"); } else if (email->type == SENT) { printf("SENT\n"); } else if (email->type == DRAFT) { printf("DRAFT\n"); } else { printf("UNKNOWN\n"); } printf("Priority: "); if (email->priority == HIGH_PRIORITY) { printf("HIGH_PRIORITY\n"); } else if (email->priority == NORMAL_PRIORITY) { printf("NORMAL_PRIORITY\n"); } else if (email->priority == LOW_PRIORITY) { printf("LOW_PRIORITY\n"); } else { printf("UNKNOWN\n"); } }