• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSString encoding and perl script encoding
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString encoding and perl script encoding


  • Subject: Re: NSString encoding and perl script encoding
  • From: "David Brennan" <email@hidden>
  • Date: Thu, 1 Feb 2007 17:45:20 +0000

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSString encoding and perl script encoding
      • From: Sherm Pendley <email@hidden>
    • Re: NSString encoding and perl script encoding
      • From: John Stiles <email@hidden>
  • Prev by Date: Re: [Q] What encoding method can be automatically detected?
  • Next by Date: Re: [Q] What encoding method can be automatically detected?
  • Previous by thread: NSString encoding and perl script encoding
  • Next by thread: Re: NSString encoding and perl script encoding
  • Index(es):
    • Date
    • Thread