Remove use of deprecated stringWithCString
[GitX.git] / PBEasyPipe.m
blobf07617a01d81ca2c1f84440f53e90623fdacb607
1 //
2 //  PBEasyPipe.m
3 //  GitX
4 //
5 //  Created by Pieter de Bie on 16-06-08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
9 #import "PBEasyPipe.h"
12 @implementation PBEasyPipe
14 + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args
16         return [self handleForCommand:cmd withArgs:args inDir:nil];
19 + (NSTask *) taskForCommand:(NSString *)cmd withArgs:(NSArray *)args inDir:(NSString *)dir
21         NSTask* task = [[NSTask alloc] init];
22         task.launchPath = cmd;
23         task.arguments = args;
24         if (dir)
25                 task.currentDirectoryPath = dir;
27         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Show Debug Messages"])
28                 NSLog(@"Starting command `%@ %@` in dir %@", cmd, [args componentsJoinedByString:@" "], dir);
29 #ifdef CLI
30         NSLog(@"Starting command `%@ %@` in dir %@", cmd, [args componentsJoinedByString:@" "], dir);
31 #endif
33         NSPipe* pipe = [NSPipe pipe];
34         task.standardOutput = pipe;
35         return task;
38 + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
40         NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
41         NSFileHandle* handle = [task.standardOutput fileHandleForReading];
42         
43         [task launch];
44         return handle;
49 + (NSString*) outputForCommand:(NSString *) cmd
50                                           withArgs:(NSArray *)  args
51                                                  inDir:(NSString *) dir
52                                       retValue:(int *)      ret
54         return [self outputForCommand:cmd withArgs:args inDir:dir inputString:NULL retValue:ret];
55 }       
57 // TODO: Refactor this to use the function above
58 + (NSString*) outputForCommand:(NSString *) cmd
59                                           withArgs:(NSArray *)  args
60                                                  inDir:(NSString *) dir
61                                    inputString:(NSString *)input
62                                       retValue:(int *)      ret
64         NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
65         NSFileHandle* handle = [task.standardOutput fileHandleForReading];
67         if (input) {
68                 task.standardInput = [NSPipe pipe];
69                 NSFileHandle *inHandle = [task.standardInput fileHandleForWriting];
70                 [inHandle writeData:[input dataUsingEncoding:NSUTF8StringEncoding]];
71                 [inHandle closeFile];
72         }
73         
74         [task launch];
75         
76         NSData* data = [handle readDataToEndOfFile];
77         NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
78         if (!string)
79                 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
80         
81         // Strip trailing newline
82         if ([string hasSuffix:@"\n"])
83                 string = [string substringToIndex:[string length]-1];
84         
85         [task waitUntilExit];
86         if (ret)
87                 *ret = [task terminationStatus];
88         return string;
89 }       
91 // We don't use the above function because then we'd have to wait until the program was finished
92 // with running
94 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args  inDir: (NSString*) dir
96         NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
97         NSFileHandle* handle = [task.standardOutput fileHandleForReading];
98         
99         [task launch];
100 #warning This can cause a "Bad file descriptor"... when?
101         NSData *data;
102         @try {
103                 data = [handle readDataToEndOfFile];
104         }
105         @catch (NSException * e) {
106                 NSLog(@"Got a bad file descriptor in %s!", _cmd);
107                 if ([NSThread currentThread] != [NSThread mainThread])
108                         [task waitUntilExit];
110                 return nil;
111         }
112         NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
113         if (!string)
114                 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
115         
116         // Strip trailing newline
117         if ([string hasSuffix:@"\n"])
118                 string = [string substringToIndex:[string length]-1];
120         if ([NSThread currentThread] != [NSThread mainThread])
121                 [task waitUntilExit];
123         return string;
127 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args
129         return [self outputForCommand:cmd withArgs:args inDir:nil];
132 @end