home *** CD-ROM | disk | FTP | other *** search
- //
- // MiscTree.m -- a generic class to build tree data structures
- // Written by Don Yacktman (c) 1993 by Don Yacktman.
- // Version 1.0. All rights reserved.
- //
- // This notice may not be removed from this source code.
- //
- // This is a free object! Contact the author for the latest version.
- // Don Yacktman, 4279 N. Ivy Lane, Provo, UT, 84604
- // e-mail: Don_Yacktman@byu.edu
- //
- // 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.
- //
-
- #import <misckit/MiscTree.h>
- #import <misckit/MiscString.h>
- #import <stdio.h>
-
- @implementation MiscTree
-
- - init // special init for this class
- {
- [self initLabel:""];
- return self;
- }
-
- - initLabel:(const char *)newLabel
- {
- [super init];
- notCollapsed = YES;
- // set up a list class to hold pointers to other Tree objects (branches).
- branches = [[List alloc] init]; // make a dynamic-sized array of objects
- label = [[MiscString alloc] init]; // make a string for the label
- [label setString:newLabel];
- return self;
- }
-
- - initLabelString:string // send in a String object
- {
- [self init];
- if (label) [label free];
- label = string;
- return self;
- }
-
- - setLabel:(const char *)newLabel
- {
- [label setString:newLabel];
- return self;
- }
-
- - setValue:newValue
- {
- if (value) [value free];
- value = newValue;
- return self;
- }
-
- - free // clean up our mess
- {
- [branches freeObjects]; // free the branches we're responsible for
- // (I assumed that each branch only has one parent...if that's not
- // the case, then the above line should be axed.)
- [branches free]; // get rid of the List object
- return [super free];
- }
-
- - addBranch:child // add a new child node
- { // add to the end of the "branches" List object
- return [branches addObject:child];
- }
-
- - dumpTree:(NXStream *)file level:(int)lev indent:(const char *)ind
- {
- int i; BOOL doKids;
-
- for (i=0; i<lev; i++) NXPrintf(file, "%s", ind); // indent
- NXPrintf(file, "%s", [label stringValue]); // and print label
- doKids = [self moreData:file level:lev indent:ind];
- NXPrintf(file, "\n"); // print newline
- if (doKids)
- for (i=0; i<[branches count]; i++) // then do children
- [[branches objectAt:i]
- dumpTree:file level:(lev + 1) indent:ind];
- return self;
- }
-
- - (BOOL)moreData:(NXStream *)file level:(int)lev indent:(const char *)ind
- { // Subclass responsibility -- you can dynamically control collapsing
- // (for example, cut off at a certain level, etc.) and also add info
- // to the end of a dumped node's line from here. Be sure to message
- // super when you override this method; if this method returns a NO
- // then you should return a NO, regardless. Don't just use the
- // notCollapsed instance var, since it may change in the future; look
- // at the return value from super!
- //
- // Here's how you might override to keep from printing levels deeper
- // than level 2 (remember that the root level is zero):
- //
- // - (BOOL)moreData:(NXStream *)file level:(int)lev indent:(const char *)ind
- // {
- // if ((lev > 2) || ![super moreData:file level:lev indent:ind])
- // return NO;
- // return YES;
- // }
- //
- return notCollapsed;
- }
-
- - (int)width
- { // This is recursive, as you will note.
- int num = [branches count];
- int i, count = 0;
-
- if (!num) return 1; // No children, so we're only one node wide...
-
- // have kids, so sum up their widths.
- for (i=0; i<num; i++) count += [[branches objectAt:i] width];
- return count;
- }
-
- - (int)depth
- { // This is recursive, as you will note.
- int num = [branches count];
- int i, temp, deepest = 1;
-
- if (!num) return 1; // No children, so only one node deep
-
- // have kids, so see which branch is deepest.
- for (i=0; i<num; i++) {
- temp = [[branches objectAt:i] depth];
- if (temp > deepest) deepest = temp;
- }
- return (deepest + 1); // we are one deeper than the deepest child.
- }
-
- - branches { return branches; }
- - collapse { notCollapsed = NO; return self; }
- - uncollapse { notCollapsed = YES; return self; }
- - (BOOL)collapsed { return !notCollapsed; }
- - (const char *)label { return [label stringValue]; }
- - (const char *)value { return [value stringValue]; }
-
- @end
-