163
|
1 //
|
|
2 // Tree.h -- a generic class to build tree data structures
|
|
3 // This class requires the String class, also by Don Yacktman.
|
|
4 // Written by Don Yacktman (c) 1993 by Don Yacktman.
|
|
5 // All rights reserved.
|
|
6 //
|
|
7 // Subclasses should be designed to hold more data than just children and
|
|
8 // a String-based label...That's where the usefulness of the class
|
|
9 // becomes apparent. By using a list, any number of children is ok.
|
|
10 //
|
|
11 // You may use and copy this class freely as long as you
|
|
12 // comply with the following terms:
|
|
13 // (1) If you use this class in an application which you
|
|
14 // intend to sell commercially, as shareware, or otherwise,
|
|
15 // you may only do so with express written permission
|
|
16 // of the author. Use in applications which will
|
|
17 // be distributed free of charge is encouraged.
|
|
18 // (2) You must include the source code to this object and
|
|
19 // all accompanying documentation with your application,
|
|
20 // or provide it to users if requested, free of charge.
|
|
21 // (3) Do not remove the author's name or any of the
|
|
22 // copyright notices
|
|
23 //
|
|
24
|
|
25 #import <appkit/appkit.h> // superclass is in there
|
|
26 #import <stdio.h>
|
|
27 #import "String.h"
|
|
28
|
|
29 @interface Tree:Object
|
|
30 {
|
|
31 id branches; // an instance of the list class
|
|
32 id label; // node name
|
|
33 id value; // must be a string
|
|
34 BOOL notCollapsed; // print children when dumping if true.
|
|
35 }
|
|
36
|
|
37
|
|
38 // init with null label
|
|
39 - init;
|
|
40
|
|
41 // designated initializer
|
|
42 - initLabel:(const char *)newLabel; // send a char* string
|
|
43 - initLabelString:string; // send a String object
|
|
44
|
|
45 // access to the label of this node
|
|
46 - setLabel:(const char *)newLabel; // send a char* string
|
|
47 - (const char *)label;
|
|
48
|
|
49 // access to the value of this node
|
|
50 - setValue:newValue; // send a String object
|
|
51 - (const char *)value;
|
|
52
|
|
53 // clean up our mess
|
|
54 - free;
|
|
55
|
|
56 // add a new child node
|
|
57 - addBranch:child;
|
|
58
|
|
59 // Print the tree to a stream (file, whatever). Call the root with level
|
|
60 // set to zero, and set the indent string however you like; the indent
|
|
61 // string should be something like " " or "\t" to show how to indent to
|
|
62 // the next level. This method recursively traverses the tree's children.
|
|
63 - dumpTree:(NXStream *)file level:(int)lev indent:(const char *)ind;
|
|
64
|
|
65 // set whether or not we print the children (we don't if collapsed)
|
|
66 // when dumping. This does NOT affect the tree's width or depth!
|
|
67 - collapse;
|
|
68 - uncollapse;
|
|
69 - (BOOL)collapsed;
|
|
70
|
|
71 // when dumping the tree, if you want to add extra data to the output
|
|
72 // before the newline and before children are printed, add it here. If
|
|
73 // you return NO, then the children won't be printed; this is how the
|
|
74 // collapse stuff works.
|
|
75 - (BOOL)moreData:(NXStream *)file level:(int)lev indent:(const char *)ind;
|
|
76
|
|
77 // How deep or wide is the tree?
|
|
78 - (int) width;
|
|
79 - (int) depth;
|
|
80
|
|
81 // Return the List object that contains ids of all the kids.
|
|
82 - branches;
|
|
83
|
|
84 @end
|