// readWorkoutData.cc P. Conrad 02/24/06
// CISC181.. a program to read a CSV file with only two items per line

// CSV stands for: Comma Separated Values
//  e.g. Microsoft Excel, WebCT, and a zillion other programs
//   can generate and read CSV files


#include <iostream>
#include <fstream>
#include <cstring> // for strtok
#include <cstdlib>
using namespace std;


#include "computeSplitTime.h"


#define INPUT_FILE "workoutData.dat"

// fields in the input file are:

// date, 
// distance, 
// split time at 500 meters, 
// total minutes in workout, 
// personal record

// Format of the input file is:

// mm/dd/yy,Meters,m:ss.s,minutes,yes_or_no
// mm/dd/yy,Meters,m:ss.s,minutes,yes_or_no
// mm/dd/yy,Meters,m:ss.s,minutes,yes_or_no
// etc...



const int DATE_LEN=9;

struct Workout_S
{
  char date[DATE_LEN];
  int meters;
  double splitTimeSeconds; // store the field in pure seconds
  int minutes;
  bool personalRecord;
};


// instead of #define MAX_LINE_LENGTH 1024
const int MAX_LINE_LENGTH = 1024;

const int MAX_NUM_WORKOUTS = 200;


int main(void)
{
  // an array of 10 integers would be int x[10];
  // so an array of 10 "Workout_S" structs is Workout_S x[10];
  
  // what we want is a a large array of Workout_S records.

  Workout_S workout[MAX_NUM_WORKOUTS];
  

  ifstream  infile (INPUT_FILE, ios::in);
  
  if (!infile)
    {
      cerr << "The file " << INPUT_FILE <<  " did not open" << endl;
      return 1;
    }

  char inputFileLine[MAX_LINE_LENGTH];

  // try to read an entire line of text   
  // In C, we did:  fgets(infile,MAX_LINE_LENGTH,inputFileLine);
  // In C++, the equivalent is:

  int count=0;

  infile.getline(inputFileLine, MAX_LINE_LENGTH);
  while (!infile.eof()  ) 
    {
      // process the line of data we read

      char *datePtr;
      char *metersPtr;
      char *splitTimePtr;
      char *minutesPtr;
      char *persRecPtr;

      // we use the "string tokenize" function, strtok
      // we need to use #include <cstring>

      datePtr = strtok(inputFileLine,",");
      metersPtr = strtok(NULL,","); // NULL means "reuse same old string"
      splitTimePtr = strtok(NULL,","); 
      minutesPtr = strtok(NULL,","); 
      persRecPtr = strtok(NULL,","); 

      // store data into the array

      strcpy(workout[count].date, datePtr);
      workout[count].meters = atoi(metersPtr); // convert ascii to integer
      workout[count].splitTimeSeconds = computeSplitTime(splitTimePtr);
      workout[count].minutes = atoi(minutesPtr);
      if ( *persRecPtr == 'y'  )
	workout[count].personalRecord = true;
      else
	workout[count].personalRecord = false;

      // after storing data in the array, increment i

      count++;
      
      // try to read

      infile.getline(inputFileLine, MAX_LINE_LENGTH);
    }

  cout << count << "    records read from " << INPUT_FILE << endl;

  for (int i=0; i<count; i++)
    {
      cout << "On " << workout[i].date 
	   << " Mike rowed " << workout[i].meters << " meters" << endl;

      cout << "It took him ";
      printSplitTime(workout[i].splitTimeSeconds, cout);
      cout  << "." << endl;

      cout << "The total workout took " << workout[i].minutes << " minutes." << endl;
      
      cout << "This was " << ( (workout[i].personalRecord) ? "": "not ")
	   << "a personal record." << endl;

    }

  return 0;
}





