forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatType.m
151 lines (122 loc) · 4.7 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
// -*- 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-2012 Steve Nygard.
#include <stdio.h>
#include <libc.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#import <Foundation/Foundation.h>
#import "CDClassDump.h"
#import "CDTypeFormatter.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
);
}
typedef enum : NSUInteger {
CDFormat_Ivar = 0,
CDFormat_Method = 1,
CDFormat_Balance = 2,
} CDFormatType;
int main(int argc, char *argv[])
{
@autoreleasepool {
CDTypeFormatter *ivarTypeFormatter = [[CDTypeFormatter alloc] init];
ivarTypeFormatter.shouldExpand = YES;
ivarTypeFormatter.shouldAutoExpand = YES;
ivarTypeFormatter.baseLevel = 0;
CDTypeFormatter *methodTypeFormatter = [[CDTypeFormatter alloc] init];
methodTypeFormatter.shouldExpand = NO;
methodTypeFormatter.shouldAutoExpand = NO;
methodTypeFormatter.baseLevel = 0;
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 = CDFormat_Ivar;
BOOL errorFlag = NO;
int ch;
while ( (ch = getopt_long(argc, argv, "bm", longopts, NULL)) != -1) {
switch (ch) {
case 'b':
formatType = CDFormat_Balance;
break;
case 'm':
formatType = CDFormat_Method;
break;
case '?':
default:
errorFlag = YES;
break;
}
}
if (errorFlag) {
print_usage();
exit(2);
}
switch (formatType) {
case CDFormat_Ivar: printf("Format as ivars\n"); break;
case CDFormat_Method: printf("Format as methods\n"); break;
case CDFormat_Balance: printf("Format as balance\n"); break;
}
for (NSUInteger index = optind; index < (NSUInteger)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 CDFormat_Ivar:
str = [ivarTypeFormatter formatVariable:name type:type];
break;
case CDFormat_Method:
str = [methodTypeFormatter formatMethodName:name type:type];
break;
case CDFormat_Balance: {
CDBalanceFormatter *balance = [[CDBalanceFormatter alloc] initWithString:type];
str = [balance format];
}
}
if (str == nil)
printf("Error formatting type.\n");
else
printf("%s\n", [str UTF8String]);
printf("----------------------------------------------------------------------\n");
name = type = nil;
}
}
}
}
return 0;
}