// 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
using namespace std;


#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;


double computeSplitTime(char * splitTimePtr);

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 i=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,","); 

      // temporarily, show that we successfully broke up the string

      cout << "datePtr="      << datePtr     << endl;
      cout << "metersPtr="    << metersPtr   << endl;
      cout << "splitTimePtr=" << splitTimePtr<< endl;
      cout << "minutesPtr="   << minutesPtr  << endl;
      cout << "persRecPtr="   << persRecPtr  << endl;

      // store data into the array

      // this doesn't work: workout[i].date = datePtr;
      // instead do:

      strcpy(workout[i].date, datePtr);
      workout[i].meters = atoi(metersPtr); // convert ascii to integer
      workout[i].splitTimeSeconds = computeSplitTime(splitTimePtr);
      
      

      // after storing data in the array, increment i

      i++;
      
      // try to read

      infile.getline(inputFileLine, MAX_LINE_LENGTH);
    }

  return 0;
}


double computeSplitTime(char * splitTimePtr)
{

  // stub function

   @@@

  return 0.0;


}
