/*
 * University of Utah
 * 2005 High School Programming Contest
 * Record Matching
 *
 * Filename: RecordMatcher.java
 * 
 * Description: Implements a basic record matcher.  Will load a file
 * with a format as specified in the problem description and output indices
 * of matching pairs of records.  This basic implementation will consider
 * two records to be matching if the lowercased values of each field match.
 *
 * Modification History:
 * 2/21/2005 - Initial Release
 */

#include <iostream>
#include <fstream>
#include <vector>
#include "record.h"

using namespace std;

// A vector of record objects
typedef vector<record> rvector;

// Function prototypes
rvector readFile(char* filename);
void runRecordMatcher(rvector records);
  
// Main
// The filename of the input file is expected as a single
// command line parameter.  If it is not provided, a message is
// output to the screen and the program execution is terminated.
// Otherwise, record matching is performed and the matching pair
// indices are output to standard out, one pair per line.
int main(int argc, char* argv[]) {
  // Check command-line parameters
  if (argc != 2) {
    cout << "Expecting command line parameter." << endl;
    cout << "Proper syntax:" << endl;
    cout << "  " << argv[0] << " <filename>" << endl;
    return 1;
  }

  rvector records = readFile(argv[1]);
  runRecordMatcher(records);
  return 0;
}
  
  
// Reads the specified file into a new vector of Record objects.
//
// Parameters:
// filename - name of a file containg the records. Formatted as
//            specified in problem description.
rvector readFile(char* filename) {
  rvector records;
  ifstream input;
  input.open(filename, ios::in);

  string uid;
  string fName;
  string lName;
  string address;
  string zip;
  string phone;
  string email;
  string empty;
  // Load each sentence in the story
  while (!input.eof()) {
    getline(input, uid);
    getline(input, fName);
    getline(input, lName);
    getline(input, address);
    getline(input, zip);
    getline(input, phone);
    getline(input, email);
    // Eat empty line.
    getline(input, empty);
    
    records.push_back(record(uid, fName, lName, address,
                             zip, phone, email));
  }      
  input.close();
  return records;
}

  
// Performs record matching and outputs the indices of matching
// pairs of recrods.
// 
// Parameters:
// records - Vector of Record objects containing the records.  This
//           function should not modify this data.
void runRecordMatcher(rvector records) {
  for (int i = 0; i < records.size(); i++) {
    record ri = records[i];
    for (int j = i+1; j < records.size(); j++) {
      record rj = records[j];
      if (ri.lowerCaseEquals(rj)) {
        cout << ri.getUID() << "," << rj.getUID() << endl;
      }
    }
  }
}


