-
Notifications
You must be signed in to change notification settings - Fork 15
/
NSTaskAdditions.m
75 lines (60 loc) · 1.9 KB
/
NSTaskAdditions.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
//
// NSTaskAdditions.m
//
// Created by Kevin Wojniak on 10/18/11.
// Copyright 2011 Kevin Wojniak, Inc. All rights reserved.
//
#import "NSTaskAdditions.h"
@implementation NSTask (NSTaskAdditions)
+ (NSData *)outputDataForTaskAtPath:(NSString *)taskPath arguments:(NSArray *)args status:(int *)status
{
NSTask *task = nil;
NSPipe *inPipe = nil, *outPipe = nil;
NSFileHandle *inHandle = nil, *outHandle = nil;
NSData *outputData = nil;
NSFileManager *fm = [[NSFileManager alloc] init];
if (!taskPath || ![fm fileExistsAtPath:taskPath] || ![fm isExecutableFileAtPath:taskPath]) {
return nil;
}
task = [[NSTask alloc] init];
[task setLaunchPath:taskPath];
if ((args != nil) && ([args count] > 0)) {
[task setArguments:args];
}
// NSPipe can return nil
outPipe = [[NSPipe alloc] init];
if (outPipe != nil) {
outHandle = [outPipe fileHandleForReading];
[task setStandardOutput:outPipe];
[task setStandardError:outPipe];
}
inPipe = [[NSPipe alloc] init];
if (inPipe != nil) {
inHandle = [inPipe fileHandleForWriting];
[task setStandardInput:inPipe];
}
[task launch];
[inHandle closeFile];
if (outHandle != nil) {
outputData = [outHandle readDataToEndOfFile];
[outHandle closeFile];
}
[task waitUntilExit];
if (status) {
*status = [task terminationStatus];
}
return outputData;
}
+ (NSString *)outputStringForTaskAtPath:(NSString *)taskPath arguments:(NSArray *)args encoding:(NSStringEncoding)encoding
{
return [self outputStringForTaskAtPath:taskPath arguments:args encoding:encoding status:NULL];
}
+ (NSString *)outputStringForTaskAtPath:(NSString *)taskPath arguments:(NSArray *)args encoding:(NSStringEncoding)encoding status:(int *)status
{
NSData *data = [[self class] outputDataForTaskAtPath:taskPath arguments:args status:status];
if (data && [data length]) {
return [[NSString alloc] initWithData:data encoding:encoding];
}
return @"";
}
@end