//
//	MiscStringArray.h -- a generic class to store lists of strings
//		Originally written by Drew Davidson (c) 1994 by Drew Davidson.
//		Modified and extended by Don Yacktman for inclusion into the MiscKit.
//				Version 1.0  All rights reserved.
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

/*----------------------------------------------------------------------------
	$Source$

	SYNOPSIS
		Implements an array of char * values built from MiscString objects.

	REVISIONS
	$Log$
----------------------------------------------------------------------------*/
# import <misckit/misckit.h>

@interface MiscStringArray(private)

- _invalidateArray;
- _buildArray;

@end

@implementation MiscStringArray

/*----------------------------< PRIVATE METHODS >----------------------------*/
- _invalidateArray
{
	NX_FREE(stringArray);
	stringArray = NULL;
	return(self);
}

- _buildArray
{
	if (stringArray)
		[self _invalidateArray];
	NX_MALLOC(stringArray,const char *,[strings count]+1);
	MISCforAllListObjects(strings,string)
	{	stringArray[MISCCurrentSlot] = ((uniqued) ? [string uniqueStringValue] : [string stringValue]);
		stringArray[MISCCurrentSlot+1] = NULL;
	} MISCendFor
	return(self);
}

/*---------------------------< INIT/FREE METHODS >---------------------------*/
- init
{
	[super init];
	strings = [[List allocFromZone:[self zone]] initCount:0];
	uniqued = NO;
	stringArray = NULL;
	return(self);
}

- free
{
	[[strings freeObjects] free];
	[self _invalidateArray];
	return([super free]);
}

/*--------------------------< OVERRIDDEN METHODS >---------------------------*/
- copyFromZone:(NXZone *)aZone
{
	self = [super copyFromZone:aZone];
	strings = [strings deepCopyFromZone:aZone];
	stringArray = NULL;
	[self _buildArray];
	return(self);
}

/*-----------------------------< OTHER METHODS >-----------------------------*/
- (BOOL)uniqued
{
	return(uniqued);
}

- setUniqued:(BOOL)yn
{
	if (yn != uniqued)
	{	uniqued = yn;
		[self _invalidateArray];
	}
	return(self);
}

- strings
{
	[self _invalidateArray];
	return(strings);
}

- addString:(const char *)aString
{
	[self _invalidateArray];
	[[self strings] addObject:[[MiscString allocFromZone:[self zone]] initStringValue:aString]];
	return(self);
}

- insertString:(const char *)aString at:(unsigned int)index
{
	[self _invalidateArray];
	[[self strings] insertObject:[[MiscString allocFromZone:[self zone]] initStringValue:aString] at:index];
	return(self);
}

- (unsigned int)stringCount
{
	return([strings count]);
}

- (unsigned int)count
{
	return([strings count]);
}

- (const char **)stringArray
{
	if (!stringArray)
		[self _buildArray];
	return(stringArray);
}

- (const char *)stringAt:(unsigned int)index
{	const char		**array = [self stringArray];

	return(array[index]);
}

- (unsigned int)indexOfString:(const char *)aString
{	unsigned int	index = MISC_NOT_IN_ARRAY;
	
	MISCforAllListObjects(strings,string)
	{	if ([string strcmp:aString] == 0)
		{	index = MISCCurrentSlot;
			break;
		}
	} MISCendFor
	return(index);
}

@end
