home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiscKit1.2.6 / Palettes / MiscTeePalette / StringList.bproj / StringList.m < prev   
Encoding:
Text File  |  1993-07-15  |  14.1 KB  |  372 lines

  1. // Format: 80 columns, tabs = 4 spaces
  2. #import "StringList.h"
  3.  
  4. /******************************************************************************
  5.     StringList - by Jeff Martin
  6.  
  7.     StringList (a subclass of Storage) is a convenient way to deal with lists of character strings. It contains methods for adding strings, inserting strings, sorting strings and searching for strings.
  8.         
  9.     StringList can also add strings from a delimited string (such as the ones passed to an app by the workspace). The delimiters can be supplied or assumed to be white space.
  10.     
  11.     Finally, StringList implements a browser delegate method so that it can easily display itself if set to be a browser delegate.
  12.  
  13.     To use this object inside of InterfaceBuilder, simply drag its icon from the palettes window into the suitcases window. Then drag in a browser and set its delegate to be the stringlist. Type in some entries into StringList's inspector and test interface. The browser fills up!
  14.  
  15. Written by: Jeff Martin (jmartin@next.com)
  16. You may freely copy, distribute and reuse the code in this example.  
  17. Don't even talk to me about warranties.
  18. ******************************************************************************/
  19.     
  20. @implementation StringList : Storage
  21.  
  22. - init
  23.     // Customize Storage to be 'char *' only
  24.     [super initCount:0 elementSize:4 description:"*"];
  25.  
  26.     // StringList is assumed to be sorted by default and when emptied
  27.     //    Adding a string without sorted flag causes 'isSorted' to be set to NO
  28.     isSorted = YES;
  29.     return self; 
  30. }
  31.  
  32. /******************************************************************************
  33.     addString:, addStringIfAbsent: addStringNoCopy: addStringIfAbsentNoCopy:
  34.     addString:at:, addStringIfAbsent:at:, addStringIfAbsent:at:
  35.     addStringSorted:, addStringIfAbsentSorted:, addStringIfAbsentNoCopySorted:
  36.  
  37.     These methods are convenience methods that simply call the master addString method and provide shorter names for cleaner use assuming the defaults of ifAbsent=NO, noCopy=NO, sorted=NO and at=count. Returns the result of the main addString method (self).
  38. ******************************************************************************/
  39. - addString:(const char *)string { return 
  40.     [self addString:string ifAbsent:NO noCopy:NO sorted:NO at:[self count]];  }
  41. - addStringIfAbsent:(const char *)string { return 
  42.     [self addString:string ifAbsent:YES noCopy:NO sorted:NO at:[self count]]; }
  43. - addStringNoCopy:(const char *)string { return 
  44.     [self addString:string ifAbsent:NO noCopy:YES sorted:NO at:[self count]]; }
  45. - addStringIfAbsentNoCopy:(const char *)string { return
  46.     [self addString:string ifAbsent:YES noCopy:YES sorted:NO at:[self count]];}
  47.  
  48. - addString:(const char *)string at:(int)at;
  49. { return [self addString:string ifAbsent:NO noCopy:NO sorted:NO at:at]; }
  50. - addStringIfAbsent:(const char *)string at:(int)at;
  51. { return [self addString:string ifAbsent:YES noCopy:NO sorted:NO at:at]; }
  52. - addStringNoCopy:(const char *)string at:(int)at;
  53. { return [self addString:string ifAbsent:NO noCopy:YES sorted:NO at:at]; }
  54. - addStringIfAbsentNoCopy:(const char *)string at:(int)at;
  55. { return [self addString:string ifAbsent:YES noCopy:YES sorted:NO at:at]; }
  56.  
  57. - addStringSorted:(const char *)string;
  58. { return [self addString:string ifAbsent:NO noCopy:NO sorted:YES at:0]; }
  59. - addStringIfAbsentSorted:(const char *)string;
  60. { return [self addString:string ifAbsent:YES noCopy:NO sorted:YES at:0]; }
  61. - addStringNoCopySorted:(const char *)string;
  62. { return [self addString:string ifAbsent:NO noCopy:YES sorted:YES at:0]; }
  63. - addStringIfAbsentNoCopySorted:(const char *)string;
  64. { return [self addString:string ifAbsent:YES noCopy:YES sorted:YES at:0]; }
  65.  
  66. /******************************************************************************
  67.     addString:ifAbsent:noCopy:sorted:at:
  68.  
  69.     This method provides a flexible method for adding strings to the list. The ifAbsent flag specifies whether a string should be added if it already exists. The noCopy flag specifies whether the given string should be used (or copied). The sorted flag specifies whether the string should be added alphabetically. The at value specifies where the string should be inserted at if it is not added alphabetically. Returns self.
  70. ******************************************************************************/
  71. - addString:(const char *)string ifAbsent:(BOOL)ifAbsent noCopy:(BOOL)noCopy sorted:(BOOL)sorted at:(int)at
  72. {
  73.     BOOL stringExists;
  74.     int index = (ifAbsent || sorted)? 
  75.         [self indexOfString:string exists:&stringExists] : 0;
  76.  
  77.     // If we add only if absent and string is in list return self
  78.     if(ifAbsent && stringExists) return self;
  79.  
  80.     // If not noCopy (in other words, if copy) make copy
  81.     if(!noCopy) string = NXCopyStringBufferFromZone(string, [self zone]);
  82.  
  83.     // If sorted, get index else add at 'at'; otherwise set isSorted flag to NO
  84.     if(sorted) at = index; else isSorted = NO;
  85.     
  86.     // Add the string and return
  87.     [self insertElement:(char **)&string at:at];
  88.     return self;
  89. }
  90.  
  91. /******************************************************************************
  92.     addStrings, addStringList
  93.  
  94.     These methods allow for lists of strings to be added either from a char ** or from a StringList object. Returns self.
  95. ******************************************************************************/
  96. - addStrings:(const char *const*)strings { return 
  97.     [self addStrings:strings ifAbsent:NO noCopy:NO sorted:NO at:[self count]];}
  98. - addStrings:(const char *const*)strings ifAbsent:(BOOL)ifAbsent noCopy:(BOOL)noCopy sorted:(BOOL)sorted at:(int)at
  99. {
  100.     char **temp = (char **)strings;
  101.     
  102.     // Add each string individually, incrementing 'at' to preserve their order
  103.     while(*temp) {
  104.         [self addString:*temp ifAbsent:ifAbsent noCopy:noCopy 
  105.             sorted:sorted at:at];
  106.         at++; temp++;
  107.     }
  108.     
  109.     // If 'noCopy' then we own the memory 'strings' and should free it
  110.     if(noCopy) free((char *)strings);
  111.     
  112.     return self;
  113. }
  114.  
  115. - addStringList:stringListObject 
  116. { return [self addStrings:[stringListObject strings]]; }
  117. - addStringList:stringListObject ifAbsent:(BOOL)ifAbsent noCopy:(BOOL)noCopy sorted:(BOOL)sorted at:(int)at
  118. { return [self addStrings:[stringListObject strings] ifAbsent:ifAbsent 
  119.         noCopy:noCopy sorted:sorted at:at]; }
  120.  
  121. /******************************************************************************
  122.     addDelimitedStrings:delimiters:
  123.     addDelimitedStrings:delimiters:ifAbsent:sorted:at:
  124.  
  125.     This method takes a delimited string (like the ones passed from the workspace) and searches for the given delimiters (NULL for general whitespace). It adds each string that it finds between the delimiters using the addString method. Returns self.
  126. ******************************************************************************/
  127. - addDelimitedStrings:(const char *)string delimiters:(const char *)dels
  128. { return [self addDelimitedStrings:string delimiters:dels ifAbsent:NO  sorted:NO at:[self count]]; }
  129. - addDelimitedStrings:(const char *)string delimiters:(const char *)dels ifAbsent:(BOOL)ifAbsent sorted:(BOOL)sorted at:(int)at
  130. {
  131.     static char defaultDels[] = {' ', '\t', '\n', '\r', '\0'};
  132.     int delCount, i;
  133.     char *currChar, *currString, *stringEnd;
  134.  
  135.     // Check to see if we were handed a bogus string
  136.     if(!string || !strlen(string)) return self;
  137.  
  138.     // Check for default delimiter and get number of delimiters
  139.     if(!dels) dels = defaultDels;
  140.     delCount = strlen(dels) + 1;
  141.     
  142.     // Make copy of the string (going to use string as scratch)
  143.     string = NXCopyStringBuffer(string);
  144.     currChar = currString = (char *)string; 
  145.     stringEnd = (char *)((int)string+strlen(string));
  146.     
  147.     // Look at each character until we pass null terminator(stringEnd)
  148.     while(currChar <= stringEnd) {
  149.         
  150.         // Check the character against the delimiters
  151.         for(i=0; i<delCount; i++)
  152.  
  153.             // If current character matches a delimiter add current string
  154.             if(*currChar==dels[i]) {
  155.             
  156.                 // Set delimiter to NULL('\0')
  157.                 *currChar = '\0'; 
  158.                 
  159.                 // Only add if there is something to add. Increment at.
  160.                 if(strlen(currString))
  161.                     [self addString:currString ifAbsent:ifAbsent noCopy:NO 
  162.                         sorted:sorted at:at++];
  163.  
  164.                 // set currString to start of next string(just past curr del)
  165.                 currString = currChar + 1;
  166.                 break;
  167.             }
  168.         currChar++;
  169.     }
  170.     
  171.     free((char *)string);
  172.     return self;
  173. }
  174.  
  175.  
  176. /******************************************************************************
  177.     (const char *const*)strings
  178.     (const char *)stringAt:(int)at
  179.  
  180.     The strings methods returns all of the strings as an array of char pointers. This array is NOT null terminated.
  181.     The stringAt: method returns the string at a particular index.
  182. ******************************************************************************/
  183. - (const char *const*)strings     { return (const char *const*)dataPtr; }
  184. - (const char *)stringAt:(int)at { return *(char **)[self elementAt:at]; }
  185.  
  186.  
  187. /******************************************************************************
  188.     (BOOL)stringExists
  189.     (unsigned int)indexOfString
  190.     (unsigned int)indexOfString exists:(BOOL *)exists
  191.  
  192.     stringExists returns whether or not the string is already in the list. indexOfString returns either the strings current index or the index that it should be alphabetically(returning [self count] if not sorted).
  193.     indexOfString:stringExists: is a composite method that returns both values in about the same amount of time that it takes to compute one.
  194. ******************************************************************************/
  195. - (BOOL)stringExists:(const char *)string
  196. { BOOL exists; [self indexOfString:string exists:&exists]; return exists; }
  197.  
  198. - (unsigned)indexOfString:(const char *)string
  199. { return [self indexOfString:string exists:NULL]; }
  200.  
  201. - (unsigned)indexOfString:(const char *)string exists:(BOOL *)exists
  202. {
  203.     int index;
  204.     
  205.     // Assume the string won't be found
  206.     if(exists) *exists = NO;
  207.  
  208.     // If list is empty or no string or zero strlen, return end of list
  209.     if(![self count] || !string || !strlen(string)) return [self count];
  210.     
  211.     // If not sorted do sequential search
  212.     if(!isSorted) {
  213.         int i=0;
  214.         for(i=0; i<[self count]; i++) 
  215.             if([self stringAt:i] && (!strcasecmp(string, [self stringAt:i]))) 
  216.                 { if(exists) *exists = YES; break; }
  217.  
  218.         index = i;
  219.     }
  220.     
  221.     // Otherwise if it is sorted do a binary search
  222.     else {
  223.         int l = 0;                        // lower index
  224.         int u = [self count] - 1;        // upper index
  225.         int m = 0;                        // middle index
  226.         int guess = 0;                    // compare val.
  227.         
  228.         while(l <= u) {
  229.             m = (l+u)/2;
  230.             guess = strcasecmp([self stringAt:m], string);
  231.             
  232.             // If guess is too high, adjust the upper value
  233.             if(guess>0) u = m-1;
  234.             
  235.             // If guess is too low, adjust the lower value
  236.             else if(guess<0) l = m+1;
  237.             
  238.             // If guess is equal to string, set 'exists' flag and break
  239.             else { if(exists) *exists = YES; break; }
  240.         }
  241.  
  242.         // If last guess was right or too high index is m; if too low then m+1;
  243.         if(guess>=0) index = m; else index = m+1;
  244.     }
  245.  
  246.     return index;
  247. }
  248.  
  249.  
  250. /******************************************************************************
  251.     removeString:(const char *)string
  252.     removeStrings:(const char *const*)strings
  253.     removeStringAt:(int)at
  254.  
  255.     These methods allow for the removal of strings by value or index. Returns self.
  256. ******************************************************************************/
  257. - removeString:(const char *)string
  258. {
  259.     BOOL exists;
  260.     int index = [self indexOfString:string exists:&exists];
  261.     if(exists) [self removeStringAt:index];
  262.     return self;
  263. }
  264. - removeStrings:(const char *const*)strings
  265. {
  266.     int i;
  267.     for(i=0; i<[self count]; i++) [self removeString:strings[i]];
  268.     return self;
  269. }
  270. - (char *)removeStringAt:(int)at
  271. {
  272.     char *string = (char *)[self stringAt:at];
  273.     [self removeElementAt:at];
  274.     if(![self count]) isSorted = YES;
  275.     return string;
  276. }
  277.  
  278. /******************************************************************************
  279.     (BOOL)isSorted
  280.     sortStrings:sender
  281.     
  282.     isSorted returns whether or not the list is currently considered to be sorted. Lists are set to be sorted by default and when empty. isSorted is set to NO when a string is added without the 'sorted' flag.
  283.     sortStrings: sorts the stringList (ignoring case) and sets the sorted flag. Returns self.
  284. ******************************************************************************/
  285. - (BOOL)isSorted    { return isSorted; }
  286.  
  287. // Wrap around strcasecmp to accept 'char **' and NULL strings(NULLs to back)
  288. static int strPtrCaseCmp(const void *s1, const void *s2)
  289. {
  290.     if(*(char **)s1==*(char **)s2) return 0;
  291.     else if(!*(char **)s1) return 1; else if(!*(char **)s2) return -1;
  292.     else return strcasecmp(*((char **)s1),*((char **)s2));
  293. }
  294.  
  295. - sortStrings:sender
  296. {
  297.     qsort(dataPtr, [self count], sizeof(char *), strPtrCaseCmp); 
  298.     isSorted = YES;
  299.     return self;
  300. }
  301.  
  302. /******************************************************************************
  303.     Write and read the StringList for archiving.
  304. ******************************************************************************/
  305. - write:(NXTypedStream *)stream
  306. {    
  307.     [super write:stream];
  308.     NXWriteType(stream, "c", &isSorted);
  309.     return self;
  310. }
  311.  
  312. - read:(NXTypedStream *)stream
  313. {
  314.     [super read:stream];
  315.     NXReadType(stream, "c", &isSorted);
  316.     return self;
  317. }
  318.  
  319. /******************************************************************************
  320.     empty, freeStrings, free
  321.     
  322.     These methods empty the list, free the strings and free the list, respectively.
  323. ******************************************************************************/
  324. - empty { isSorted = YES; return [super empty]; }
  325.  
  326. - freeStrings
  327. {
  328.     while([self count]) free([self removeStringAt:0]); 
  329.     return self;
  330. }
  331.  
  332. - free
  333.     return [super free];
  334. }
  335.  
  336. /******************************************************************************
  337.     (int)browser:sender fillMatrix:matrix inColumn:(int)column
  338.     
  339.     This method is provided as a delegate method for browser to quickly display string list.
  340. ******************************************************************************/
  341. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  342. {
  343.     int   i;
  344.     id   cellList, theCell;
  345.   
  346.     // Set matrix to have the right number of cells.
  347.     [matrix renewRows:[self count] cols:1];
  348.  
  349.     // Get list of cells from the matrix.
  350.     cellList = [matrix cellList];
  351.  
  352.     // For each cell set its value, set whether it is a leaf or not and 
  353.     //   mark it loaded.
  354.     for(i=0;i<[cellList count];i++) {
  355.         theCell = [cellList objectAt:i];
  356.         [theCell setStringValue:[self stringAt:i]];
  357.         [theCell setLeaf:YES];
  358.         [theCell setLoaded:YES];
  359.     }
  360.  
  361.     // Return the number of rows.
  362.     return [self count];
  363. }
  364.  
  365. // Interface Builder support
  366. - (const char *)getInspectorClassName { return "SLInspector"; }
  367.  
  368. @end
  369.  
  370. // Fixed indexOf: bug. Thanks Stefanie Herzer!