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