All pastes #815444 Raw Edit

Stuff

public c v1 · immutable
·published 2007-12-14 01:54 UTC
rendered paste body
/* * File:   main.c * Author: anf6 * Description: main() function and small miscellaneous ones * Created on 17 November 2007, 13:32 */#include <stdio.h> /* Include I/O functions from the standard library */#include <stdlib.h> /* Memory allocation */#include <ctype.h> /* Character handling functions, isspace() */#include "dllist.h"#include "formula1.h" /* My constants and function prototypes */int main(int argc, char** argv) {    int choice, numresults, read;    /* Create an initial node for drivers and teams */    Dllist teams = new_dllist();    Dllist drivers = new_dllist();        clearterm();        /* Get a filehandle and number of results user inputted with load_file_prompt()     * Then call parse(), saving the number of results read */    read = parse(load_file_prompt(&numresults), drivers, teams);    printf("** Read in %d results ** ", read);    /* I don't know why I need to ask the user how many results there are, so I've     * tried to make some use of this input... */    printf("This is%s how many you specified **\n", read == numresults ? "" : " NOT");        /* Print a menu */    do {        printf("\nYour choices are:\n");        printf("\t%d: Load another results file.\n", CHOICE_LOAD);        printf("\t%d: List drivers and their points (descending).\n", CHOICE_LISTDRIVERS_D);        printf("\t%d: List drivers and their points (ascending).\n", CHOICE_LISTDRIVERS_A);        printf("\t%d: List teams and their points (descending).\n", CHOICE_LISTTEAMS_D);        printf("\t%d: List teams and their points (ascending).\n", CHOICE_LISTTEAMS_A);        printf("\t%d: Quit.\n", CHOICE_QUIT);        printf("Choice: ");                while (!read_int(&choice));        switch (choice) {            case CHOICE_LOAD:                read = parse(load_file_prompt(&numresults), drivers, teams);                printf("** Read in %d results ** ", read);                printf("%s **\n", read == numresults ? "This is how many you specified." : "This is NOT how many you specified.");                break;            case CHOICE_LISTDRIVERS_D:                clearterm();                dll_traverse(drivers, 0, NULL, (*dll_listdrivers));                break;            case CHOICE_LISTDRIVERS_A:                clearterm();                dll_traverse(drivers, 1, NULL, (*dll_listdrivers));                break;            case CHOICE_LISTTEAMS_D:                clearterm();                dll_traverse(teams, 0, NULL, (*dll_listteams));                break;            case CHOICE_LISTTEAMS_A:                clearterm();                dll_traverse(teams, 1, NULL, (*dll_listteams));                break;        }    } while (choice != CHOICE_QUIT);        printf("\nBye!\n");    return(EXIT_SUCCESS);}/* For when things could potentially pear shaped */void fatal(char *msg) {    fprintf(stderr, msg);    exit(EXIT_FAILURE);}/* If built with -DWINDOWS, this will clear the screen with newline characters */void clearterm() {    printf(CLEARTERM_STRING);}/* Prompt for a file and open a filehandle (which is returned) * Also prompt for number of results in the file (as spec says) * and save this using the pointer provided. */FILE *load_file_prompt(int *numresults) {    FILE *file;    char *filename[MAX_FILE_LEN+1];    do {        printf("Enter filename to open: ");        scanf("%32s", &filename);    } while ((file = fopen(filename, "r")) == NULL);    printf("Enter number of results in this file: ");    while (!read_int(numresults));    return file;}/* Read an int from stdin and store. scanf() kept getting in infinite loops. * Pieced together from a chapter in K&R's ANSI C book. */int read_int(int *result) {    char c, buff[13];    return fgets(buff, sizeof buff, stdin) && !isspace(*buff) && sscanf(buff, "%d%c", result, &c) == 2 && (c == '\n' || c == '\0');}