Skip to content

Commit

Permalink
小优化
Browse files Browse the repository at this point in the history
小优化
  • Loading branch information
CoderMJLee committed Jan 29, 2018
1 parent d770e57 commit 3d531ba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
18 changes: 6 additions & 12 deletions MJAppTools/Models/MJMachO.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,9 @@ + (instancetype)machOWithFileHandle:(NSFileHandle *)handle
- (instancetype)initWithFileHandle:(NSFileHandle *)handle
{
if (self = [super init]) {
// 开始的偏移
unsigned long long beginOffset = handle.offsetInFile;

// magic
uint32_t magic = [handle mj_readUint32];

// 恢复偏移
[handle seekToFileOffset:beginOffset];

uint32_t magic = [handle mj_staticReadUint32];
if (magic == FAT_CIGAM || magic == FAT_MAGIC) { // FAT
[self setupFat:handle bigEndian:magic == FAT_CIGAM];
[self setupFat:handle];
} else {
[self setupMachO:handle];
}
Expand Down Expand Up @@ -102,13 +94,14 @@ - (void)setupMachO:(NSFileHandle *)handle
}
}

- (void)setupFat:(NSFileHandle *)handle bigEndian:(BOOL)bigEndian
- (void)setupFat:(NSFileHandle *)handle
{
self.fat = YES;

// fat头
struct fat_header header;
[handle mj_readData:&header length:sizeof(struct fat_header)];
BOOL bigEndian = (header.magic == FAT_CIGAM);

// 架构数量
uint32_t archCount = MJEndianConvert(bigEndian, header.nfat_arch);
Expand All @@ -123,7 +116,8 @@ - (void)setupFat:(NSFileHandle *)handle bigEndian:(BOOL)bigEndian

// 偏移到架构具体数据的开始
[handle seekToFileOffset:MJEndianConvert(bigEndian, arch.offset)];
MJMachO *machO = [[self class] machOWithFileHandle:handle];
MJMachO *machO = [[[self class] alloc] init];
[machO setupMachO:handle];
if (machO.isEncrypted) {
self.encrypted = YES;
}
Expand Down
35 changes: 23 additions & 12 deletions MJAppTools/Tools/MJPrintTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,58 @@
const NSString *MJPrintColorError = @"\033[1;31m";
const NSString *MJPrintColorStrong = @"\033[1;32m";

#define MJBeginFormat \
if (!format) return; \
va_list args; \
va_start(args, format); \
format = [[NSString alloc] initWithFormat:format arguments:args];

#define MJEndFormat va_end(args);

@implementation MJPrintTools

+ (void)printError:(NSString *)format, ...
{
MJBeginFormat;
format = [@"Error: " stringByAppendingString:format];
[self printColor:(NSString *)MJPrintColorError format:format];
}

+ (void)printWarning:(NSString *)format, ...
{
MJBeginFormat;
format = [@"Warning: " stringByAppendingString:format];
[self printColor:(NSString *)MJPrintColorWarning format:format];
MJEndFormat;
}

+ (void)printStrong:(NSString *)format, ...
{
MJBeginFormat;
[self printColor:(NSString *)MJPrintColorStrong format:format];
MJEndFormat;
}

+ (void)print:(NSString *)format, ...
{
MJBeginFormat;
[self printColor:nil format:format];
MJEndFormat;
}

+ (void)printColor:(NSString *)color format:(NSString *)format, ...
{
if (!format) return;
MJBeginFormat;

va_list args;
va_start(args, format);
NSString *formatStr = [[NSString alloc] initWithFormat:format arguments:args];
NSString *printStr = nil;

if (color) {
printStr = [color stringByAppendingString:formatStr];
if (color && ![color isEqual:MJPrintColorDefault]) {
printStr = [color stringByAppendingFormat:@"%@%@", format, MJPrintColorDefault];
} else {
printStr = [MJPrintColorDefault stringByAppendingString:formatStr];
printStr = [MJPrintColorDefault stringByAppendingString:format];
}

printStr = [printStr stringByAppendingString:(NSString *)MJPrintColorDefault];

printf("%s", printStr.UTF8String);
va_end(args);

MJEndFormat;
}

@end
Binary file removed Release/MJAppTools
Binary file not shown.

0 comments on commit 3d531ba

Please sign in to comment.