/*	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 - Yet another application framework
 *  Copyright (C) 1997, 1998 William Edward Woody
 *  
 *  This library is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Library
 *  General Public License as published by the Free Software
 *  Foundation; either version 2 of the License, or any
 *  later version.
 *  
 *  This library is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE. See the GNU Library General Public License for
 *  more details.
 *  
 *  You should have received a copy of the GNU Library General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *  
 *  To contact the author, either e-mail me at woody@pandawave.com, 
 *	or write to
 *  
 *          William Edward Woody
 *          In Phase Consulting
 *          1545 Ard Eevin Avenue
 *          Glendale, CA 91202
 *
 *	Or visit us on the web at http://www.pandawave.com
 */

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

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

// @G"Construction/Destruction"

/*	@F"XGString::XGString" @I
 *
 *		This creates an empty C string
 */

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

/*	@F"XGString::XGString" @I
 *
 *		This creates a C string by copying the contents of another XGString
 */

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

/*	@F"XGString::XGString" @I
 *
 *		This creates a C string by copying the contents of the provided C string
 */

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

/*	@F"XGString::~XGString" @I
 *
 *		Releases allocated C string
 */

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

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

// @G"Copy operator"

/*	@F"XGString::operator =" @I
 *
 *		Assignment operator. This is the meat of my string class
 */

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;
		fAlloc = 0;
		return *this;
	}
	
	/*
	 *	Determine how much memory I need, and grow in the off chance...
	 */
	
	len = strlen(x) + 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;
}
