Happy new year! Bump copyright.
[macgdbp.git] / Source / NSXMLElementAdditions.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2009, 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 (![self isLeaf] && [children count] < 1)
54 {
55 return [[[(AppDelegate *)[NSApp delegate] debugger] connection] getProperty:[self fullname]];
56 }
57 return children;
58 }
59
60 /**
61 * Returns the value of the property
62 */
63 - (NSString *)value
64 {
65 // not a leaf, so don't display any value
66 if (![self isLeaf])
67 {
68 return @"...";
69 }
70
71 // base64 encoded data
72 if ([[[self attributeForName:@"encoding"] stringValue] isEqualToString:@"base64"])
73 {
74 char *str = (char *)[[self stringValue] cStringUsingEncoding:NSASCIIStringEncoding];
75 int strlen = [[self stringValue] lengthOfBytesUsingEncoding:NSASCIIStringEncoding];
76
77 char *data;
78 size_t datalen;
79
80 if (!base64_decode_alloc(str, strlen, &data, &datalen))
81 {
82 NSLog(@"error in converting %@ to base64", self);
83 }
84
85 NSString *ret = [NSString stringWithCString:data length:datalen];
86 free(data);
87
88 return ret;
89 }
90
91 // just a normal string
92 return [self stringValue];
93 }
94
95 /**
96 * Returns the type of variable this is
97 */
98 - (NSString *)type
99 {
100 NSXMLNode *className = [self attributeForName:@"classname"];
101 NSString *type = [[self attributeForName:@"type"] stringValue];
102 if (className != nil)
103 {
104 return [NSString stringWithFormat:@"%@ (%@)", [className stringValue], type];
105 }
106 return type;
107 }
108
109 @end