#include <stdio.h>

/* 
 * This is a program to read a file containing a person's name, address, and
 * other info, and produce a set of mailing lists.  
 * 
 * The data line format is like this:
 * 
 *     Field\tData\n
 * 
 * Where `Field' is white-space-less, and `Data' may contain anything (up to
 * a newline.)  See the file `14.6.data'. 
 */

int main(int argc, char *argv[]) {
    FILE *data_file_ptr;     /* The file containing the label data. */
    char line[100];          /* A line read from the data file. */
    char *line_ptr;          /* Used with fgets to detect an EOF or error. */
    char good_file = 0;      /* 0 if the data file is invalid; otherwise 1. */
    char scan_count;         /* Number of matches with sscanf. */
    char keyword[50];        /* A keyword in the data file. */
    char value[50];          /* A value corresponding to a keyword. */
    char inside_record = 0;  /* 1 if we're parsing inside a record. */
    unsigned int labelId;    /* The label unique identifier. */
    unsigned int label_num;  /* Index into `for' loop. */

    /*
     * Each label lives inside a `label' structure.  Start out by allowing
     * enough space for 100 of them.
     */

    struct label_struct {
        char lastName[80];     
        char firstName[80];
        char address1[80];
        char address2[80];
        char phone[25];
        char mail[80];
        char url[100];
    } label[100];

    /* Open the data file. */

    data_file_ptr = fopen("14.6.data", "r");
    if (data_file_ptr == NULL) {
        fprintf(stderr, "%s:  Failed to open file %s for reading.\n",
            argv[0], "14.6.data");
        exit(8);
    }

    /* Read the records in, assigning them to `label_struct's. */

    while (1) {
        line_ptr = fgets(line, sizeof(line), data_file_ptr);       
        if (line_ptr == NULL) 
            break;
        if (strcmp(line, "Labels\tConfiguration file\n") == 0) {
            good_file = 1;  /* This is a good data file. */

#ifdef DEBUG
            printf("debug ==>  The data file is good.\n");
#endif   

        } else if (good_file == 1) {  /* The file is already known good. */
            scan_count = sscanf(line, "%s\t%[^\n]\n", &keyword, &value);
            if (scan_count != 2) {
                continue;  /* This must have been a blank line. */

#ifdef DEBUG
                printf("debug ==>  Blank line in data file.\n");
#endif

            }

            /*
             * I would really like to use a "switch" statement here, but 
             * it only works with integers, and not strings.  I'm sure 
             * there is a better way to do this, but for lack of knowing
             * a better way, Bob's your uncle. 
             */

            if (strcmp(keyword, "LabelID") == 0) {
                labelId = atoi(&value);  /* Index into `label' array. */
                continue;                /* Continue to the next line. */
            }
            if (strcmp(keyword, "LastName") == 0) {
                strcpy(label[labelId].lastName, value); 
                continue;                /* Continue to the next line. */
            }
            if (strcmp(keyword, "FirstName") == 0) {
                strcpy(label[labelId].firstName, value);
                continue;                /* Continue to the next line. */
            }     
            if (strcmp(keyword, "Address1") == 0) {
                strcpy(label[labelId].address1, value);
                continue;                /* Continue to the next line. */
            }     
            if (strcmp(keyword, "Address2") == 0) {
                strcpy(label[labelId].address2, value);
                continue;                /* Continue to the next line. */
            }     
            if (strcmp(keyword, "Phone") == 0) {
                strcpy(label[labelId].phone, value);
                continue;                /* Continue to the next line. */
            }     
            if (strcmp(keyword, "Mail") == 0) {
                strcpy(label[labelId].mail, value);
                continue;                /* Continue to the next line. */
            }     
            if (strcmp(keyword, "URL") == 0) {
                strcpy(label[labelId].url, value);
                continue;                /* Continue to the next line. */
            }     
        }        
    }
    fclose(data_file_ptr);

    /* Print out the formatted mailing labels. */

    for (label_num = 0; label_num <= labelId; ++label_num) { 
        printf("--------------------------------------------------\n");
        printf("%s, %s\n", label[label_num].lastName,
            label[label_num].firstName);
        printf("%s\n", label[label_num].address1);
        printf("%s\n", label[label_num].address2);
        printf("Mail:  %s\n", label[label_num].mail);
    }
    printf("--------------------------------------------------\n");
    return(0);
}