This slight changed one will detect any non text file as binary.
http://en.wikipedia.org/wiki/Ascii
Linefeed, Carriage Return,Tab is allowed.
Anything else below from $0 - $1F is flagged as binary,
likewise $7F is considered binary.
Code: Select all
Procedure.b CheckBinaryfile(FileName.s)
Protected file.l, buf.b
If FileSize(FileName)>0
file = ReadFile(#PB_Any,FileName)
While Eof(file)=0
buf.b =ReadByte(file)
Select buf
Case $00 To $08,$0B,$0C,$0E To $1F,$7F
Debug "BINARY"
CloseFile(file)
ProcedureReturn #True
EndSelect
Wend
CloseFile(file)
EndIf
ProcedureReturn #False
EndProcedure
anything else in 0-31 ascii range or 127 will flag as being binary.
This should be as good as it can get, for detecting normal text (latin-1) and binary.
I'm not sure about UTF8 text, but I do believe that it might be ok in most cases.
However, the only way to truly detect if something is binary or not is simple (but cpu/disk intensive),
if you find $0 (binary zero) then it's binary, if not, it's 99.99% chance of being text.
I have yet to see any text files with a binary zero in them.
(Microsoft Word documents do not count as being "text" those are actually a binary document file format)