Stop crashing with EXC_BAD_ACCESS when expanding properties beyond the fetched depth.
[macgdbp.git] / Source / NSXMLElementAdditions.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2010, 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 <Cocoa/Cocoa.h>
18 #include "base64.h"
19 #import "AppDelegate.h"
20
21 @implementation NSXMLElement (GDBpAdditions)
22
23 /**
24 * Return's the property's full name
25 */
26 - (NSString*)fullname
27 {
28 return [[self attributeForName:@"fullname"] stringValue];
29 }
30
31 /**
32 * Return's the property's name from the attributes list
33 */
34 - (NSString*)variable
35 {
36 return [[self attributeForName:@"name"] stringValue];
37 }
38
39 /**
40 * Returns whether or not this node has any children
41 */
42 - (BOOL)isLeaf
43 {
44 return ([[[self attributeForName:@"children"] stringValue] intValue] == 0);
45 }
46
47 /**
48 * Override children so we can fetch more depth as needed
49 */
50 - (NSArray*)subnodes
51 {
52 NSArray* children = [self children];
53 // If this node has children but they haven't been loaded from the backend,
54 // request them asynchronously.
55 if (![self isLeaf] && [children count] < 1) {
56 [[[(AppDelegate*)[NSApp delegate] debugger] connection] getProperty:[self fullname]];
57 }
58 return children;
59 }
60
61 /**
62 * Returns the value of the property
63 */
64 - (NSString*)value
65 {
66 // not a leaf, so don't display any value
67 if (![self isLeaf])
68 {
69 return @"...";
70 }
71
72 // base64 encoded data
73 if ([[[self attributeForName:@"encoding"] stringValue] isEqualToString:@"base64"])
74 {
75 const char* str = [[self stringValue] UTF8String];
76 int strlen = [[self stringValue] length];
77
78 char* data;
79 size_t datalen;
80
81 if (!base64_decode_alloc(str, strlen, &data, &datalen))
82 NSLog(@"error in converting %@ from base64", self);
83
84 NSString* ret = nil;
85 if (data)
86 {
87 ret = [NSString stringWithUTF8String:data];
88 free(data);
89 }
90
91 return ret;
92 }
93
94 // just a normal string
95 return [self stringValue];
96 }
97
98 /**
99 * Returns the type of variable this is
100 */
101 - (NSString*)type
102 {
103 NSXMLNode* className = [self attributeForName:@"classname"];
104 NSString* type = [[self attributeForName:@"type"] stringValue];
105 if (className != nil)
106 {
107 return [NSString stringWithFormat:@"%@ (%@)", [className stringValue], type];
108 }
109 return type;
110 }
111
112 @end