forked from nygard/class-dump
/
CDSymbolReferences.m
172 lines (133 loc) · 4.13 KB
/
CDSymbolReferences.m
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// -*- mode: ObjC -*-
// This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
// Copyright (C) 1997-1998, 2000-2001, 2004-2010 Steve Nygard.
#import "CDSymbolReferences.h"
@implementation CDSymbolReferences
- (id)init;
{
if ([super init] == nil)
return nil;
frameworkNamesByClassName = nil;
frameworkNamesByProtocolName = nil;
classes = [[NSMutableSet alloc] init];
protocols = [[NSMutableSet alloc] init];
return self;
}
- (void)dealloc;
{
[frameworkNamesByClassName release];
[frameworkNamesByProtocolName release];
[classes release];
[protocols release];
[super dealloc];
}
- (void)setFrameworkNamesByClassName:(NSDictionary *)newValue;
{
if (newValue == frameworkNamesByClassName)
return;
[frameworkNamesByClassName release];
frameworkNamesByClassName = [newValue retain];
}
- (void)setFrameworkNamesByProtocolName:(NSDictionary *)newValue;
{
if (newValue == frameworkNamesByProtocolName)
return;
[frameworkNamesByProtocolName release];
frameworkNamesByProtocolName = [newValue retain];
}
- (NSString *)frameworkForClassName:(NSString *)aClassName;
{
return [frameworkNamesByClassName objectForKey:aClassName];
}
- (NSString *)frameworkForProtocolName:(NSString *)aProtocolName;
{
return [frameworkNamesByProtocolName objectForKey:aProtocolName];
}
- (NSArray *)classes;
{
return [[classes allObjects] sortedArrayUsingSelector:@selector(compare:)];
}
- (void)addClassName:(NSString *)aClassName;
{
[classes addObject:aClassName];
}
- (void)removeClassName:(NSString *)aClassName;
{
if (aClassName != nil)
[classes removeObject:aClassName];
}
- (NSArray *)protocols;
{
return [[protocols allObjects] sortedArrayUsingSelector:@selector(compare:)];
}
- (void)addProtocolName:(NSString *)aProtocolName;
{
[protocols addObject:aProtocolName];
}
- (void)addProtocolNamesFromArray:(NSArray *)protocolNames;
{
[protocols addObjectsFromArray:protocolNames];
}
- (NSString *)description;
{
return [NSString stringWithFormat:@"<%@:%p> frameworkNamesByClassName: %@, frameworkNamesByProtocolName: %@, classes: %@, protocols: %@",
NSStringFromClass([self class]), self,
frameworkNamesByClassName, frameworkNamesByProtocolName,
[self classes], [self protocols]];
}
- (void)_appendToString:(NSMutableString *)resultString;
{
NSArray *names;
names = [self protocols];
for (NSString *name in names) {
NSString *str;
str = [self importStringForProtocolName:name];
if (str != nil)
[resultString appendString:str];
}
if ([names count] > 0)
[resultString appendString:@"\n"];
names = [self classes];
if ([names count] > 0) {
[resultString appendFormat:@"@class %@;\n\n", [names componentsJoinedByString:@", "]];
}
}
- (NSString *)referenceString;
{
NSMutableString *referenceString;
referenceString = [[[NSMutableString alloc] init] autorelease];
[self _appendToString:referenceString];
if ([referenceString length] == 0)
return nil;
return referenceString;
}
- (void)removeAllReferences;
{
[classes removeAllObjects];
[protocols removeAllObjects];
}
- (NSString *)importStringForClassName:(NSString *)aClassName;
{
if (aClassName != nil) {
NSString *framework;
framework = [self frameworkForClassName:aClassName];
if (framework == nil)
return [NSString stringWithFormat:@"#import \"%@.h\"\n", aClassName];
else
return [NSString stringWithFormat:@"#import <%@/%@.h>\n", framework, aClassName];
}
return nil;
}
- (NSString *)importStringForProtocolName:(NSString *)aProtocolName;
{
if (aProtocolName != nil) {
NSString *framework;
framework = [self frameworkForClassName:aProtocolName];
if (framework == nil)
return [NSString stringWithFormat:@"#import \"%@-Protocol.h\"\n", aProtocolName];
else
return [NSString stringWithFormat:@"#import <%@/%@-Protocol.h>\n", framework, aProtocolName];
}
return nil;
}
@end