Happy new year!
[macgdbp.git] / Source / NSXMLElementAdditions.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2008, 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
20 @implementation NSXMLElement (NSXMLElementAdditions)
21
22 /**
23 * Return's the property's name from the attributes list
24 */
25 - (NSString *)variable
26 {
27 return [[self attributeForName:@"name"] stringValue];
28 }
29
30 /**
31 * Returns whether or not this node has any children
32 */
33 - (BOOL)isLeaf
34 {
35 return ([[[self attributeForName:@"children"] stringValue] intValue] == 0);
36 }
37
38 /**
39 * Returns the value of the property
40 */
41 - (NSString *)value
42 {
43 // not a leaf, so don't display any value
44 if (![self isLeaf])
45 {
46 return @"...";
47 }
48
49 // base64 encoded data
50 if ([[[self attributeForName:@"encoding"] stringValue] isEqualToString:@"base64"])
51 {
52 char *str = (char *)[[self stringValue] cStringUsingEncoding:NSASCIIStringEncoding];
53 int strlen = [[self stringValue] lengthOfBytesUsingEncoding:NSASCIIStringEncoding];
54
55 char *data;
56 size_t datalen;
57
58 if (!base64_decode_alloc(str, strlen, &data, &datalen))
59 {
60 NSLog(@"error in converting %@ to base64", self);
61 }
62
63 return [NSString stringWithCString:data length:datalen];
64 }
65
66 // just a normal string
67 return [self stringValue];
68 }
69
70 /**
71 * Returns the type of variable this is
72 */
73 - (NSString *)type
74 {
75 return [[self attributeForName:@"type"] stringValue];
76 }
77
78 @end