// baseballMaster.cc  P. Conrad for CISC220, 06J
// Class for Lahman Baseball Database (www.baseball1.com), version 5.3

// See license and copyright statement at bottom of file



#include "date.h"
#include "event.h"
#include "baseballMaster.h"
#include <cstring>
#include <iostream>
#include <cstdlib>
#include "strtokWithQuotedStrings.h"


  // construct one object from a line in the Master.csv file

BaseballMaster_C::BaseballMaster_C( char * const inputLine)
{
  char * lahmanIDPtr = strtokWithQuotedStrings(inputLine,',','"');
  lahmanID = atoi(lahmanIDPtr);

  char * playerIDPtr = strtokWithQuotedStrings(NULL,',','"');
  playerID = new char [strlen(playerIDPtr) + 1];
  strcpy(playerID, playerIDPtr);

  char * managerIDPtr = strtokWithQuotedStrings(NULL,',','"');
  managerID = new char [strlen(managerIDPtr) + 1];
  strcpy(managerID, managerIDPtr);

  char * hofIDPtr = strtokWithQuotedStrings(NULL,',','"');
  hofID = new char [strlen(hofIDPtr) + 1];
  strcpy(hofID, hofIDPtr);

  char * birthYearPtr = strtokWithQuotedStrings(NULL,',','"');
  char * birthMonthPtr = strtokWithQuotedStrings(NULL,',','"');
  char * birthDayPtr = strtokWithQuotedStrings(NULL,',','"');
  char * birthCountryPtr = strtokWithQuotedStrings(NULL,',','"');
  char * birthStatePtr = strtokWithQuotedStrings(NULL,',','"');
  char * birthCityPtr = strtokWithQuotedStrings(NULL,',','"');

  birth = new Event_C( Date_C(atoi(birthYearPtr),
			      atoi(birthMonthPtr),
			      atoi(birthDayPtr)),
		       birthCountryPtr,
		       birthStatePtr,
		       birthCityPtr);
  
  char * deathYearPtr = strtokWithQuotedStrings(NULL,',','"');
  char * deathMonthPtr = strtokWithQuotedStrings(NULL,',','"');
  char * deathDayPtr = strtokWithQuotedStrings(NULL,',','"');
  char * deathCountryPtr = strtokWithQuotedStrings(NULL,',','"');
  char * deathStatePtr = strtokWithQuotedStrings(NULL,',','"');
  char * deathCityPtr = strtokWithQuotedStrings(NULL,',','"');

  if ( strcmp(deathYearPtr,"") == 0 )
    death = NULL;
  else
    death = new Event_C( Date_C(atoi(deathYearPtr),
				atoi(deathMonthPtr),
				atoi(deathDayPtr)),
			 deathCountryPtr,
			 deathStatePtr,
			 deathCityPtr);

  char * firstNamePtr = strtokWithQuotedStrings(NULL,',','"');
  nameFirst = new char [strlen(firstNamePtr) + 1];
  strcpy(nameFirst, firstNamePtr);

  char * lastNamePtr = strtokWithQuotedStrings(NULL,',','"');
  nameLast = new char [strlen(lastNamePtr) + 1];
  strcpy(nameLast, lastNamePtr);

  char * noteNamePtr = strtokWithQuotedStrings(NULL,',','"');
  nameNote = new char [strlen(noteNamePtr) + 1];
  strcpy(nameNote, noteNamePtr);

  char * givenNamePtr = strtokWithQuotedStrings(NULL,',','"');
  nameGiven = new char [strlen(givenNamePtr) + 1];
  strcpy(nameGiven, givenNamePtr);

  char * nickNamePtr = strtokWithQuotedStrings(NULL,',','"');
  nameNick = new char [strlen(nickNamePtr) + 1];
  strcpy(nameNick, nickNamePtr);

  char * weightPtr = strtokWithQuotedStrings(NULL,',','"');
  weight = atoi(weightPtr); 

  char * heightPtr = strtokWithQuotedStrings(NULL,',','"');
  height = atoi(heightPtr); 

  // note that for char fields, we dereference the ptr
  // we could also do bats = batsPtr[0];

  char * batsPtr = strtokWithQuotedStrings(NULL,',','"');
  bats = *batsPtr;

  char * throwsPtr = strtokWithQuotedStrings(NULL,',','"');
  throws = *throwsPtr;

  char * debutPtr = strtokWithQuotedStrings(NULL,',','"');
  debut = new Date_C(debutPtr);
  
  char * finalGamePtr = strtokWithQuotedStrings(NULL,',','"');
  if (strcmp(finalGamePtr,"") == 0)
    finalGame = NULL;
  else
    finalGame = new Date_C(finalGamePtr);  

  // @@@ college,lahman40ID,lahman45ID,retroID,holtzID,bbrefID";

  char * collegePtr = strtokWithQuotedStrings(NULL,',','"');
  college = new char [strlen(collegePtr) + 1];
  strcpy(college, collegePtr);

  char * lahman40IDPtr = strtokWithQuotedStrings(NULL,',','"');
  lahman40ID = new char [strlen(lahman40IDPtr) + 1];
  strcpy(lahman40ID, lahman40IDPtr);

  char * lahman45IDPtr = strtokWithQuotedStrings(NULL,',','"');
  lahman45ID = new char [strlen(lahman45IDPtr) + 1];
  strcpy(lahman45ID, lahman45IDPtr);

  char * retroIDPtr = strtokWithQuotedStrings(NULL,',','"');
  retroID = new char [strlen(retroIDPtr) + 1];
  strcpy(retroID, retroIDPtr);

  char * holtzIDPtr = strtokWithQuotedStrings(NULL,',','"');
  holtzID = new char [strlen(holtzIDPtr) + 1];
  strcpy(holtzID, holtzIDPtr);

  char * bbrefIDPtr = strtokWithQuotedStrings(NULL,',','"');
  bbrefID = new char [strlen(bbrefIDPtr) + 1];
  strcpy(bbrefID, bbrefIDPtr);


  

}
  

// print just a few key fields
void BaseballMaster_C::print(std::ostream & out) const 
{
  out << "@@@ NOT IMPLEMENTED YET: " 
      << __FILE__ << ", line" << __LINE__ << std::endl;
}


// COPYRIGHT NOTICE:

// This code corresponds to the data layout of the Lahmann Database,
// which is copyrighted by Sean Lahmann.  The data and formats here
// are used by special permission of Sean Lahmann, granted to Phill 
// Conrad for use in Computer Science courses at the University of 
// Delaware.  You must obtain permission from Sean Lahmann to use this
// data and/or data format for any other purpose.  For more details,
// visit www.baseball1.com


