⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdlogtask.m

📁 处理WORD文档的
💻 M
字号:
/* SDLogTask.m created by andrew on Thu 30-Apr-1998 */#import "SDLogTask.h"// kudos to bbum@codefab.com for helping me fix IO:#import "NSFileHandle_CFRNonBlockingIO.h"//// If you want notification when the task completes// implement this method in the "owner" class//@interface NSObject(SDLogTask_Delegate)- (void)taskTerminated:(BOOL)success;@end@implementation SDLogTask- (void)dealloc{	if (outFile) [outFile release];	[super dealloc];}//// Methods to make it easy to spew feedback to user://#define END_RANGE NSMakeRange([[tv string]length],0)+ (void)appendString:(NSString *)string toText:(NSTextView *)tv newLine:(BOOL)newLine;{        [tv replaceCharactersInRange:END_RANGE withString:string];        if (newLine)                [tv replaceCharactersInRange:END_RANGE withString:@"\n"];        else                [tv replaceCharactersInRange:END_RANGE withString:@" "];        if ([[tv window] isVisible]) {                [tv scrollRangeToVisible:END_RANGE];        }}- (void)appendString:(NSString *)string newLine:(BOOL)newLine{    [[self class] appendString:string toText:logText newLine:newLine]; }- (void)outputData:(NSData *)data{	[self appendString:[[[NSString alloc]initWithData:data encoding:[NSString defaultCStringEncoding]]autorelease] newLine:YES];}- (void) processAvailableData;{	NSData *data;	BOOL dataProcessed;NS_DURING	dataProcessed = NO;	data = [standardErrorHandle availableDataNonBlocking];	if ( (data != nil) && ([data length] != 0) ) {		[self outputData:data];		dataProcessed = YES;	}NS_HANDLER	dataProcessed = NO;NS_ENDHANDLERNS_DURING	if (includeStandardOutput) {		data = [standardOutputHandle availableDataNonBlocking];		if ( (data != nil) && ([data length] != 0) ) {			[self outputData:data];			dataProcessed = YES;		}	}NS_HANDLER        dataProcessed = NO;NS_ENDHANDLER	if ([task isRunning] == YES) {		if (dataProcessed == YES)			[self performSelector: @selector(processAvailableData)				withObject: nil afterDelay: .1];        else		[self performSelector: @selector(processAvailableData)				withObject: nil afterDelay: 2.5];	}}- (void) outputInfoAtStart:(NSString *)pathToExe args:(NSArray *)args{	int i;	// clear it out from last time:	//[logText setString:@""];	[self appendString:pathToExe newLine:NO];	for (i = 0; i < [args count]; i++)		[self appendString:[args objectAtIndex:i] newLine:NO];	[self appendString:@"\n" newLine:YES];}- initAndLaunchWithArgs:(NSArray *)args executable:(NSString *)pathToExe directory:(NSString *)dir logToText:(NSTextView *)text includeStandardOutput:(BOOL)includeStandardOut outFile:(NSString *)anOutFile owner:anOwner environment:(NSDictionary *)environment{	[super init];	logText = text;        includeStandardOutput = includeStandardOut;        outFile = [anOutFile copy];	owner = anOwner;	standardErrorPipe = [NSPipe pipe];	standardErrorHandle =  [standardErrorPipe fileHandleForReading];        if ((outFile!=nil) && ![outFile isEqualToString:@""]) {            [[NSFileManager defaultManager] removeFileAtPath:outFile  handler:nil];            if ([[NSFileManager defaultManager] createFileAtPath:outFile  contents:[NSData data] attributes:nil])            outputFileHandle = [NSFileHandle fileHandleForWritingAtPath:outFile];            else {                NSLog(@"Cannot open %@! Aborting...",outFile);                if ([owner respondsToSelector:@selector(taskTerminated:)])                        [owner taskTerminated:0];		return nil;            }        }else if (includeStandardOutput) {            standardOutputPipe = [NSPipe pipe];            standardOutputHandle =   [standardOutputPipe fileHandleForReading];        } 	task = [[NSTask alloc] init];        [task setArguments:args];	[task setCurrentDirectoryPath:dir];        [task setLaunchPath:pathToExe];	[task setStandardError:standardErrorPipe];	if (environment != nil) [task setEnvironment:environment];        if (outputFileHandle != nil) [task setStandardOutput:outputFileHandle];	else if (includeStandardOutput) [task setStandardOutput:standardOutputPipe];        [self outputInfoAtStart:pathToExe args:args];	[self performSelector: @selector(processAvailableData) withObject: nil afterDelay: 1.0];		// HERE WE GO: spin off a thread to do this:	[task launch];	[[NSNotificationCenter defaultCenter]		addObserver: self		selector: @selector(checkATaskStatus:)		name: NSTaskDidTerminateNotification		object: task];	return self;}#define TASK_SUCCEEDED NSLocalizedStringFromTable(@"Job SUCCEEDED!\n", @"DOC", "message when task is successful")#define TASK_FAILED NSLocalizedStringFromTable(@"Job FAILED!\nPlease see log.", @"DOC", "when the task fails...")- (void)checkATaskStatus:(NSNotification *)aNotification{	int status = [[aNotification object] terminationStatus];	if (status == 0 /* STANDARD SILLY UNIX RETURN VALUE */) {		[self appendString:TASK_SUCCEEDED newLine:YES];	}   else {		[self appendString:TASK_FAILED newLine:YES];		[[logText window] orderFront:self];	}        if (outputFileHandle != nil)		[outputFileHandle release]; 	// this will close it according to docs	if ([owner respondsToSelector:@selector(taskTerminated:)])		[owner taskTerminated:(status == 0)];}@end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -