checking for line ending types
checking for line ending types
- Subject: checking for line ending types
- From: Craig Hunter <email@hidden>
- Date: Sat, 27 Nov 2004 12:43:46 -0500
Hi All,
I thought I would contribute a solution I worked up for one of our legacy
CAD apps that has been ported to Cocoa
(http://aaac.larc.nasa.gov/tsab/tetruss/mac/gtc.jpg). The inner workings of
the software remain straight C, and as a result, the software can only read
IGES files with UNIX style line endings (\n). For years, users have run
into problems reading IGES files with DOS line endings (\r\n) or Mac line
endings (\r). It's often compounded by e-mail encoding and e-mail apps that
may change the line ending type when someone sends an IGES file by e-mail,
even if it stared out as a UNIX file. The end result was a lot of support
requests when users got generic I/O errors trying to read IGES files.
Here's a check I worked up using grep with NSTask:
char *check_file_type(const char *file_name)
{
NSTask *theGrep=[[NSTask alloc] init];
NSPipe *Gpipe=[[NSPipe alloc] init];
NSFileHandle *Ghandle;
NSString *Gstring;
[theGrep setLaunchPath:@"/usr/bin/grep"];
[theGrep setArguments:[NSArray arrayWithObjects:@"-c",@"\r",[NSString
stringWithCString:file_name],nil]];
[theGrep setStandardOutput:Gpipe];
Ghandle=[Gpipe fileHandleForReading];
[theGrep launch];
Gstring=[[NSString alloc] initWithData:[Ghandle readDataToEndOfFile]
encoding:NSASCIIStringEncoding];
if (atoi([Gstring cString]) == 0) return NULL;
else if (atoi([Gstring cString]) == 1) return "Mac";
else return "DOS";
[Gstring release];
[Gpipe release];
[theGrep release];
}
This check implements grep to search for cases of carriage returns in a
multi-line file. When "grep -c \r filename" returns 0, it's a UNIX file (no
carriage returns, just newlines), when it returns 1 it's a Mac file (grep
detects 1 long line containing multiple \r characters), and greater than 1
would be a DOS (\r\n on each line). Of course there are possible loopholes
and shortcomings to this check in general, but it should work fine for
multi-line files (at least two lines), and if the user tries to read a
non-standard IGES file into the software, it will fail a subsequent IGES
check anyhow.
Hopefully this may be of use to someone else! And by all means, if anyone
has suggestions or improvements, I'd be glad to learn about them -- since I
am a FORTRAN programmer new to C and Obj-C, there is no doubt I have some
clunky implementations here....
thanks,
Craig
--
Dr. Craig A. Hunter
NASA Langley Research Center
AAAC / Configuration Aerodynamics Branch
(757) 864-3020
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden