// birthday.cc    main program for Birthday_C class
// P. Conrad, for CISC220, 06J

#include <iostream>
#include <fstream>
using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
using std::ios;

#include "Birthday.h"

#include <cstdlib> // for g++, need this for atoi()
#include <cstring>


#define INPUT_LINE_LEN 1024   // no semicolon on a #define

int main(int argc, char *argv[])
{

  if ( argc != 4)
    {
      cerr << "Usage: " << argv[0] << " filename month day  " << endl;
      cerr << "  This will print all people in the file whose "
	   << "birthday is on that date" << endl;
      exit(1);  // error condition available at unix cmd line -- echo $?
    }
  
  int userMonth = atoi(argv[2])          ; // the month user is looking for 
  int userDay =  atoi(argv[3])       ; // the day the user is looking for
  

  ifstream inf(argv[1],ios::in);
  
  if (!inf)
    {
      cerr << "Error: could not open " << argv[1] << endl;
      exit(2);
    }

  // now we know that we have an open file on inf
  // so read all lines from the file

  
  char inputLine[INPUT_LINE_LEN];

  
  const int MAX_NUM_BIRTHDAYS = 10;

  Birthday_C *birthdays[MAX_NUM_BIRTHDAYS]={0};

  int count = 0;
  
  inf.getline(inputLine, INPUT_LINE_LEN);
  while (!inf.eof())
    {
      // process the inputLine
      // specifically, use strtok to get the stuff

      char *userNamePtr = strtok(inputLine,",");
      char *firstNamePtr = strtok(NULL,",");
      char *monthPtr = strtok(NULL,",");
      char *dayPtr = strtok(NULL,",");


      // On Tuesday, add code to 
      // instantiate the class, and add 
      // birthday into the array
      
      birthdays[count] = new Birthday_C(userNamePtr,
					firstNamePtr,
					atoi(monthPtr),
					atoi(dayPtr));
      count ++;   

#ifdef DEBUG
      cout << inputLine << endl; // temporary to make sure we are reading ok
#endif // DEBUG


      inf.getline(inputLine, INPUT_LINE_LEN);
    }

#ifdef DEBUG
  cout << "Here are the contents of the array" << endl;
  for (int i=0; i<count; i++)
    cout << birthdays[i] << endl;

#endif // DEBUG

  for (int i=0; i<count; i++)
    if ( birthdays[i]->getMonth() == userMonth && 
	 birthdays[i]->getDay() == userDay)
      {
	cout << birthdays[i]->getName()  << " has a birthday on " 
	     << userMonth << "/" << userDay << endl;

      }


}
