-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathobjectrenderer.m
More file actions
143 lines (123 loc) · 4.81 KB
/
objectrenderer.m
File metadata and controls
143 lines (123 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// formatter.m
// appscript
//
#import "objectrenderer.h"
static NSUInteger dataDescriptionTruncation = (NSUInteger)-1;
@implementation AEMObjectRenderer
+(NSString *)formatOSType:(OSType)code {
NSMutableString *str;
char c;
int i;
code = CFSwapInt32HostToBig(code);
str = [NSMutableString stringWithCapacity: 16];
for (i = 0; i < sizeof(code); i++) {
c = ((char*)(&code))[i];
if (c < 32 || c > 126 || c == '\\' || c == '\'')
[str appendFormat: @"\\x%02x", c];
else
[str appendFormat: @"%c", c];
}
return str;
}
+(void)formatObject:(id)obj indent:(NSString *)indent result:(NSMutableString *)result {
NSEnumerator *n;
id obj2, obj3;
NSMutableString *tmp;
NSString *nextIndent = [NSString stringWithFormat: @"\t%@", indent];
NSNumber *num;
[result appendString: indent];
if ([obj isKindOfClass: [NSArray class]]) {
n = [(NSArray *)obj objectEnumerator];
[result appendString: @"[NSArray arrayWithObjects:\n"];
while (obj2 = [n nextObject]) {
[self formatObject: obj2 indent: nextIndent result: result];
[result appendString: @",\n"];
}
[result appendFormat: @"%@nil\n%@]", nextIndent, indent];
} else if ([obj isKindOfClass: [NSDictionary class]]) {
n = [(NSDictionary *)obj keyEnumerator];
[result appendString: @"[NSDictionary dictionaryWithObjectsAndKeys:\n"];
while (obj2 = [n nextObject]) {
obj3 = [(NSDictionary *)obj objectForKey: obj2];
[self formatObject: obj3 indent: nextIndent result: result];
[result appendString: @", "];
[self formatObject: obj2 indent: @"" result: result];
[result appendString: @",\n"];
}
[result appendFormat: @"%@nil\n%@]", nextIndent, indent];
} else if ([obj isKindOfClass: [NSString class]]) {
tmp = [[NSMutableString alloc] initWithString: (NSString *)obj];
[tmp replaceOccurrencesOfString: @"\\" withString: @"\\\\" options: 0 range: NSMakeRange(0, [tmp length])];
[tmp replaceOccurrencesOfString: @"\"" withString: @"\\\"" options: 0 range: NSMakeRange(0, [tmp length])];
[tmp replaceOccurrencesOfString: @"\r" withString: @"\\r" options: 0 range: NSMakeRange(0, [tmp length])];
[tmp replaceOccurrencesOfString: @"\n" withString: @"\\n" options: 0 range: NSMakeRange(0, [tmp length])];
[tmp replaceOccurrencesOfString: @"\t" withString: @"\\t" options: 0 range: NSMakeRange(0, [tmp length])];
[result appendFormat: @"@\"%@\"", tmp];
[tmp release];
} else if ([obj isKindOfClass: [NSNumber class]]) {
num = (NSNumber *)obj;
switch (*[num objCType]) {
case 'b':
case 'c':
case 'C':
case 's':
case 'S':
case 'i':
case 'l':
[result appendFormat: @"[NSNumber numberWithInt: %@]", num];
break;
case 'I':
case 'L':
[result appendFormat: @"[NSNumber numberWithUnsignedInt: %@]", num];
break;
case 'q':
[result appendFormat: @"[NSNumber numberWithLongLong: %@]", num];
break;
case 'Q':
[result appendFormat: @"[NSNumber numberWithUnsignedLongLong: %@]", num];
break;
default: // f, d
[result appendFormat: @"[NSNumber numberWithDouble: %@]", num];
}
} else if ([obj isKindOfClass: [NSDate class]]) {
[result appendString: @"[NSDate dateWithString: "];
[self formatObject: [obj description] indent: @"" result: result];
[result appendString: @"]"];
} else if ([obj isKindOfClass: [NSURL class]]) {
[result appendString: @"[NSURL URLWithString: "];
[self formatObject: [obj description] indent: @"" result: result];
[result appendString: @"]"];
} else if ([obj isKindOfClass:[NSAppleEventDescriptor class]]) {
NSString *s = [obj description];
if ([s hasPrefix:@"<NSAppleEventDescriptor:"]) {
NSArray *bits = [s componentsSeparatedByString:@"$"];
if ([bits count] == 3) {
NSString *d = [bits objectAtIndex:1];
NSUInteger length = [d length];
if (length / 2 > dataDescriptionTruncation) {
s = [NSString stringWithFormat:@"%@%d:$%@...$%@",
[bits objectAtIndex:0],
(int)length / 2,
[d substringToIndex:dataDescriptionTruncation * 2],
[bits objectAtIndex:2]];
}
}
}
[result appendString:s];
} else {
[result appendFormat:@"%@", obj];
}
}
+(NSString *)formatObject:(id)obj {
NSString *result;
NSMutableString *collector = [[NSMutableString alloc] init];
[self formatObject: obj indent: @"" result: collector];
result = [NSString stringWithString: collector];
[collector release];
return result;
}
+(void)setDataDescriptionTruncation:(NSUInteger)maxBytes {
dataDescriptionTruncation = maxBytes;
}
@end