Very simple CLI Wrapper. This code works. I'll change it to show what doesn't work.
Code:
#import "Controller.h"
@implementation Controller
- (IBAction)button:(id)sender
{
NSTask *topTask = [[NSTask alloc] init];
NSPipe *topPipe = [NSPipe pipe];
NSTask *sedTask = [[NSTask alloc] init];
NSPipe *sedPipe = [NSPipe pipe];
NSFileHandle *theHandle = [sedPipe fileHandleForReading];
NSData *theData = nil;
NSMutableString *theString = [NSMutableString string];
[topTask setLaunchPath:@"/usr/bin/top"];
[topTask setArguments:[NSArray arrayWithObject:@"-l"]];
[sedTask setLaunchPath:@"/usr/bin/sed"];
[sedTask setArguments:[NSArray arrayWithObject:@"s|1||g"]];
[topTask setStandardOutput:topPipe];
[sedTask setStandardInput:topPipe];
[sedTask setStandardOutput:sedPipe];
[topTask launch];
[sedTask launch];
while ((theData = [theHandle availableData]) && [theData length]) {
[theString appendString:[NSString stringWithCString:[theData bytes]]];
}
[textfield setString:theString];
[topTask release];
[sedTask release];
}
@end
OK, I've got this code. It's simple, it runs top. I have "sed" which the data is piped to. In the code above, I have the arguments for sed set to replace all occurrences of the number one (1) with nothing (essentially deleting it).
What I want to do is have it replace all occurrences of a quote mark ("). Strings are so messy, and they require quote marks around them, so how do I go about doing this?
Code:
#import "Controller.h"
@implementation Controller
- (IBAction)button:(id)sender
{
NSTask *topTask = [[NSTask alloc] init];
NSPipe *topPipe = [NSPipe pipe];
NSTask *sedTask = [[NSTask alloc] init];
NSPipe *sedPipe = [NSPipe pipe];
NSFileHandle *theHandle = [sedPipe fileHandleForReading];
NSData *theData = nil;
NSMutableString *theString = [NSMutableString string];
[topTask setLaunchPath:@"/usr/bin/top"];
[topTask setArguments:[NSArray arrayWithObject:@"-l"]];
[sedTask setLaunchPath:@"/usr/bin/sed"];
[sedTask setArguments:[NSArray arrayWithObject:@"s|"||g"]]; // the evil code is here!
[topTask setStandardOutput:topPipe];
[sedTask setStandardInput:topPipe];
[sedTask setStandardOutput:sedPipe];
[topTask launch];
[sedTask launch];
while ((theData = [theHandle availableData]) && [theData length]) {
[theString appendString:[NSString stringWithCString:[theData bytes]]];
}
[textfield setString:theString];
[topTask release];
[sedTask release];
}
@end
I put the code I want to change in bold. It wants to end the string at the quote mark that I want as part of the argument. What can I do? I was thinking along the lines of ASCII or something, but I'm not sure how to make that work the way it's supposed to. Is there a way to say "Hey, this string consists of THESE characters!" without the quote mark fusking it up?
Thanks for any and all help.
Oh, and if you know how to fix it, please show me with a code change or something. I'm pretty new to this, I don't know any ASCII characters (except 13) and unless you explain it really well, it'd be easier just to show.
------------------
[This message has been edited by Xeo (edited 05-17-2001).]