// compare.cc  by Goro Takei   revised 6/18/98

#include "compare.h"
#include <fstream.h>
#include "list.h"
#include "profile.h"

// This files contain the matching functions for different 
// data types.
// "CloseMatch()" functions accepts the mismatched characters
// up to half the length of the string desired.

bool CompareFirstName::Match(Profile P)
  {                                   //check for name match
//    cout<<P.GetName()<<" "<<LookString<<" "<<strcmp(P.GetName(),LookString)<<endl;
    return strcmp(P.GetFirstName(),LookString)==0;
  }

bool CompareFirstName::CloseMatch(Profile P)
  {
	
    int  mismatch=0;
    int  m=strlen(LookString);
    int	 l=strlen(P.GetFirstName());

    if (m<l) {mismatch=l-m; }   //if the name field is shorter than 
                                // the one you're looking for, add
                                //number extra spaces as mismatc
    for(int i=0;i<strlen(LookString);i++)
      {
        if(P.GetFirstName()[i]!=LookString[i]) 	        //check each letter 
          {                                     //if they are different,
            mismatch++;                         //mismatch goes up by one.
          }
//      cout<<mismatch<<" "<<P.GetName()[i]<<" "<<LookString[i]<<endl;
      }
//	cout<<P.GetFirstName()<<"?"<<LookString<<" "<<mismatch<<endl;	
    return mismatch<int(strlen(LookString));
  }


bool CompareFirstName::WildCardMatch(Profile P)
  {
    int mismatch=0;
    for(int i=0;i<strlen(LookString)-1;i++)       //for characters up to the "*"
      {
        if(P.GetFirstName()[i]!=LookString[i]) mismatch++;  //check for character match
      }
		
     return mismatch==0;                //it matches if all char match up to "*"
}

////////////////////////////////////////////////////////


bool CompareLastName::Match(Profile P)
  {                                   //check for name match
//    cout<<P.GetName()<<" "<<LookString<<" "<<strcmp(P.GetName(),LookString)<<endl;
    return strcmp(P.GetLastName(),LookString)==0;
  }

bool CompareLastName::CloseMatch(Profile P)
  {
	
    int  mismatch=0;
    int  m=strlen(LookString);
    int	 l=strlen(P.GetLastName());

    if (m<l) {mismatch=l-m; }   //if the name field is shorter than 
                                // the one you're looking for, add
                                //number extra spaces as mismatch
    for(int i=0;i<strlen(LookString);i++)
      {
        if(P.GetLastName()[i]!=LookString[i]) 	        //check each letter 
          {                                     //if they are different,
            mismatch++;                         //mismatch goes up by one.
          }
//      cout<<mismatch<<" "<<P.GetLastName()[i]<<" "<<LookString[i]<<endl;
      }
//	cout<<P.GetLastName()<<"?"<<LookString<<" "<<mismatch<<endl;	
    return mismatch<int(strlen(LookString));
  }


bool CompareLastName::WildCardMatch(Profile P)
  {
    int mismatch=0;
    for(int i=0;i<strlen(LookString)-1;i++)       //for characters up to the "*"
      {
        if(P.GetLastName()[i]!=LookString[i]) mismatch++;  //check for character match
      }
		
     return mismatch==0;                //it matches if all char match up to "*"
}

//////////////////////////////////////////////////////////////
	
	
bool CompareStreet::Match(Profile P)
  {
    return strcmp(P.GetAdrsStrtName(),LookString)==0;	
  }

	
bool CompareStreet::CloseMatch(Profile P)
  {
    int mismatch=0;
    int m=strlen(LookString);
    int l=strlen(P.GetAdrsStrtName());

    if (m<l) {mismatch=l-m; }   //if the name field is shorter than 
                                // the one you're looking for, add
                                //number extra spaces as mismatch; 
    for(int i=0;i<strlen(LookString);i++)
      {
        if(P.GetAdrsStrtName()[i]!=LookString[i]) 
           {  mismatch++; }
				
//      cout<<mismatch<<" "<<P.GetName()[i]<<" "<<LookString[i]<<endl;
      }
	cout<<P.GetAdrsStrtName()<<"?"<<LookString<<" "<<mismatch<<endl;	
    return mismatch<int(strlen(LookString));
}

bool CompareStreet::WildCardMatch(Profile P)
  {
    int mismatch=0;
	
     for(int i=0;i<strlen(LookString)-1;i++)
       {
         if(P.GetAdrsStrtName()[i]!=LookString[i]) mismatch++; 
       }

    return mismatch==0;
  }

////////////////////////////////////////////////////////////

bool CompareCity::Match(Profile P)
  {
    return strcmp(P.GetCity(),LookString)==0;	
  }

	
bool CompareCity::CloseMatch(Profile P)
  {
    int mismatch=0;
    int m=strlen(LookString);
    int l=strlen(P.GetCity());

    if (m<l) {mismatch=l-m; }   //if the name field is shorter than 
                                // the one you're looking for, add
                                //number extra spaces as mismatch; 
    for(int i=0;i<strlen(LookString);i++)
      {
        if(P.GetCity()[i]!=LookString[i]) 
           {  mismatch++; }
				
//      cout<<mismatch<<" "<<P.GetName()[i]<<" "<<LookString[i]<<endl;
      }
//	cout<<P.GetCity()<<"?"<<LookString<<" "<<mismatch<<endl;	
    return mismatch<int(strlen(LookString));
}

bool CompareCity::WildCardMatch(Profile P)
  {
    int mismatch=0;
	
     for(int i=0;i<strlen(LookString)-1;i++)
       {
         if(P.GetCity()[i]!=LookString[i]) mismatch++; 
       }

    return mismatch==0;
  }

/////////////////////////////////////////////////////////////

bool CompareState::Match(Profile P)
  {
    return strcmp(P.GetState(),LookString)==0;	
  }

	
bool CompareState::CloseMatch(Profile P)
  {
    int mismatch=0;
    int m=strlen(LookString);
    int l=strlen(P.GetState());

    if (m<l) {mismatch=l-m; }   //if the name field is shorter than 
                                // the one you're looking for, add
                                //number extra spaces as mismatch; 
    for(int i=0;i<strlen(LookString);i++)
      {
        if(P.GetState()[i]!=LookString[i]) 
           {  mismatch++; }
				
//      cout<<mismatch<<" "<<P.GetName()[i]<<" "<<LookString[i]<<endl;
      }
//	cout<<P.GetState()<<"?"<<LookString<<" "<<mismatch<<endl;	
    return mismatch<int(strlen(LookString));
}

bool CompareState::WildCardMatch(Profile P)
  {
    int mismatch=0;
	
     for(int i=0;i<strlen(LookString)-1;i++)
       {
         if(P.GetState()[i]!=LookString[i]) mismatch++; 
       }

    return mismatch==0;
  }
