// email_management_system.c // Tammy Zhong // Implementation file for email management system functions // (TODO) #include #include #include #include "email_management_system.h" // Function prototypes // Internal functions // Used by insertion operations struct email *create_email(char sender[MAX_LEN], char subject[MAX_LEN], double size, enum email_type type, int priority); // like create_node // Used by `delete_email_of_priority` for commented out approach // void delete_specific_email(struct folder *email_folder, char subject[MAX_LEN]); // Used by `split_folder` struct folder *insert_email_at_tail(struct folder *email_folder, char sender[MAX_LEN], char subject[MAX_LEN], double size, enum email_type type, int priority); // Function definitions // Creates a new empty email folder // Similar to previous lecture code create_node struct folder *create_folder(char name[MAX_LEN]) { // TODO: implement this function printf("create_folder not yet implemented.\n"); exit(1); } // Creates and initialises a new email // Internal function used by insertion operations // Similar to previous lecture code create_node struct email *create_email(char sender[MAX_LEN], char subject[MAX_LEN], double size, enum email_type type, int priority) { 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 = NULL; // important return new_email; } // Add a new email to the folder at the head // Similar to previous lecture code insert_head struct folder *insert_email_at_head(struct folder *email_folder, char sender[MAX_LEN], char subject[MAX_LEN], double size, enum email_type type, int priority) { // create email struct email *new_email = create_email(sender, subject, size, type, priority); // if there are no emails if (email_folder->emails == NULL) { email_folder->emails = new_email; return email_folder; } new_email->next = email_folder->emails; email_folder->emails = new_email; return email_folder; } // Prints out details of a single email void print_single_email(struct email *email) { if (email == NULL) { printf("Email is NULL.\n"); } 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"); } } // 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; } } // 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; } // Given a string, look for email where the subject is this string struct email *search_email(struct folder *email_folder, char subject[MAX_LEN]) { // TODO: implement this function printf("search_email not yet implemented.\n"); exit(1); } // Remove email(s) with certain priority (e.g. filter out low priority emails) void delete_email_of_priority(struct folder *email_folder, int priority) { // TODO: implement this function printf("delete_email_of_priority not yet implemented.\n"); exit(1); } // 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[MAX_LEN], struct folder *folder_1, struct folder *folder_2) { // TODO: implement this function printf("merge_folders not yet implemented.\n"); exit(1); } // 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); } // Inserts email at tail - Internal Function // Similar to previous lecture code on insert_at_tail (no need to write this one in lecture) struct folder *insert_email_at_tail(struct folder *email_folder, char sender[MAX_LEN], char subject[MAX_LEN], double size, enum email_type type, int priority) { struct email *new_email = create_email(sender, subject, size, type, priority); // empty list case if (email_folder->emails == NULL) { email_folder->emails = new_email; return email_folder; } struct email *current = email_folder->emails; // loop the current pointer until the last node while (current->next != NULL) { current = current->next; } // at this pt, current->next == NULL // which means current is pointing at the last node current->next = new_email; return email_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); } // Removes all emails from a folder // clears and delete associated memory of folder and emails in it void clear_folder(struct folder *email_folder) { // TODO: implement this function printf("clear_folder not yet implemented.\n"); exit(1); }