Adding support for base64 encoded string values
[macgdbp.git] / Source / NSXMLElementAdditions.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2002 - 2007, 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 BOOL leaf = ([[[self attributeForName: @"children"] stringValue] intValue] == 0);
36
37 // hmm... we're not a leaf but have no children. this must be beyond our depth, so go make us
38 // deeper
39 if (!leaf && [[self children] count] < 1)
40 {
41 // count upwards to see how deep we should go
42 int depth = 0;
43 NSXMLElement *elm = self;
44 while (elm != nil)
45 {
46 depth++;
47 elm = (NSXMLElement *)[elm parent];
48 }
49 NSLog(@"let's go to depth %d for %@", depth, self);
50 }
51
52 return leaf;
53 }
54
55 /**
56 * Returns the value of the property
57 */
58 - (NSString *)value
59 {
60 // not a leaf, so don't display any value
61 if (![self isLeaf])
62 {
63 return @"...";
64 }
65
66 // base64 encoded data
67 if ([[[self attributeForName: @"encoding"] stringValue] isEqualToString: @"base64"])
68 {
69 char *str = (char *)[[self stringValue] cStringUsingEncoding: NSASCIIStringEncoding];
70 int strlen = [[self stringValue] lengthOfBytesUsingEncoding: NSASCIIStringEncoding];
71
72 char *data;
73 size_t datalen;
74
75 if (!base64_decode_alloc(str, strlen, &data, &datalen))
76 {
77 NSLog(@"error in converting %@ to base64", self);
78 }
79
80 return [NSString stringWithCString: data length: datalen];
81 }
82
83 // just a normal string
84 return [self stringValue];
85 }
86
87 /**
88 * Returns the type of variable this is
89 */
90 - (NSString *)type
91 {
92 return [[self attributeForName: @"type"] stringValue];
93 }
94
95 @end