Re: determining binary files from text
Re: determining binary files from text
- Subject: Re: determining binary files from text
- From: Steve Bird <email@hidden>
- Date: Thu, 28 Feb 2002 15:59:14 -0500
On Thursday, February 28, 2002, at 03:12 , Ed Silva wrote:
I have a bit of a problem: I need to be able to figure out if a file is a
binary or plain text.
Here's the problem that I ran into: I am working on a Cocoa application
to manage the /Library/StartupItems folders; you can edit the scripts and
plists and enable/disable items (changing the /etc/hostconfig entry to
'-NO-' or '-YES-', if there is one), create new startup items, etc. It's
pretty nifty, IMHO. :-) Unfortunately it is possible that someone could
put binaries in a startup item folder, which causes my app to barf and
die when it tires to parse it. So, how do I tell if a file is a binary or
text?
Foolproof? Ain't gonna happen.
BUT
You can improve the odds.
Assuming we're looking for ASCII text (not UniCode)
Search through the first 1000 chars or so.
If you find NULL characters, call it binary.
If the ratio of chars in [0x0D, 0x0A, 0x09, 0x20..07F] to total characters
scanned is less than 99%, call it binary.
Else it's text.
There may be a NSCharacterSet already defined to do that.
-----------------------------------------------------------------
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
www.Culverson.com (toll free) 1-877-676-8175
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.