Fixing a memory leak
[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 full name
24 */
25 - (NSString *)fullname
26 {
27 return [[self attributeForName:@"fullname"] stringValue];
28 }
29
30 /**
31 * Return's the property's name from the attributes list
32 */
33 - (NSString *)variable
34 {
35 return [[self attributeForName:@"name"] stringValue];
36 }
37
38 /**
39 * Returns whether or not this node has any children
40 */
41 - (BOOL)isLeaf
42 {
43 return ([[[self attributeForName:@"children"] stringValue] intValue] == 0);
44 }
45
46 /**
47 * Returns the value of the property
48 */
49 - (NSString *)value
50 {
51 // not a leaf, so don't display any value
52 if (![self isLeaf])
53 {
54 return @"...";
55 }
56
57 // base64 encoded data
58 if ([[[self attributeForName:@"encoding"] stringValue] isEqualToString:@"base64"])
59 {
60 char *str = (char *)[[self stringValue] cStringUsingEncoding:NSASCIIStringEncoding];
61 int strlen = [[self stringValue] lengthOfBytesUsingEncoding:NSASCIIStringEncoding];
62
63 char *data;
64 size_t datalen;
65
66 if (!base64_decode_alloc(str, strlen, &data, &datalen))
67 {
68 NSLog(@"error in converting %@ to base64", self);
69 }
70
71 NSString *ret = [NSString stringWithCString:data length:datalen];
72 free(data);
73
74 return ret;
75 }
76
77 // just a normal string
78 return [self stringValue];
79 }
80
81 /**
82 * Returns the type of variable this is
83 */
84 - (NSString *)type
85 {
86 return [[self attributeForName:@"type"] stringValue];
87 }
88
89 @end