 |
 |
Wrapping wget
|
 |
|
 |
|
Junior Member
Join Date: Jul 2002
Location: Australia
Status:
Offline
|
|
Hi all,
I'm making a cocoa app in obj-c that wrapps a command line program. So to get started I thought I'd do some simple wrapping so I know how all this works.
There is no easy way to put this so I'll just blurt it out...
Whats wrong with the following ???
Code:
#import "Controller.h"
@implementation Controller
- (IBAction)go:(id)sender
{
//Declare our variables
NSTask *theTask = [[NSTask alloc] init];
NSPipe *thePipe = [[NSPipe alloc] init];
NSFileHandle *theHandle;
/* Set Launch path and arguments */
[theTask setLaunchPath:@"/usr/bin/wget"];
[theTask setArguments:[NSArray arrayWithObject:@"http://www.apple.com/"]];
//Prepare and set our output
[theTask setStandardOutput:thePipe];
theHandle = [thePipe fileHandleForReading];
//Launch the program
[theTask launch];
//Make a new thread for streaming data
[NSThread detachNewThreadSelector:@selector(recieveData:) toTarget:self withObject:theHandle];
//Release memory objects
[theTask release];
[thePipe release];
}
- (void)recieveData:(NSFileHandle*)handle
{
NSAutoreleasePool *apool=[[NSAutoreleasePool alloc] init];
NSData *data = nil;
NSLog(@"\nNew Thread Launched\n");
NSLog(@"Starting getting data:\n");
while((data=[handle availableData]) && [data length]) { // until EOF
NSString *string=[[NSString alloc] initWithData:[handle availableData] encoding:NSASCIIStringEncoding];
NSLog(string);
[string release];
}
[apool release];
}
@end
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
What exactly doesn't wrk?
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2003
Location: USA
Status:
Offline
|
|
I'm curious too...only thing I did differently (as far as I can tell) is I specified the directory to download to using setCurrentDirectoryPath.
|
|
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Jul 2002
Location: Australia
Status:
Offline
|
|
It doesn't do this properly:
Code:
while((data=[handle availableData]) && [data length]) { // until EOF
NSString *string=[[NSString alloc] initWithData:[handle availableData] encoding:NSASCIIStringEncoding];
NSLog(string);
[string release];
}
You'll see more better if u do this:
Code:
while((data=[handle availableData]) && [data length]) { // until EOF
NSString *string=[[NSString alloc] initWithData:[handle availableData] encoding:NSASCIIStringEncoding];
NSLog(@"Data Recieved:%@",string);
[string release];
}
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by techtrucker:
I'm curious too...only thing I did differently (as far as I can tell) is I specified the directory to download to using setCurrentDirectoryPath.
One thing I noticed: Does -[NSFileHandle availableData] clear the buffer? If it does, that would be a problem, because it's being called twice per iteration (once as a loop condition, and once when creating the string).
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Jul 2002
Location: Australia
Status:
Offline
|
|
Thanks for pointing that out, though it still doesn't work the way i want.
Code:
- (void)recieveData:(NSFileHandle*)handle
{
NSAutoreleasePool *apool=[[NSAutoreleasePool alloc] init];
NSData *data = nil;
NSLog(@"\nNew Thread Launched\n");
NSLog(@"\nStarting getting data:\n");
while((data=[handle availableData]) && [data length]) { // until EOF
NSString *string=[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"DATA:%@",string);
[string release];
}
[apool release];
}
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |