Introduce DebuggerModel, which will be updated by the BackEnd.
[macgdbp.git] / Source / NSXMLElementAdditions.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, 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
19 #import "AppDelegate.h"
20 #include "modp_b64.h"
21
22 @interface NSXMLElement (GDBpAdditions_Private)
23 - (NSString*)internalName;
24 - (NSString*)internalBase64DecodedValue;
25 - (void)recursiveBase64DecodedValue:(NSMutableString*)stringBuilder
26 depth:(NSUInteger)depth;
27 @end
28
29 @implementation NSXMLElement (GDBpAdditions)
30
31 /**
32 * Returns whether or not this node has any children
33 */
34 - (BOOL)isLeaf
35 {
36 return ([[[self attributeForName:@"children"] stringValue] intValue] == 0);
37 }
38
39 /**
40 * Returns the "name" attribute.
41 */
42 - (NSString*)internalName
43 {
44 return [[self attributeForName:@"name"] stringValue];
45 }
46
47 /**
48 * Does the actual work of decoding base64.
49 */
50 - (NSString*)internalBase64DecodedValue
51 {
52 // The value of the node is base64 encoded.
53 if ([[[self attributeForName:@"encoding"] stringValue] isEqualToString:@"base64"]) {
54 const char* src = [[self stringValue] UTF8String];
55 NSUInteger srclen = [[self stringValue] length];
56
57 int destlen = modp_b64_decode_len(srclen);
58 char* dest = malloc(destlen);
59 memset(dest, 0, destlen);
60
61 modp_b64_decode(dest, src, srclen);
62
63 NSString* ret = nil;
64 if (dest) {
65 ret = [NSString stringWithUTF8String:dest];
66 free(dest);
67 }
68 return ret;
69 }
70
71 // The value is just a normal string.
72 return [self stringValue];
73 }
74
75 /**
76 * Returns the value of the property
77 */
78 - (NSString*)base64DecodedValue
79 {
80 if (![self isLeaf]) {
81 // For non-leaf nodes, display the object structure by recursively printing
82 // the base64-decoded values.
83 NSMutableString* mutableString = [[NSMutableString alloc] initWithString:@"(\n"];
84 [self recursiveBase64DecodedValue:mutableString depth:1];
85 [mutableString appendString:@")"];
86 return [mutableString autorelease];
87 }
88
89 return [self internalBase64DecodedValue];
90 }
91
92 /**
93 * Recursively builds a print_r()-style output by attaching the data to
94 * |stringBuilder| with indent level specified by |depth|.
95 */
96 - (void)recursiveBase64DecodedValue:(NSMutableString*)stringBuilder
97 depth:(NSUInteger)depth
98 {
99 // Create the indention string for this level.
100 NSString* indent = [@"" stringByPaddingToLength:depth withString:@"\t" startingAtIndex:0];
101
102 if ([self isLeaf]) {
103 // If this is a leaf node, simply append the key=>value pair.
104 [stringBuilder appendFormat:@"%@%@\t=>\t%@\n", indent, [self internalName], [self internalBase64DecodedValue]];
105 } else {
106 // If this node has children, increase the depth and recurse.
107 [stringBuilder appendFormat:@"%@%@\t=>\t(\n", indent, [self internalName]];
108 for (NSXMLElement* elm in [self children]) {
109 [elm recursiveBase64DecodedValue:stringBuilder depth:depth + 1];
110 }
111 [stringBuilder appendFormat:@"%@)\n", indent];
112 }
113 }
114
115 @end