/* proj3start.c  P. Conrad CISC105  Fall 2005 */
/* starter file for project 3 */

#include <stdio.h>
#include <string.h> /* for strtok, strncpy */
#include <stdlib.h> /* for atof() */

#include "hiking.h"

#define MAXLINE 1024 /* maximum length of input line in file */

#define FILENAME_LEN 128  /* maximum length of filename */

/* function prototypes */

char readOneChar(void);
void printPrompt(void);
void readOneLineFromFile(struct Hiking_S *hikingData);

void printHelp(void);

/* Ask about this "const" in lecture */

void   printValues(const struct Hiking_S * const hikingData);


void      setValuesToEmptyStringAndZero(struct Hiking_S *hikingData);

/***************** MAIN PROGRAM **************/

int main(void)
{
  /* declare variables */

  struct Hiking_S hikingData;

  char c; /* menu option */
  
  /* main menu */

  printPrompt();
  c = readOneChar();
  while (c!='q' && c!='Q')  /* while user didn't select quit */
    {
      switch(c)
	{
	case 'h':
	case 'H':
	  printHelp();
	  break;

	case 'r':
	case 'R':
	  /* read one line from the file and store the result
	     into the character array hikingTrailName, and the
	     double value hikingTrailLengthInMiles */

	  readOneLineFromFile(&hikingData);
	  break;
	    
	case 'p':
	case 'P':
	  printValues(&hikingData);
	  break;
	    
	default:
	  printf("I don't understand the option: %c\n",c);
	  break;
	  
	}
      printPrompt();
      c = readOneChar();

    } /* end while */

  printf("Thanks for using this program.\n");
  printf("Bye now.\n");

}


void printHelp(void)
{
  printf("\n");
  printf(" h: help\n");
  printf(" p: print values\n");
  printf(" q: quit\n");
  printf(" r: read values\n");
  printf("\n");

}

void printPrompt(void)
{
  printf("Enter option (type h for help): ");
}

char readOneChar(void)
{
  char c;
  fflush(stdin);
  scanf("%c",&c);
  return c;
}

void      setValuesToEmptyStringAndZero(struct Hiking_S *hikingData)
{
  hikingData->name[0] = '\0';
  hikingData->location[0] = '\0';
  hikingData->lengthInMiles = 0.0;
  hikingData->state[0] = '\0';
  hikingData->numTimesHiked = 0;
}


void  readOneLineFromFile(struct Hiking_S *hikingData)

{
  
  char filename[FILENAME_LEN];
  char inputLine[MAXLINE];
  FILE *infile;

  char *result; /* for result of fgets */

  /* all of these pointers are for use with strtok(),
     to find the locations of each field on a line of input from the file 

     Lines in the example file hikingData.txt have the format: 

     South Kaibab,Grand Canyon NP,6.3,AZ,1
     
     (Note: change this comment to reflect YOUR example data file!)
  */

  char *namePtr;
  char *locationPtr;
  char *lengthPtr;
  char *statePtr;
  char *numTimesHikedPtr;

  printf("Please enter a filename: ");
  fflush(stdin);
  scanf("%s",&filename); /* &filename is redundant;ask about this in lecture!*/
  
  infile = fopen(filename, "r");

  /* check whether file opened properly */

  if (infile == NULL)
    {
      perror(filename);
      fprintf(stderr,"Can't open %s\n", filename);
      
      /* set the values to empty string, and zero */

      setValuesToEmptyStringAndZero(hikingData);

      fclose(infile); /* close the input file */
      return;
 
    }

  /* read one line from the file */

  result = fgets(inputLine, MAXLINE, infile);
  
  if (result == NULL)
    {
      fprintf(stderr,"Can't read data from %s\n", filename);
      
      /* set the values to empty string, and zero */

      setValuesToEmptyStringAndZero(hikingData);

      fclose(infile); /* close the input file */
      return;
    }
  
  /* divide up the string at the commas;
     requires #include <string.h>.  Ask about this in lecture */

  namePtr          = strtok(inputLine,",");
  locationPtr      = strtok(NULL,",");
  lengthPtr        = strtok(NULL,",");
  statePtr         = strtok(NULL,",");
  numTimesHikedPtr = strtok(NULL,",");

  /* store the results in the output parameters */

  strncpy(hikingData[0].name,    namePtr,    HIKING_TRAIL_NAME_LEN);
  strncpy(hikingData[0].location,locationPtr,HIKING_TRAIL_LOCATION_LEN);
  strncpy(hikingData[0].state,   statePtr,   HIKING_TRAIL_STATE_LEN);

  hikingData[0].lengthInMiles = atof(lengthPtr);
  hikingData[0].numTimesHiked = atoi(numTimesHikedPtr);

  printf("Data read successfully from file\n");

  /* close the input file */

  fclose(infile);

  return;
  
}




void   printValues(const struct Hiking_S * const hikingData)
{
  printf("\n");
  printf("  hiking trail name: %s\n",hikingData[0].name);
  printf("           location: %s\n",hikingData[0].location);
  printf("    length in miles: %6.1lf miles\n",hikingData[0].lengthInMiles);
  printf("              state: %s miles\n",hikingData[0].state);
  printf("              hiked: %d %s\n",hikingData[0].numTimesHiked,
	 (hikingData[0].numTimesHiked == 1) ? "time" : "times");
  printf("\n");
  
}





