//  File: record3.C
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "record3.h"


Record::Record() {  // Default Constructor
   printf("Default Constructor: this=%p\n", this) ;
   str = NULL ;
}


Record::Record(char *s) { // Alternate Constructor
   printf("Alternate Constructor: this=%p, s=(%p,\"%s\"),",
      this, s, s) ;
   str = strdup(s) ;
   if (str == NULL) {
      printf("Ouch\n") ;
      exit(1) ;
   }
   printf("str=(%p,\"%s\")\n", str, str) ;
}

Record::Record(Record& R) { // Duplicate Constructor
   if (R.str == NULL) {
      printf("Duplicate Constructor: this=%p, &R=%p, R.str=(nil)\n",
        this, &R) ;
      str = NULL ;
      return ;
   }

   printf("Duplicate Constructor: this=%p, &R=%p, R.str=(%p,%s),",
      this, &R, R.str, R.str) ;
   str = strdup(R.str) ;
   if (str == NULL) {
      printf("Ouch\n") ;
      exit(1) ;
   }
   printf("str=(%p,\"%s\")\n", str, str) ;
}


Record& Record::operator=(const Record &R) {
   printf("Assignment Operator:  this=%p, &R=%p, R.str=(%p,%s),",
      this, &R, R.str, R.str) ;
   str = strdup(R.str) ;
   if (str == NULL) {
      printf("Ouch\n") ;
      exit(1) ;
   }
   printf("str=(%p,\"%s\")\n", str, str) ;
   return *this ;
}


Record::~Record() { // Destructor
   // nothing needs to be done.
   printf("Destructor: this=%p, str=(%p,\"%s\")\n", this, str, str) ;

   if (str != NULL) free(str) ;
}


void Record::copy(Record& R) {
   if (R.str != NULL) free(R.str) ;

   R.str = strdup(str) ;
   if (R.str == NULL) {
      fprintf(stderr, "Could not copy!!\n") ;
      exit(1) ;
   }
}

void Record::id() {
   printf ("id: this=%p, str=(%p,\"%s\")\n", this, str, str) ;
}
