home *** CD-ROM | disk | FTP | other *** search
- //
- // MiscStringArray.m -- 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.2 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; // Carl says unneeded
- } MISCendFor
- stringArray[[self count]] = NULL; // Carl added
- 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);
- }
-
- - removeString:(const char *)aString
- { // This method will remove aString from the MiscStringArray. If the string
- // doesn't exist, it will return nil, otherwise it will return self.
- // contributed by Juergen Zeller <zet@cip.e-technik.uni-erlangen.de>
- unsigned int index = MISC_NOT_IN_ARRAY;
-
- index = [self indexOfString:aString];
- if (index == MISC_NOT_IN_ARRAY) return (nil);
- [self _invalidateArray];
- [[[self strings] removeObjectAt:index] free];
- 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);
- }
-
- - (int)browser:sender fillMatrix:matrix inColumn:(int)column
- {
- int i;
- id cellList, theCell;
-
- // Set matrix to have the right number of cells.
- [matrix renewRows:[self count] cols:1];
-
- // Get list of cells from the matrix.
- cellList = [matrix cellList];
-
- // For each cell set its value, set whether it is a leaf or not and
- // mark it loaded.
- for(i=0; i<[cellList count]; i++) {
- theCell = [cellList objectAt:i];
- [theCell setStringValue:[self stringAt:i]];
- [theCell setLeaf:YES];
- [theCell setLoaded:YES];
- }
-
- // Return the number of rows.
- return [self count];
- }
-
- @end
-