/* * File: * tdli.c * Author: * Lindsey Kuper * Summary: * A simple interactive interface for a to-do list. */ #include "tdli.h" list myList; int main (void) { char input[128]; char option; myList = (list)newList(); /* Print a friendly menu. */ printf ("Welcome to your to-do list interface!\n"); while (1) { printf ("\nMenu Options\n"); printf (" P - Print entire to-do list\n"); printf (" H - Print highest-priority task\n"); printf (" S - Save to-do list to a file\n"); printf (" R - Read to-do list from a file\n"); printf (" N - Show next task on list\n"); printf (" A - Add a task to the list\n"); printf (" D - Remove all tasks from the list\n"); printf (" Q - Quit\n"); printf ("Enter desired option: "); scanf (" %c", &option); switch (option) { case 'p': case 'P': if (printAll(myList)) printf("Printing completed\n\n"); else printf("Error printing list\n\n"); break; case 'h': case 'H': if (printHighestPriority(myList)) printf("Printing completed\n\n"); else printf("Error printing highest-priority task\n\n"); break; case 's': case 'S': if (saveToFile(myList)) printf("List saved\n\n"); else printf("Error saving list\n\n"); break; case 'r': case 'R': if (readFromFile(myList)) printf("Finished reading from file\n\n"); else printf("Error reading from file\n\n"); break; case 'n': case 'N': if (printNext(myList)) printf("Printing completed\n\n"); else printf("Error printing list item"); break; case 'a': case 'A': if (addTask()) printf("Task added to list\n\n"); else printf("Error adding task to list"); break; case 'd': case 'D': if (removeAll(myList)) printf("All tasks removed\n\n"); else printf("Error removing all tasks"); break; case 'q': case 'Q': printf ("Program terminated successfully\n"); exit(EXIT_SUCCESS); break; default: printf("Invalid option\n\n"); } } return EXIT_SUCCESS; } int printAll(list ls) { /* Check that the list exists and is not empty. */ if ((!ls) || (!(ls->current)) || (!(ls->current->contents))) return 0; /* Move cursor to the beginning. */ moveCursorToHead(ls); /* Print first task. */ printTask(getCurrentContents(ls)); /* Advance the cursor. */ advance(ls); /* Continue through rest of list. */ while (ls->current != ls->head) { printTask(getCurrentContents(ls)); advance(ls); } return 1; } /* No time to implement this one. Oops. */ int printHighestPriority(list ls) {} int saveToFile(list ls) { char outfilename[128]; FILE *outputFile; /* Ask the user what file to write to. */ printf("Enter output file name: "); scanf("%s", &outfilename); /* Check that the file is open for writing; create it if necessary. */ if (!(outputFile = fopen(outfilename, "w+"))) return 0; /* Check that the list exists and is not empty. */ if ((!ls) || (!ls->current)) return 0; /* Cursor to beginning. */ moveCursorToHead(ls); /* Print first task to file. */ fprintf(outputFile, "Name: %s\nDeadline: %sPriority: %d\n", ((task *)ls->current->contents)->taskname, ctime(&((task *)ls->current->contents)->deadline), ((task *)ls->current->contents)->priority); /* Advance the cursor. */ advance(ls); /* Continue through rest of list. */ while(ls->current != ls->head) { fprintf(outputFile, "Name: %s\nDeadline: %sPriority: %d\n", ((task *)ls->current->contents)->taskname, ctime(&((task *)ls->current->contents)->deadline), ((task *)ls->current->contents)->priority); advance(ls); } return 1; } int readFromFile(list ls) { char infilename[128]; FILE *inputFile; int ch; char taskname[128]; task *new; int tmpmonth; int tmpday; int tmpyear; struct tm dlstruct; time_t deadline; int priority; /* Find out which file to read from. */ printf("Enter input file name: "); scanf("%s", &infilename); /* Check that the file is open for reading. */ if (!(inputFile = fopen(infilename, "r"))) return 0; /* While fscanf works... */ while ((fscanf(inputFile, "%s\t%d\t%d\t%d\t%d", taskname, &priority, &tmpmonth, &tmpday, &tmpyear)) == 5) { /* Initialize the time structure (Daren's idea) */ memset(&dlstruct, '\0', sizeof(struct tm)); /* tm_mon stores month of the year (from 0) */ dlstruct.tm_mon = (tmpmonth - 1); /* tm_mday stores day of the month (from 1) */ dlstruct.tm_mday = tmpday; /* tm_year stores years since 1900 */ dlstruct.tm_year = (tmpyear - 1900); /* mktime returns the completed time_t */ deadline = mktime(&dlstruct); /* Create a new task. */ new = makeNewTask(taskname, deadline, priority); /* Add it to the list. */ if (!(addAfterCurrent(myList, new))) return 0; } /* Close the file on our way out. */ return (fclose(inputFile) == EOF); } int printNext(list ls) { /* Does this really need inline comments? */ printTask(getCurrentContents(ls)); advance(ls); } int addTask() { char taskname[128]; task *new; int tmpmonth; int tmpday; int tmpyear; struct tm dlstruct; time_t deadline; int priority; /* Get task info from the user. */ printf("Enter task name: "); scanf("%s", &taskname); printf("Enter task priority: "); scanf("%d", &priority); printf("Enter deadline (MM.DD.YYYY): "); scanf("%d.%d.%d", &tmpmonth, &tmpday, &tmpyear); /* tm_mon stores month of the year (from 0) */ dlstruct.tm_mon = (tmpmonth - 1); /* tm_mday stores day of the month (from 1) */ dlstruct.tm_mday = tmpday; /* tm_year stores years since 1900 */ dlstruct.tm_year = (tmpyear - 1900); /* Our time_t object is ready to go. */ deadline = mktime(&dlstruct); /* Create the new task. */ new = makeNewTask(taskname, deadline, priority); /* Add it to the list on our way out. */ return (addAfterCurrent(myList, new)); } int removeAll(list ls) { /* Check that the list exists and is not empty. */ if ((!ls) || (!ls->current)) return 0; /* While there are still elements in the list... */ while (advance(ls)) { /* Delete them. */ if (!(deleteCurrent(ls))) return 0; } return 1; }