/* This program performs a variety of processing tasks for a file about 
   cities*/

#include <stdio.h>

#define MAX 45
#define TEXT_FILE "/home/walker/151p/labs/lab26.dat"

struct CityData {
  char name[MAX];
  char county[MAX];
  char state[MAX];
  int year;
  int pop;
  int area;
  int phone;
  int radio;
  int tv;
};

void readLine(FILE *cityFile, char line[MAX]);
/* This procedures reads a line from the given file */

void displayCities();
/* This procedure displays the city data from a text file on the screen */

void readCity(FILE * cityFile, struct CityData *city);
/* This procerdure reads a single city's worth of data from the file */

void printCity(struct CityData city);
/* This procedure prints information on a given city */

void incorporated();
/* This procedure prints the names of all cities incorporated before 1843*/ 

void media();
/* This procedure computes the city with the lowest per capita number
   of radio and TV stations*/

void populationDensity();
/*This procedure scans population density */
  
void telephones();
/*This procedure displays cities with over 1,000,000 telephones */

int main(void) {
  char option;
  printf("This program processes data on cities in various ways");

  while (1) {
    printf("\n");;
    printf("Menu Options\n");
    printf("    D - Display data from Text File\n");
    printf("    I - Find cities incorporated 150 years ago\n");
    printf("    M - Find city with fewest radio and TV stations\n");
    printf("    P - Find cities with highest population density\n");
    printf("    Q - Quit\n");
    printf("    T - Find cities with over 1,000,000 telephones\n");
    printf("Enter desired option:  ");
    scanf(" %c", &option);
    switch (option) 
      { case 'd':
        case 'D':  
          displayCities();
          break;
        case 'i':
        case 'I': 
          incorporated();
          break;
        case 'm':
        case 'M': 
          media();
          break;
        case 'p':
        case 'P': 
          populationDensity();
          break;
        case 'q':
        case 'Q': 
          printf("Program terminated\n");
          return ;
          break;
        case 't':
        case 'T': 
          telephones();
          break;
      default:  printf("Invalid Option\n");
      }
  }
  return 0;
}

void displayCities() {
  /* This procedure displays the city data from a text file on the screen */
  FILE *cityFile;
  struct CityData city;
    
  printf("\nThe city data are shown below: \n\n");
  cityFile = fopen(TEXT_FILE, "r");
  city.name[0] = ' ';

  while(city.name[0] != EOF)  {
    readCity(cityFile, &city);
    printCity(city);
  }
  fclose(cityFile);
}

void readLine(FILE *cityFile, char line[MAX]) {
/* This procedures reads a line from the given file */
  int i = 0;
  if (fscanf(cityFile, "%c", &line[i]) == EOF) {
    line[0] = EOF;
    return;
  }
    
  while (line[i] != '\n') {
    i++;
    fscanf(cityFile, "%c", &line[i]);
  }
  line[i] = 0;
}

void readCity(FILE * cityFile, struct CityData *city) {
  /* This procedure reads one city record from a file */
  char ch;
  readLine(cityFile, (*city).name);
  readLine(cityFile, (*city).county);
  readLine(cityFile, (*city).state);
  if ((*city).state[0] != EOF) {
    fscanf(cityFile,"%d", &(*city).year);
    fscanf(cityFile,"%d", &(*city).pop);
    fscanf(cityFile,"%d", &(*city).area);
    fscanf(cityFile,"%d", &(*city).phone);
    fscanf(cityFile,"%d", &(*city).radio);
    fscanf(cityFile,"%d", &(*city).tv);

    /* finish reading tv line */
    fscanf(cityFile, "%c", &ch);
    while (ch != '\n') 
      fscanf(cityFile, "%c", &ch);

    /* read blank line at end of city */
    fscanf(cityFile, "%c", &ch);
    while (ch != '\n') 
      fscanf(cityFile, "%c", &ch);
  }
}

void printCity(struct CityData city) {
  /* This procedure prints information on a given city */
  printf("%s, %s\n", city.name, city.state);
  printf("     County:  %s\n\n", city.county);
  printf("     Year                                      ");
  printf("    Area     Radio      TV\n");
  printf("     Incorporated     Population     Telephones");
  printf("   (km)   Stations  Stations\n");
  printf("%13d %16d %14d %9d %7d %9d\n\n", 
          city.year, city.pop, city.phone, city.area, city.radio, city.tv);
}

void incorporated() {
  /* This procedure prints the names of all cities incorporated before 1843*/ 
    printf("\nThe following cities were incorporated before 1843\n\n");

    printf("     Option not yet implemented\n");
}

void media() { 
  /* This procedure computes the city with the lowest per capita number
     of radio and TV stations*/
    printf("\n   Processing Radio and TV Data\n\n");

    printf("     Option not yet implemented\n");
}

void populationDensity() {
  /*This procedure scans population density */
    printf("\n   Processing cities by population density\n\n");

    printf("     Option not yet implemented\n");
}
   
void telephones() {
  /*This procedure displays cities with over 1,000,000 telephones */
    printf("\n   The following cities have over 1,000,000 telephones:\n\n");

    printf("     Option not yet implemented\n");
}
