// StudentRecord.cpp
// CSI 2131 Asst 2a by Naim R. El-Far (naim@discover.uottawa.ca)
// CSI 2131 - Winter 2006 - Prof. L.M. Moura

// File:	StudentRecord.cpp
// Desc:	Class StudentRecord definitions

#include <iostream>
#include <fstream>
#include <string>
#include "StudentRecord.h"
using namespace std;

StudentRecord::StudentRecord() { //Default constructor: Initializes data members to default values
	email = "";
	firstName = "";
	lastName = "";
	offsetInDataFile = -1;
	studentNumber = "";
	unit = "";
}

StudentRecord::StudentRecord(long int offset, string rawString) { //String constructor: Given a string, parse it and assign each value to its respective field
	offsetInDataFile = offset;
	ParseAndFillFields(rawString);
}

StudentRecord::StudentRecord(StudentRecord* ptrToRHS) { //Copy constructor: Copy information from right-hand side (RHS) record to this record. An assignment (i.e. = operation) for dynamically allocated classes
	email = ptrToRHS->email;
	firstName = ptrToRHS->firstName;
	lastName = ptrToRHS->lastName;
	offsetInDataFile = ptrToRHS->offsetInDataFile;
	studentNumber = ptrToRHS->studentNumber;
	unit = ptrToRHS->unit;
}

StudentRecord::~StudentRecord() { //Destructor: No dynamic data to delete and no flags to reset so empty
}

void StudentRecord::PrintFormatted() { //Output the record information to the screen
	cout << "Offset:\t"			<< offsetInDataFile	<< endl
		 << "Last name:\t"		<< lastName			<< endl
		 << "First name:\t"		<< firstName		<< endl
		 << "Student#:\t"		<< studentNumber	<< endl
		 << "Unit:\t"			<< unit				<< endl
		 << "Email:\t"			<< email			<< endl
		 << "-----------------------" << endl;
}

void StudentRecord::ParseAndFillFields(string rawString) { //Given a string corresponding to a full line out of the data text file, break it up and populate the fields with the info from it
	string		fieldDelimiter = ";";
	char*		token = NULL;

	token = strtok((char*)rawString.c_str(), (char*)fieldDelimiter.c_str());
	lastName = token;
	token = strtok(NULL, (char*)fieldDelimiter.c_str());
	firstName = token;
	token = strtok(NULL, (char*)fieldDelimiter.c_str());
	studentNumber = token;
	token = strtok(NULL, (char*)fieldDelimiter.c_str());
	unit = token;
	token = strtok(NULL, (char*)fieldDelimiter.c_str());
	email = token;
}