Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Wrapping wget

Wrapping wget
Thread Tools
Junior Member
Join Date: Jul 2002
Location: Australia
Status: Offline
Reply With Quote
Aug 20, 2004, 11:21 PM
 
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
Reply With Quote
Aug 21, 2004, 05:35 AM
 
What exactly doesn't wrk?
     
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Aug 21, 2004, 08:54 AM
 
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
     
Syphor  (op)
Junior Member
Join Date: Jul 2002
Location: Australia
Status: Offline
Reply With Quote
Aug 21, 2004, 09:17 AM
 
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
Reply With Quote
Aug 21, 2004, 11:25 AM
 
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'."
     
Syphor  (op)
Junior Member
Join Date: Jul 2002
Location: Australia
Status: Offline
Reply With Quote
Aug 22, 2004, 12:00 AM
 
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]; }
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:10 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2