Re: NSString encoding and perl script encoding
Re: NSString encoding and perl script encoding
- Subject: Re: NSString encoding and perl script encoding
- From: John Stiles <email@hidden>
- Date: Thu, 1 Feb 2007 10:30:43 -0800
You know you can use regular expressions from C, right?
There are good ObjC libraries, but personally I use "PCRE" which is
simple C code (so it's cross-platform).
I wouldn't shell out to Perl just for regular expressions, that's
overkill.
On Feb 1, 2007, at 9:45 AM, David Brennan wrote:
I am doing nothing with the perl script yet. I plan to use perl, to
apply regular expressions to the text from PDF's, so I want to use
unicode. I tried to use the encoding NSUnicodeStringEncoding, but the
returned string was not correct. When I use the NSUTF8StringEncoding
encoding, I lose some of the text, because converting the NSData from
the pipe to NSString returns nil sometimes.
I don't know why this is not working, I would be very grateful of
any help.
Regards,
Dave.
Here is the script
use encoding 'utf8';
while (defined($line = <STDIN>))
{
print $line
}
The code that calls the script.
- (void)runRegExpOnString
{
resultString = @""; //class member
//inputString //class member
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/perl"];
[task setArguments:[NSArray
arrayWithObjects:@"/Users/david/Desktop/testScript.perl",nil]];
NSPipe *writePipe = [NSPipe pipe];
writeHandle = [writePipe fileHandleForWriting];
[task setStandardInput: writePipe];
NSPipe *readPipe = [NSPipe pipe];
NSFileHandle *readHandle = [readPipe fileHandleForReading];
[task setStandardOutput: readPipe];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getData:)
name: NSFileHandleReadCompletionNotification
object: readHandle];
[[[task standardOutput] fileHandleForReading]
readInBackgroundAndNotify];
[task launch];
inputString = inString;
[NSThread detachNewThreadSelector:@selector(threadRunFuntion:)
toTarget:self withObject:nil];
[task waitUntilExit];
NSMutableData *data = [[NSMutableData alloc] init];
NSData *readData;
while ((readData = [readHandle availableData]) && [readData length])
{
[data appendData: readData];
}
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSFileHandleReadCompletionNotification object: [[task
standardOutput] fileHandleForReading]];
NSString *tempOutputString = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
if(tempOutputString != nil)
{
[resultString stringByAppendingString: tempOutputString];
}
[task release];
[data release];
}
- (void) getData: (NSNotification *)aNotification
{
NSData *data = [[aNotification userInfo]
objectForKey:NSFileHandleNotificationDataItem];
if ([data length])
{
NSString *tempOutputString = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
if(tempOutputString != nil)
{
[resultString stringByAppendingString: tempOutputString];
}
else
{
NSLog(@"\n Error \n");
}
}
[[aNotification object] readInBackgroundAndNotify];
}
- (void)threadRunFuntion:(id)sender
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if(writeHandle != nil && inputString != nil)
{
[writeHandle writeData:[inputString
dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
}
[writeHandle closeFile];
[pool release];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
40blizzard.com
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden