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 > Strings with quote marks (")?

Strings with quote marks (")?
Thread Tools
Xeo
Moderator Emeritus
Join Date: Mar 2001
Location: Austin, MN, USA
Status: Offline
Reply With Quote
May 17, 2001, 11:30 AM
 
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).]
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
May 17, 2001, 12:00 PM
 
I think the following should work. you have to escape the quote.
Code:
[sedTask setArguments:[NSArray arrayWithObject:@"s|\"||g"]]; // the evil code is here!
If not, try this.

Code:
[sedTask setArguments:[NSArray arrayWithObject:[NSString stringWithCString: "s|\"||g"]]]; // the evil code is here!
You can use my lame ASCII Chart.app to lookup ASCII codes. Get it at http://homepage.mac.com/~tglass/Projects

[This message has been edited by int69h (edited 05-17-2001).]
     
Xeo  (op)
Moderator Emeritus
Join Date: Mar 2001
Location: Austin, MN, USA
Status: Offline
Reply With Quote
May 17, 2001, 12:10 PM
 
Yaay! The first one did it. I'm sure the 2nd one would too, but I don't need the extra code so...

I'm very happy it was as easy as that.

The thought crossed my mind but I dismissed it thinking it would try to replace the backslash and the quote as a pair, in which it wouldn't find any. I guess I should try things sometimes.

Anyway, thank you. That works beautifully.

------------------
     
   
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 03:11 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