forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDMultiFileVisitor.m
238 lines (182 loc) · 6.67 KB
/
CDMultiFileVisitor.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// -*- 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-2009 Steve Nygard.
#import "CDMultiFileVisitor.h"
#import "NSArray-Extensions.h"
#import "CDClassDump.h"
#import "CDClassFrameworkVisitor.h"
#import "CDSymbolReferences.h"
#import "CDOCClass.h"
#import "CDOCProtocol.h"
#import "CDOCIvar.h"
@implementation CDMultiFileVisitor
- (id)init;
{
if ([super init] == nil)
return nil;
outputPath = nil;
return self;
}
- (void)dealloc;
{
[outputPath release];
[super dealloc];
}
- (NSString *)outputPath;
{
return outputPath;
}
- (void)setOutputPath:(NSString *)newOutputPath;
{
if (newOutputPath == outputPath)
return;
[outputPath release];
outputPath = [newOutputPath retain];
}
- (void)createOutputPathIfNecessary;
{
if (outputPath != nil) {
NSFileManager *fileManager;
BOOL isDirectory;
fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath isDirectory:&isDirectory] == NO) {
BOOL result;
result = [fileManager createDirectoryAtPath:outputPath attributes:nil];
if (result == NO) {
NSLog(@"Error: Couldn't create output directory: %@", outputPath);
return;
}
} else if (isDirectory == NO) {
NSLog(@"Error: File exists at output path: %@", outputPath);
return;
}
}
}
- (void)buildClassFrameworks;
{
CDClassFrameworkVisitor *visitor;
visitor = [[CDClassFrameworkVisitor alloc] init];
[visitor setClassDump:classDump];
[classDump recursivelyVisit:visitor];
[symbolReferences setFrameworkNamesByClassName:[visitor frameworkNamesByClassName]];
[symbolReferences setFrameworkNamesByProtocolName:[visitor frameworkNamesByProtocolName]];
[visitor release];
}
- (void)generateStructureHeader;
{
NSString *filename;
NSString *referenceString;
[resultString setString:@""];
[classDump appendHeaderToString:resultString];
[symbolReferences removeAllReferences];
referenceIndex = [resultString length];
[classDump appendStructuresToString:resultString symbolReferences:symbolReferences];
referenceString = [symbolReferences referenceString];
if (referenceString != nil)
[resultString insertString:referenceString atIndex:referenceIndex];
filename = @"CDStructures.h";
if (outputPath != nil)
filename = [outputPath stringByAppendingPathComponent:filename];
[[resultString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filename atomically:YES];
}
- (void)willBeginVisiting;
{
[super willBeginVisiting];
[classDump appendHeaderToString:resultString];
// TODO (2007-06-14): Make sure this generates no output files in this case.
if ([classDump containsObjectiveCData] == NO)
NSLog(@"Warning: This file does not contain any Objective-C runtime information.");
[self buildClassFrameworks];
[self createOutputPathIfNecessary];
[self generateStructureHeader];
}
- (void)willVisitClass:(CDOCClass *)aClass;
{
NSString *str;
// First, we set up some context...
[resultString setString:@""];
[classDump appendHeaderToString:resultString];
[symbolReferences removeAllReferences];
str = [symbolReferences importStringForClassName:[aClass superClassName]];
if (str != nil) {
[resultString appendString:str];
[resultString appendString:@"\n"];
}
referenceIndex = [resultString length];
// And then generate the regular output
[super willVisitClass:aClass];
}
- (void)didVisitClass:(CDOCClass *)aClass;
{
NSString *referenceString;
NSString *filename;
// Generate the regular output
[super didVisitClass:aClass];
// Then insert the imports and write the file.
[symbolReferences removeClassName:[aClass name]];
[symbolReferences removeClassName:[aClass superClassName]];
referenceString = [symbolReferences referenceString];
if (referenceString != nil)
[resultString insertString:referenceString atIndex:referenceIndex];
filename = [NSString stringWithFormat:@"%@.h", [aClass name]];
if (outputPath != nil)
filename = [outputPath stringByAppendingPathComponent:filename];
[[resultString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filename atomically:YES];
}
- (void)willVisitCategory:(CDOCCategory *)aCategory;
{
NSString *str;
// First, we set up some context...
[resultString setString:@""];
[classDump appendHeaderToString:resultString];
[symbolReferences removeAllReferences];
str = [symbolReferences importStringForClassName:[aCategory className]];
if (str != nil) {
[resultString appendString:str];
[resultString appendString:@"\n"];
}
referenceIndex = [resultString length];
// And then generate the regular output
[super willVisitCategory:aCategory];
}
- (void)didVisitCategory:(CDOCCategory *)aCategory;
{
NSString *referenceString;
NSString *filename;
// Generate the regular output
[super didVisitCategory:aCategory];
// Then insert the imports and write the file.
[symbolReferences removeClassName:[aCategory className]];
referenceString = [symbolReferences referenceString];
if (referenceString != nil)
[resultString insertString:referenceString atIndex:referenceIndex];
filename = [NSString stringWithFormat:@"%@-%@.h", [aCategory className], [aCategory name]];
if (outputPath != nil)
filename = [outputPath stringByAppendingPathComponent:filename];
[[resultString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filename atomically:YES];
}
- (void)willVisitProtocol:(CDOCProtocol *)aProtocol;
{
[resultString setString:@""];
[classDump appendHeaderToString:resultString];
[symbolReferences removeAllReferences];
referenceIndex = [resultString length];
// And then generate the regular output
[super willVisitProtocol:aProtocol];
}
- (void)didVisitProtocol:(CDOCProtocol *)aProtocol;
{
NSString *referenceString;
NSString *filename;
// Generate the regular output
[super didVisitProtocol:aProtocol];
// Then insert the imports and write the file.
referenceString = [symbolReferences referenceString];
if (referenceString != nil)
[resultString insertString:referenceString atIndex:referenceIndex];
filename = [NSString stringWithFormat:@"%@-Protocol.h", [aProtocol name]];
if (outputPath != nil)
filename = [outputPath stringByAppendingPathComponent:filename];
[[resultString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filename atomically:YES];
}
@end