//string_c.cpp
#include "string_c.h"
#include <string.h>
using namespace enhance;
/**
This function will construct a new, empty, string.
*/
String* enhance::String_ctor() {
      //allocate the new string on the heap & initialize values
   String *theString = new String;
   theString->len_  = 0;
   theString->size_ = 0;
   theString->buf_  = 0;
   return theString;
}

/**
This function will construct a new string from the null terminated
character string passed in.
*/
String* enhance::String_ctor(const char* cstr) {
      //allocate the new string on the heap & initialize values
   String *theString = new String;
   theString->len_  = 0;
   theString->size_ = 0;
   theString->buf_  = 0;
      //now initialize all the values to what was passed in
   if (cstr) {
      theString->len_  = strlen(cstr);
      theString->size_ = theString->len_+1;
      theString->buf_  = new char[theString->size_];
      strcpy(theString->buf_, cstr);
   }
   return theString;
}

/**
This function will construct a new string from the one passed in.
*/
String* enhance::String_ctor(const String& str) {
      //allocate the new string on the heap & initialize values
   String *theString = new String;
   theString->len_  = 0;
   theString->size_ = 0;
   theString->buf_  = 0;
      //now initialize all the values to what was passed in
   if (&str) {
      theString->len_  = str.len_;
      theString->size_ = theString->len_ + 1;
      theString->buf_  = new char[theString->size_];
      strcpy(theString->buf_, str.buf_);
   }
   return theString;
}

/**
This function will destory the contents of the string and
   release all allocated resources back to the syatem.
*/
void enhance::dtor(String *theString) {
   if (theString)
      delete [] theString->buf_;  //delete array of chars as array
   delete theString;              //delete single object as single object
}

/**
This function will destory the current contents of the string and assign
it the new value from the string passed in.
*/
String& enhance::assign(String* theString, const String& str) {
   if ((theString) && (&str)) { //check that we were really given two strings
      if (theString != &str) { //check that they aren't the same strings
	 if (theString->size_ < str.len_) {    //check if we are large enough
	    delete [] theString->buf_;         //make sure we delete as array
	    theString->size_ = str.len_ + 1;
	    theString->buf_  = new char[theString->size_];
	 }
	 theString->len_  = str.len_;
	 strcpy(theString->buf_, str.buf_);
      }
   }
   return *theString;      //return a reference to the string with new value
}

/**
This function will determine if the two strings contain the same values.
*/
int enhance::operator==(const String* theString, const String& str) {
   int match = 0;
   if ((theString) && (&str)) { //make sure we were given something
      if ((theString->len_) && (str.len_))
	 match = !strcmp(theString->buf_, str.buf_);
      else if ((!theString->len_) && (!str.len_))
	 match = 1;
   }
   return match;
}

/**
This function will save the contents of the string to the specified
output stream for the purpose of reading it back in later with
restoreFrom().
*/
int enhance::saveOn(const String *theString, ostream &str) {
   int status = -1; //default to error
   if (str) {
         //write the length and then the contents of the string
      str << theString->len_ << endl << theString->buf_ << endl;
         //check the status of the stream after writing to it
      status = (str.fail()) ? -1 : 0; //okay unless write failed
   }
   return status;
}

/**
This function will read the contents of the pre-stored string from the
input stream into the string.
*/
int enhance::restoreFrom(String *theString, istream &str) {
   int status = -1; //default to error
   if (str) {
         //first get the length of the string in the stream
      str >> theString->len_;
      str.ignore(1);  //ignore the \n after the length
         //get rid of the old string
      delete [] theString->buf_;
         //allocate enough space to hold the new string + a null
      theString->buf_ = new char[theString->len_+1];
         //read the string
      str.get(theString->buf_,theString->len_+1);
      str.ignore(1);  //ignore the /n terminator
         //check the status of the stream after we read from it
      status = (str.fail()) ? -1 : 0; //okay unless write failed
   }
   return status;
}
//end of file

