Bump project version to 212.1.
[macgdbp.git] / Source / VariableNode.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2011, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "VariableNode.h"
18
19 #include "NSXMLElementAdditions.h"
20
21 @implementation VariableNode {
22 NSMutableArray* _children;
23 NSString* _nodeValue;
24 }
25
26 - (id)initWithXMLNode:(NSXMLElement*)node {
27 if (self = [super init]) {
28 _name = [[[node attributeForName:@"name"] stringValue] copy];
29 _fullName = [[[node attributeForName:@"fullname"] stringValue] copy];
30 _className = [[[node attributeForName:@"classname"] stringValue] copy];
31 _type = [[[node attributeForName:@"type"] stringValue] copy];
32 _nodeValue = [[node base64DecodedValue] copy];
33 _children = [[NSMutableArray alloc] init];
34 if ([node children]) {
35 [self setChildrenFromXMLChildren:[node children]];
36 }
37 _childCount = [[[node attributeForName:@"numchildren"] stringValue] integerValue];
38 _address = [[[node attributeForName:@"address"] stringValue] copy];
39 }
40 return self;
41 }
42
43 - (void)setChildrenFromXMLChildren:(NSArray*)children {
44 [self willChangeValueForKey:@"children"];
45
46 [_children removeAllObjects];
47
48 for (NSXMLNode* child in children) {
49 // Other child nodes may be the string value.
50 if ([child isKindOfClass:[NSXMLElement class]]) {
51 VariableNode* node = [[VariableNode alloc] initWithXMLNode:(NSXMLElement*)child];
52 // Don't include the CLASSNAME property as that information is retrieved
53 // elsewhere.
54 if (![node.name isEqualToString:@"CLASSNAME"])
55 [_children addObject:node];
56 }
57 }
58
59 [self didChangeValueForKey:@"children"];
60 }
61
62 - (BOOL)isLeaf {
63 return self.childCount == 0;
64 }
65
66 - (NSString*)displayType {
67 if (self.className != nil) {
68 return [NSString stringWithFormat:@"%@ (%@)", self.className, self.type];
69 }
70 return self.type;
71 }
72
73 - (NSString*)description {
74 return [NSString stringWithFormat:@"<VariableNode %p : %@>", self, self.fullName];
75 }
76
77 - (NSString*)value {
78 if (!self.isLeaf) {
79 if (self.childCount != self.children.count) {
80 return @"…";
81 }
82 // For non-leaf nodes, display the object structure by recursively printing
83 // the base64-decoded values.
84 NSMutableString* mutableString = [[NSMutableString alloc] initWithString:@"(\n"];
85 for (VariableNode* child in self.children) {
86 [self recusivelyFormatNode:child appendTo:mutableString depth:1];
87 }
88 [mutableString appendString:@")"];
89
90 return mutableString;
91 }
92
93 return _nodeValue;
94 }
95
96 ////////////////////////////////////////////////////////////////////////////////
97 #pragma mark Private
98
99 /**
100 * Recursively builds a print_r()-style output by attaching the data to
101 * |stringBuilder| with indent level specified by |depth|.
102 */
103 - (void)recusivelyFormatNode:(VariableNode*)node
104 appendTo:(NSMutableString*)stringBuilder
105 depth:(NSUInteger)depth
106 {
107 // Create the indention string for this level.
108 NSString* indent = [@"" stringByPaddingToLength:depth withString:@"\t" startingAtIndex:0];
109
110 if (node.isLeaf) {
111 // If this is a leaf node, simply append the key=>value pair.
112 [stringBuilder appendFormat:@"%@%@\t=>\t%@\n", indent, node.name, node->_nodeValue];
113 } else {
114 // If this node has children, increase the depth and recurse.
115 [stringBuilder appendFormat:@"%@%@\t=>\t(\n", indent, node.name];
116 for (VariableNode* child in node.children) {
117 [self recusivelyFormatNode:child appendTo:stringBuilder depth:depth + 1];
118 }
119 [stringBuilder appendFormat:@"%@)\n", indent];
120 }
121 }
122
123 @end