/*	XGString.cpp
 *
 *		This implements my simple variable length string handling facility.
 *	I normally allocate string memory in chunks of 64 bytes--this works
 *	as a reasonable size most of the time. If it's not enough memory,
 *	I will allocate more memory as needed.
 */

/**********************************************************************

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <string.h>
#include <XString.h>

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGString::XGString
 *
 *		This creates an empty C string.
 */

XGString::XGString()
{
	fAlloc = 0;
	fLength = 0;
	fData = NULL;
}

/*$	XGString::XGString
 *
 *		This creates a C string by copying the contents of another XGString.
 *	This is equivalent to calling the copy operator.
 */

XGString::XGString(const XGString &x)
{
	fAlloc = 0;
	fLength = 0;
	fData = NULL;
	
	operator = (x);				// Use the '=' operator to copy the contents
}

/*$	XGString::XGString
 *
 *		This creates a C string by copying the contents of the provided
 *	C string using the copy operator.
 */

XGString::XGString(const char *x)
{
	fAlloc = 0;
	fLength = 0;
	fData = NULL;
	
	operator = (x);				// Use the '=' operator to copy the contents
}

/*$	XGString::~XGString
 *
 *		Releases allocated C string. This frees the allocated memory (if
 *	any was allocated).
 */

XGString::~XGString()
{
	if (fData) delete[] fData;
}

/************************************************************************/
/*																		*/
/*	Copy operator														*/
/*																		*/
/************************************************************************/

/*$	XGString::operator =
 *
 *		Assignment operator. This is the meat of my string class. This
 *	handles reallocating the memory associated with this string, and copying
 *	the C string across.
 */

XGString &XGString::operator = (const char *x)
{
	short len;
	char *na;
	short nlen;
	
	/*
	 *	Special case: handle copying the empty string
	 */
	
	if (x == NULL) {
		if (fData) delete[] fData;
		fData = NULL;
		fAlloc = 0;
		return *this;
	}
	 
	/*
	 *	Determine how much memory I need, and grow in the off chance...
	 */
	
	len = strlen(x) + 1;
	fLength = len-1;
	nlen = len;
	if (nlen % 64) nlen += 64 - nlen % 64;
	if (fAlloc != nlen) {
		na = new char[nlen];
		if (fData) delete [] fData;
		fData = na;
		fAlloc = nlen;
	}
	
	/*
	 *	Copy the string
	 */
	
	strcpy(fData,x);
	return *this;
}

// i'm suprised i had to ADD THIS TO YAAF!
int XGString::GetLength(void) const
{
	return fLength;
}