// File: stringitem.C
//
// string operations needed supported by Item class 

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

Item::Item() {		// default constructor

   item = NULL ;
}


Item::Item(data x) {	// alternate constructor 

   if (x == NULL) return ;

   item = strdup(x) ; 
   if (item == NULL) {
      fprintf(stderr, "could not duplicate string\n") ;
      exit(1) ;
   }
}


Item::~Item() {		// destructor

// Debugging:   
// printf("Item Destructor: (%p, %s)\n", item, item) ;

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


data Item::copy() {	// make a copy
   char *str ;

   if (item == NULL) return NULL ;

   str = strdup(item) ;
   if (str == NULL) {
      fprintf(stderr, "could not duplicate string\n") ;
      exit(1) ;
   }
   return str ;
}


// Compare data in host object with given data.
// <0 is smaller, =0 is equal, >0 is bigger 
//
int Item::compare(data x) {      

  if (item == NULL && x == NULL) return 0 ;
  if (item == NULL) return -1 ;
  if (x == NULL) return 1 ;

   return strcmp(item, x) ;
}


void Item::print() {	// print to stdout 
   if (item != NULL) {

// Debugging:
//    printf("(%p,%s)", item, item) ;

      printf("\"%s\"", item) ;
   }
}
