Week 9 Code Examples

// email_management_system.c
// Tammy Zhong
// Implementation file for email management system functions
// Modified by Angela Finlayson
// (TODO)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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, enum priority_type 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;
        return new_email;
}

// 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, 
    enum priority_type priority) {
        struct email *new_email = create_email(sender, subject, size, type, priority);
        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; //head of the list
    while (current != NULL && strcmp(current->subject, subject) != 0) {
        current = current->next;
    }
    return current;
}

void free_list(struct email *head) {
    struct email *current = head;
    while (current != NULL) {
        head = head->next;
        free(current); 
        current = head;
    }
}

// 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_list(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, enum priority_type priority) {
    struct email *prev = NULL;
    struct email *current = email_folder->emails;

    while (current != NULL) {
        if(current->priority == priority) {
            //delete this node
            if (prev == NULL) {
                email_folder->emails = email_folder->emails->next;
                free(current);
                current = email_folder->emails;
            } else {
                prev->next = current->next;
                free(current);
                current = prev->next;
            }
           
        } else {
            prev = 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) {
    // TODO: implement this function
    printf("merge_folders not yet implemented.\n");
    exit(1);
}

// Split emails in a given folder into 3 folders based on email type 
// keeping original ordering
// assumption: received, sent and draft are folders already malloc-ed and initialised
void split_folder(struct folder *given_folder, 
    struct folder *received, struct folder *sent, struct folder *draft) {
    // TODO: implement this function
    printf("split_folder not yet implemented.\n");
    exit(1);
}

// returns number of emails in a given folder 
// (EXTENSION 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;
}


// Your functions here (include function comments):


////////////////////////////////////////////////////////////////////////////////
//                              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");
    }
}
// main.c
// Angela Finlayson 
// Uses the email management system to allow a user to create a folder
// and and emails (with not content) to it.
// Only works for 1 folder... 
// (Provided)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "email_management_system.h"

void print_instructions(void);
void handle_search(struct folder *current_folder);
void handle_add_email(struct folder *current_folder);
void trim_newline(char str[]);

int main(void) {
    struct folder *current_folder = create_folder("inbox");
    char command;

    print_instructions();
    printf("\nEnter command: ");
    while (scanf(" %c", &command) == 1 && command != 'q') {
        if (command == 'a') {
            handle_add_email(current_folder);
        } else if (command == 'p') {
            print_emails(current_folder);
        } else if (command == 'n') { 
            printf("Number of emails: %d\n", count_emails(current_folder));
        } else if (command == 's') {
            handle_search(current_folder);
        } else {
            printf("Unknown command. Try again.\n");
        }
        printf("\nEnter command: ");
    }
    printf("Exiting\n");
    clear_folder(current_folder);
    return 0;
}

// Prints instructions to users
void print_instructions(void) {
    printf("Email Manager - Command Interface\n");
    printf("Commands:\n");
    printf("a - Add a new email to the folder\n");
    printf("p - Print all emails in the folder\n");
    printf("n - Count emails in the folder\n");
    printf("s - Search for an email by subject\n");
    printf("q - Quit\n");
}

// Handles adding a new email to the folder
// Assumes no error checking needed
void handle_add_email(struct folder *current_folder) {
    char sender[MAX_LEN] = "";
    char subject[MAX_LEN]= "";
    double size;
    int priority, type;

    // Remove whitespace from buffer
    int whitespace_ch;
    while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {}

    printf("Enter sender: ");
    fgets(sender, MAX_LEN, stdin);
    trim_newline(sender);
    printf("Enter subject: ");
    fgets(subject, MAX_LEN, stdin);
    trim_newline(subject);
    printf("Enter size (in MB): ");
    scanf("%lf", &size);
    printf("Enter email type (0 for DRAFT, 1 for RECEIVED, 2 for SENT): ");
    scanf("%d", &type);
    printf("Enter priority (0 for LOW, 1 for NORMAL, 3 for HIGH): ");
    scanf("%d", &priority);
    insert_email_at_head(current_folder, sender, subject, size, type, priority);
    printf("Email added to folder '%s'.\n", current_folder->name);
}

// Handles searching for an email by subject in the folder
void handle_search(struct folder *current_folder) {
    char subject[MAX_LEN];
    
    // Remove whitespace from buffer
    int whitespace_ch;
    while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {}

    printf("Enter subject to search: ");
    fgets(subject, MAX_LEN, stdin);
    trim_newline(subject);
    
    struct email *found_email = search_email(current_folder, subject);
    if (found_email != NULL) {
        printf("Email found:\n");
        print_single_email(found_email);
    } else {
        printf("No email with subject '%s' found.\n", subject);
    }
}

// removes trailing newline character
void trim_newline(char str[]) {
    int len = strlen(str);
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';  
    }
}
// main.c
// Tammy Zhong
// Program to test email management system functions
// Modified by Angela Finlayson 9/11/2025
// (Provided)


#include <stdio.h>
#include <stdlib.h>

#include "email_management_system.h"

int main(void) {
    // STAGE 1
    printf("*****1. Creating Folder 1...*****\n");
    struct folder *folder_1 = create_folder("Folder 1");

    printf("\n*****2. Number of emails in Empty Folder 1:*****\n");
    printf("%d\n", count_emails(folder_1));

    printf("\n*****3. Printing Empty Folder 1...*****\n");
    print_emails(folder_1);

    printf("\n*****4. Inserting emails in Empty Folder 1 *****\n");
 
    insert_email_at_head(folder_1, "Jon", "AAA", 10.2, RECEIVED, HIGH_PRIORITY);
    insert_email_at_head(folder_1, "Tim", "BBB", 20.44, DRAFT, NORMAL_PRIORITY);
    insert_email_at_head(folder_1, "Brianna", "CCC", 150.0, SENT, LOW_PRIORITY);
    insert_email_at_head(folder_1, "Tammy", "DDD", 90.2, SENT, NORMAL_PRIORITY);
    insert_email_at_head(folder_1, "Sofia", "EEE", 90.0, DRAFT, LOW_PRIORITY);

    printf("\n*****5. Printing Folder 1...*****\n");
    print_emails(folder_1); 

    printf("\n*****6. Number of emails in Folder 1: *****\n");
    printf("%d\n", count_emails(folder_1));
   
    
    printf("\n*****7. Look for email with subject 'AAA'...*****\n");
    struct email *email_found = search_email(folder_1, "AAA");
    
    printf("\n*****8. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);

    printf("\n*****9. Look for email with subject 'DDD'...*****\n");
    email_found = search_email(folder_1, "DDD");
    
    printf("\n*****10. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);

    printf("\n*****11. Look for email with subject 'EEE'...*****\n");
    email_found = search_email(folder_1, "EEE");
    
    printf("\n*****12. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);

    printf("\n*****13. Look for email with subject 'Holiday'...*****\n");
    email_found = search_email(folder_1, "Holiday");
    
    printf("\n*****14. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);
    

    printf("\n*****15. Creating Folder 2... and inserting emails...*****\n");
    struct folder *folder_2 = create_folder("Folder 2");
    insert_email_at_head(folder_2, "Liam", "E", 50.5, SENT, LOW_PRIORITY);
    insert_email_at_head(folder_2, "Sophie", "F", 5.97, RECEIVED, HIGH_PRIORITY);
    insert_email_at_head(folder_2, "Grace", "G", 30.28, SENT, NORMAL_PRIORITY);
    insert_email_at_head(folder_2, "Andrew", "H", 5.5, RECEIVED, LOW_PRIORITY);
    insert_email_at_head(folder_2, "Holly", "I", 25.25, RECEIVED, LOW_PRIORITY);
    insert_email_at_head(folder_2, "Ibby", "J", 15.25, RECEIVED, NORMAL_PRIORITY);
    insert_email_at_head(folder_2, "Bella", "K", 15.25, RECEIVED, LOW_PRIORITY);
    
    printf("\n*****16. Printing Folder 2...*****\n");
    print_emails(folder_2);
    
    //STAGE 2

    printf("\n*****17. Deleting LOW_PRIORITY emails from Folder 2...*****\n");
    delete_email_of_priority(folder_2, LOW_PRIORITY);
    
    printf("\n*****18. Printing Folder 2...*****\n");
    print_emails(folder_2);
  
    /*
    printf("\n*****19. Merging Folder 1 and Folder 2...*****\n");
    struct folder *merged_folder = merge_folders("Merged Folder", folder_1, 
                                                                  folder_2);
    
    printf("\n*****20. Printing merged folder...*****\n");
    print_emails(merged_folder);
   
    printf("\n*****21. Split merged folder into inbox, sent, draft...*****\n");
    struct folder *inbox = create_folder("inbox");
    struct folder *sent = create_folder("sent");
    struct folder *draft = create_folder("draft");
    split_folder(merged_folder, inbox, sent, draft);
    
    printf("\n*****22. Printing inbox...*****\n");
    print_emails(inbox);

    printf("\n*****23. Printing sent...*****\n");
    print_emails(sent);

    printf("\n*****24. Printing draft...*****\n");
    print_emails(draft);
    */
    
    // STAGE 1: comment this section out once you implement merge
    printf("\n*****Clearing stage 1 folders folder_1 and folder_2...*****\n");

    clear_folder(folder_1);
    clear_folder(folder_2);

    // STAGE 2 merge:  comment this section out once you implement split
    //printf("\n*****Clearing stage 2 merged_folder...*****\n");
    //clear_folder(merged_folder);

    // STAGE 2 
    //printf("\n*****Clearing inbox, sent, draft (freeing all memory)...*****\n");
    //clear_folder(inbox);
    //clear_folder(sent);
    //clear_folder(draft);
   
    
    return 0;
}