home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / MiscKit1.2.6 / Source / MiscStringArray.m < prev    next >
Encoding:
Text File  |  1994-06-15  |  4.3 KB  |  191 lines

  1. //
  2. //    MiscStringArray.m -- a generic class to store lists of strings
  3. //        Originally written by Drew Davidson (c) 1994 by Drew Davidson.
  4. //        Modified and extended by Don Yacktman for inclusion into the MiscKit.
  5. //                Version 1.2  All rights reserved.
  6. //        This notice may not be removed from this source code.
  7. //
  8. //    This object is included in the MiscKit by permission from the author
  9. //    and its use is governed by the MiscKit license, found in the file
  10. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  11. //    for a list of all applicable permissions and restrictions.
  12. //    
  13.  
  14. /*----------------------------------------------------------------------------
  15.     $Source$
  16.  
  17.     SYNOPSIS
  18.         Implements an array of char * values built from MiscString objects.
  19.  
  20.     REVISIONS
  21.     $Log$
  22. ----------------------------------------------------------------------------*/
  23. # import <misckit/misckit.h>
  24.  
  25. @interface MiscStringArray(private)
  26.  
  27. - _invalidateArray;
  28. - _buildArray;
  29.  
  30. @end
  31.  
  32. @implementation MiscStringArray
  33.  
  34. /*----------------------------< PRIVATE METHODS >----------------------------*/
  35. - _invalidateArray
  36. {
  37.     NX_FREE(stringArray);
  38.     stringArray = NULL;
  39.     return(self);
  40. }
  41.  
  42. - _buildArray
  43. {
  44.     if (stringArray)
  45.         [self _invalidateArray];
  46.     NX_MALLOC(stringArray,const char *,[strings count]+1);
  47.     MISCforAllListObjects(strings,string)
  48.     {    stringArray[MISCCurrentSlot] = ((uniqued) ? [string uniqueStringValue] : [string stringValue]);
  49.         // stringArray[MISCCurrentSlot+1] = NULL; // Carl says unneeded
  50.     } MISCendFor
  51.     stringArray[[self count]] = NULL; // Carl added
  52.     return(self);
  53. }
  54.  
  55. /*---------------------------< INIT/FREE METHODS >---------------------------*/
  56. - init
  57. {
  58.     [super init];
  59.     strings = [[List allocFromZone:[self zone]] initCount:0];
  60.     uniqued = NO;
  61.     stringArray = NULL;
  62.     return(self);
  63. }
  64.  
  65. - free
  66. {
  67.     [[strings freeObjects] free];
  68.     [self _invalidateArray];
  69.     return([super free]);
  70. }
  71.  
  72. /*--------------------------< OVERRIDDEN METHODS >---------------------------*/
  73. - copyFromZone:(NXZone *)aZone
  74. {
  75.     self = [super copyFromZone:aZone];
  76.     strings = [strings deepCopyFromZone:aZone];
  77.     stringArray = NULL;
  78.     [self _buildArray];
  79.     return(self);
  80. }
  81.  
  82. /*-----------------------------< OTHER METHODS >-----------------------------*/
  83. - (BOOL)uniqued
  84. {
  85.     return(uniqued);
  86. }
  87.  
  88. - setUniqued:(BOOL)yn
  89. {
  90.     if (yn != uniqued)
  91.     {    uniqued = yn;
  92.         [self _invalidateArray];
  93.     }
  94.     return(self);
  95. }
  96.  
  97. - strings
  98. {
  99.     [self _invalidateArray];
  100.     return(strings);
  101. }
  102.  
  103. - addString:(const char *)aString
  104. {
  105.     [self _invalidateArray];
  106.     [[self strings] addObject:[[MiscString allocFromZone:[self zone]] initStringValue:aString]];
  107.     return(self);
  108. }
  109.  
  110. - removeString:(const char *)aString
  111. {    // This method will remove aString from the MiscStringArray. If the string
  112.     // doesn't exist, it will  return nil, otherwise it will return self.
  113.     // contributed by Juergen Zeller <zet@cip.e-technik.uni-erlangen.de>
  114.     unsigned int index = MISC_NOT_IN_ARRAY;
  115.  
  116.     index = [self indexOfString:aString];
  117.     if (index == MISC_NOT_IN_ARRAY) return (nil);
  118.     [self _invalidateArray];
  119.     [[[self strings] removeObjectAt:index] free];
  120.     return (self);
  121. }
  122.  
  123.  
  124. - insertString:(const char *)aString at:(unsigned int)index
  125. {
  126.     [self _invalidateArray];
  127.     [[self strings] insertObject:[[MiscString allocFromZone:[self zone]] initStringValue:aString] at:index];
  128.     return(self);
  129. }
  130.  
  131. - (unsigned int)stringCount
  132. {
  133.     return([strings count]);
  134. }
  135.  
  136. - (unsigned int)count
  137. {
  138.     return([strings count]);
  139. }
  140.  
  141. - (const char **)stringArray
  142. {
  143.     if (!stringArray)
  144.         [self _buildArray];
  145.     return(stringArray);
  146. }
  147.  
  148. - (const char *)stringAt:(unsigned int)index
  149. {    const char        **array = [self stringArray];
  150.  
  151.     return(array[index]);
  152. }
  153.  
  154. - (unsigned int)indexOfString:(const char *)aString
  155. {    unsigned int    index = MISC_NOT_IN_ARRAY;
  156.     
  157.     MISCforAllListObjects(strings,string)
  158.     {    if ([string strcmp:aString] == 0)
  159.         {    index = MISCCurrentSlot;
  160.             break;
  161.         }
  162.     } MISCendFor
  163.     return(index);
  164. }
  165.  
  166. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  167. {
  168.     int i;
  169.     id cellList, theCell;
  170.  
  171.     // Set matrix to have the right number of cells.
  172.     [matrix renewRows:[self count] cols:1];
  173.  
  174.     // Get list of cells from the matrix.
  175.     cellList = [matrix cellList];
  176.  
  177.     // For each cell set its value, set whether it is a leaf or not and
  178.     //   mark it loaded.
  179.     for(i=0; i<[cellList count]; i++) {
  180.         theCell = [cellList objectAt:i];
  181.         [theCell setStringValue:[self stringAt:i]];
  182.         [theCell setLeaf:YES];
  183.         [theCell setLoaded:YES];
  184.     }
  185.  
  186.     // Return the number of rows.
  187.     return [self count];
  188. }
  189.  
  190. @end
  191.