• 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: determining binary files from text
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >determining binary files from text (From: Ed Silva <email@hidden>)

  • Prev by Date: Re: How can I hide an NSView properly?
  • Next by Date: Problems converting NSString path to FSSpec
  • Previous by thread: determining binary files from text
  • Next by thread: Re: determining binary files from text
  • Index(es):
    • Date
    • Thread