OLD | NEW |
(Empty) | |
| 1 // -*- mode: ObjC -*- |
| 2 |
| 3 // This file is part of class-dump, a utility for examining the Objective-C seg
ment of Mach-O files. |
| 4 // Copyright (C) 1997-1998, 2000-2001, 2004-2010 Steve Nygard. |
| 5 |
| 6 #include <stdio.h> |
| 7 #include <libc.h> |
| 8 #include <unistd.h> |
| 9 #include <getopt.h> |
| 10 #include <stdlib.h> |
| 11 |
| 12 #import <Foundation/Foundation.h> |
| 13 #import "NSString-Extensions.h" |
| 14 |
| 15 #import "CDClassDump.h" |
| 16 #import "CDTypeFormatter.h" |
| 17 #import "CDSymbolReferences.h" |
| 18 #import "CDBalanceFormatter.h" |
| 19 |
| 20 void print_usage(void) |
| 21 { |
| 22 fprintf(stderr, |
| 23 "formatType %s\n" |
| 24 "Usage: formatType [options] <input file>\n" |
| 25 "\n" |
| 26 " where options are:\n" |
| 27 " -m format method (default is to format ivars)\n" |
| 28 , |
| 29 CLASS_DUMP_VERSION |
| 30 ); |
| 31 } |
| 32 |
| 33 enum { |
| 34 CDFormatIvar = 0, |
| 35 CDFormatMethod = 1, |
| 36 CDFormatBalance = 2, |
| 37 }; |
| 38 |
| 39 int main(int argc, char *argv[]) |
| 40 { |
| 41 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 42 NSUInteger formatType = CDFormatIvar; |
| 43 |
| 44 int ch; |
| 45 BOOL errorFlag = NO; |
| 46 |
| 47 CDTypeFormatter *ivarTypeFormatter; |
| 48 CDTypeFormatter *methodTypeFormatter; |
| 49 NSUInteger index; |
| 50 |
| 51 ivarTypeFormatter = [[CDTypeFormatter alloc] init]; |
| 52 [ivarTypeFormatter setShouldExpand:YES]; |
| 53 [ivarTypeFormatter setShouldAutoExpand:YES]; |
| 54 [ivarTypeFormatter setBaseLevel:0]; |
| 55 //[ivarTypeFormatter setDelegate:self]; |
| 56 |
| 57 methodTypeFormatter = [[CDTypeFormatter alloc] init]; |
| 58 [methodTypeFormatter setShouldExpand:NO]; |
| 59 [methodTypeFormatter setShouldAutoExpand:NO]; |
| 60 [methodTypeFormatter setBaseLevel:0]; |
| 61 //[methodTypeFormatter setDelegate:self]; |
| 62 |
| 63 struct option longopts[] = { |
| 64 { "balance", no_argument, NULL, 'b' }, |
| 65 { "method", no_argument, NULL, 'm' }, |
| 66 { NULL, 0, NULL, 0 }, |
| 67 }; |
| 68 |
| 69 if (argc == 1) { |
| 70 print_usage(); |
| 71 exit(0); |
| 72 } |
| 73 |
| 74 while ( (ch = getopt_long(argc, argv, "bm", longopts, NULL)) != -1) { |
| 75 switch (ch) { |
| 76 case 'b': |
| 77 formatType = CDFormatBalance; |
| 78 break; |
| 79 |
| 80 case 'm': |
| 81 formatType = CDFormatMethod; |
| 82 break; |
| 83 |
| 84 case '?': |
| 85 default: |
| 86 errorFlag = YES; |
| 87 break; |
| 88 } |
| 89 } |
| 90 |
| 91 if (errorFlag) { |
| 92 print_usage(); |
| 93 exit(2); |
| 94 } |
| 95 |
| 96 switch (formatType) { |
| 97 case CDFormatIvar: printf("Format as ivars\n"); break; |
| 98 case CDFormatMethod: printf("Format as methods\n"); break; |
| 99 case CDFormatBalance: printf("Format as balance\n"); break; |
| 100 } |
| 101 |
| 102 for (index = optind; index < argc; index++) { |
| 103 NSString *arg; |
| 104 NSString *input; |
| 105 NSError *error; |
| 106 NSArray *lines; |
| 107 CDTypeFormatter *formatter; |
| 108 NSString *name, *type; |
| 109 |
| 110 arg = [NSString stringWithFileSystemRepresentation:argv[index]]; |
| 111 printf("================================================================
======\n"); |
| 112 printf("File: %s\n", argv[index]); |
| 113 |
| 114 input = [[NSString alloc] initWithContentsOfFile:arg encoding:NSUTF8Stri
ngEncoding error:&error]; |
| 115 lines = [input componentsSeparatedByString:@"\n"]; |
| 116 |
| 117 name = type = nil; |
| 118 for (NSString *line in lines) { |
| 119 if ([line hasPrefix:@"//"] || [line length] == 0) { |
| 120 printf("%s\n", [line UTF8String]); |
| 121 continue; |
| 122 } |
| 123 |
| 124 if (name == nil) |
| 125 name = line; |
| 126 else if (type == nil) { |
| 127 NSString *str; |
| 128 |
| 129 type = line; |
| 130 |
| 131 switch (formatType) { |
| 132 case CDFormatIvar: |
| 133 str = [ivarTypeFormatter formatVariable:name type:type sym
bolReferences:nil]; |
| 134 break; |
| 135 |
| 136 case CDFormatMethod: |
| 137 str = [methodTypeFormatter formatMethodName:name type:type
symbolReferences:nil]; |
| 138 break; |
| 139 |
| 140 case CDFormatBalance: { |
| 141 CDBalanceFormatter *balance; |
| 142 |
| 143 balance = [[CDBalanceFormatter alloc] initWithString:type]
; |
| 144 str = [balance format]; |
| 145 [balance release]; |
| 146 } |
| 147 } |
| 148 if (str == nil) |
| 149 printf("Error formatting type.\n"); |
| 150 else |
| 151 printf("%s\n", [str UTF8String]); |
| 152 printf("--------------------------------------------------------
--------------\n"); |
| 153 |
| 154 name = type = nil; |
| 155 } |
| 156 |
| 157 } |
| 158 |
| 159 [input release]; |
| 160 } |
| 161 |
| 162 [ivarTypeFormatter release]; |
| 163 [methodTypeFormatter release]; |
| 164 |
| 165 [pool release]; |
| 166 |
| 167 return 0; |
| 168 } |
OLD | NEW |