forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatType.m
164 lines (130 loc) · 4.68 KB
/
formatType.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
// -*- 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-2011 Steve Nygard.
#include <stdio.h>
#include <libc.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#import <Foundation/Foundation.h>
#import "NSString-Extensions.h"
#import "CDClassDump.h"
#import "CDTypeFormatter.h"
#import "CDSymbolReferences.h"
#import "CDBalanceFormatter.h"
void print_usage(void)
{
fprintf(stderr,
"formatType %s\n"
"Usage: formatType [options] <input file>\n"
"\n"
" where options are:\n"
" -m format method (default is to format ivars)\n"
,
CLASS_DUMP_VERSION
);
}
enum {
CDFormatIvar = 0,
CDFormatMethod = 1,
CDFormatBalance = 2,
};
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CDTypeFormatter *ivarTypeFormatter = [[CDTypeFormatter alloc] init];
[ivarTypeFormatter setShouldExpand:YES];
[ivarTypeFormatter setShouldAutoExpand:YES];
[ivarTypeFormatter setBaseLevel:0];
//[ivarTypeFormatter setDelegate:self];
CDTypeFormatter *methodTypeFormatter = [[CDTypeFormatter alloc] init];
[methodTypeFormatter setShouldExpand:NO];
[methodTypeFormatter setShouldAutoExpand:NO];
[methodTypeFormatter setBaseLevel:0];
//[methodTypeFormatter setDelegate:self];
struct option longopts[] = {
{ "balance", no_argument, NULL, 'b' },
{ "method", no_argument, NULL, 'm' },
{ NULL, 0, NULL, 0 },
};
if (argc == 1) {
print_usage();
exit(0);
}
NSUInteger formatType = CDFormatIvar;
int ch;
BOOL errorFlag = NO;
while ( (ch = getopt_long(argc, argv, "bm", longopts, NULL)) != -1) {
switch (ch) {
case 'b':
formatType = CDFormatBalance;
break;
case 'm':
formatType = CDFormatMethod;
break;
case '?':
default:
errorFlag = YES;
break;
}
}
if (errorFlag) {
print_usage();
exit(2);
}
switch (formatType) {
case CDFormatIvar: printf("Format as ivars\n"); break;
case CDFormatMethod: printf("Format as methods\n"); break;
case CDFormatBalance: printf("Format as balance\n"); break;
}
for (NSUInteger index = optind; index < argc; index++) {
NSString *arg = [NSString stringWithFileSystemRepresentation:argv[index]];
printf("======================================================================\n");
printf("File: %s\n", argv[index]);
NSError *error = nil;
NSString *input = [[NSString alloc] initWithContentsOfFile:arg encoding:NSUTF8StringEncoding error:&error];
if (error != nil) {
NSLog(@"input error: %@", error);
NSLog(@"localizedFailureReason: %@", [error localizedFailureReason]);
}
NSArray *lines = [input componentsSeparatedByString:@"\n"];
NSString *name = nil;
NSString *type = nil;
for (NSString *line in lines) {
if ([line hasPrefix:@"//"] || [line length] == 0) {
printf("%s\n", [line UTF8String]);
continue;
}
if (name == nil) {
name = line;
} else if (type == nil) {
NSString *str;
type = line;
switch (formatType) {
case CDFormatIvar:
str = [ivarTypeFormatter formatVariable:name type:type symbolReferences:nil];
break;
case CDFormatMethod:
str = [methodTypeFormatter formatMethodName:name type:type symbolReferences:nil];
break;
case CDFormatBalance: {
CDBalanceFormatter *balance = [[CDBalanceFormatter alloc] initWithString:type];
str = [balance format];
[balance release];
}
}
if (str == nil)
printf("Error formatting type.\n");
else
printf("%s\n", [str UTF8String]);
printf("----------------------------------------------------------------------\n");
name = type = nil;
}
}
[input release];
}
[ivarTypeFormatter release];
[methodTypeFormatter release];
[pool release];
return 0;
}