home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / Rhapsody / Publishing / PDFConvert-0.1 / NSTask+utils.m < prev    next >
Encoding:
Text File  |  1997-12-10  |  2.0 KB  |  71 lines

  1. /*
  2.  File:       NSTask+utils.m
  3.  Written By: Scott Anguish
  4.  Created:    Dec 9, 1997
  5.  Copyright:  Copyright 1997 by Scott Anguish, all rights reserved.
  6. */
  7.  
  8. #import "NSTask+utils.h"
  9.  
  10. @implementation NSTask(utils)
  11.  
  12.  
  13. + (NSDictionary *)performTask:(NSString *)command
  14.                   inDirectory:(NSString *)directory
  15.                      withArgs:(NSArray *)values;
  16. {
  17.     NSTask *pipeTask;
  18.     NSPipe *readPipe;
  19.     NSFileHandle *readHandle;
  20.     NSData *inData = nil;
  21.     NSMutableData *outData;
  22.     NSMutableDictionary *outputDict;
  23.     
  24.     outputDict=[NSMutableDictionary dictionary];
  25.     [outputDict setObject:command
  26.                    forKey:@"LaunchPath"];
  27.     [outputDict setObject:directory
  28.                    forKey:@"CurrentDirectoryPath"];
  29.     [outputDict setObject:values
  30.                    forKey:@"LaunchArguments"];
  31.     
  32.     pipeTask = [[NSTask alloc] init];
  33.     readPipe = [NSPipe pipe];
  34.     readHandle = [readPipe fileHandleForReading];
  35.     outData=[NSMutableData data];
  36.     [pipeTask setCurrentDirectoryPath:directory];
  37.  
  38.     [pipeTask setStandardError:readPipe];
  39.     [pipeTask setLaunchPath:command];
  40.     [pipeTask setArguments:values];
  41.     [pipeTask launch];
  42.  
  43.     // if we are sure that the pipe is running, then we'll cycle
  44.     // while the pipe reads any available data from stderr.  This
  45.     // will only read stuff if the file that we're attempting to
  46.     // decompress is corrupted, or the commandline app that we've
  47.     // launched is horribly verbose (perhaps an argument causes an
  48.     // app to be verbose?)
  49.     if ([pipeTask isRunning])
  50.       {
  51.         while ((inData = [readHandle availableData]) && [inData length])
  52.           {
  53.             [outData appendData:inData];
  54.           }
  55.       }
  56.  
  57.     [pipeTask waitUntilExit];
  58.  
  59.     [outputDict setObject:[[[NSString alloc] initWithData:outData encoding:NSASCIIStringEncoding] autorelease]
  60.                    forKey:@"StandardError"];
  61.     [outputDict setObject:[NSNumber numberWithInt:[pipeTask terminationStatus]]
  62.                    forKey:@"TerminationStatus"];
  63.  
  64.     [pipeTask release];
  65.     
  66.     return outputDict;
  67.     
  68. }
  69.  
  70. @end
  71.