🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
8 messages
8 total messages Started by JOHNNY LIWIS Thu, 08 May 2003 12:36
Ascii or binary file
#16589
Author: JOHNNY LIWIS
Date: Thu, 08 May 2003 12:36
3 lines
936 bytes
How do I know if the file has binary or ascii content ?

thx in advance!
Re: Ascii or binary file
#16590
Author: Ed Mulroy [TeamB]
Date: Thu, 08 May 2003 13:02
14 lines
1685 bytes
I assume that by "ascii file" you mean a text file.  All text files are
binary files.  A text file has a limited set of values for each byte in the
file.  A binary file might be thought to be a text file if it happens to not
contain any values other than those in the ascii character set.

The operating system does not know if a file was intended to be a text file.
The only way to tell if the file has only bytes which are in the ascii
character set is to open the file and examine the data it contains.

.  Ed

> JOHNNY LIWIS wrote in message
> news:3eba4f4a@newsgroups.borland.com...
> How do I know if the file has binary or ascii content ?
Re: Ascii or binary file
#16591
Author: Andrue Cope
Date: Thu, 08 May 2003 13:20
26 lines
1890 bytes
Johnny Liwis,

> How do I know if the file has binary or ascii content ?
>

All you can do is scan the entire files and determine if all the byte 
values lie within the acceptable range for 'ascii content' whatever 
that is.

FWIW we have a routine that does a pretty good job at this kind of 
thing. We use it to decide whether to activate a hex view or text 
view. The decision is based upon the following function:

bool TOTextLineIndexer::IsMainlyTextual()
{
  return !( ( MyFileSize-MyViewableText )>( 3*MyViewableText ) );
}

Where MyViewableText is the count of bytes whose values lie within 
the range 32 (space) to 0x7F.

I can't give particular statistics for success but we use it a lot 
and it chooses the right mode most of the time.

Andrue Cope
[Bicester, UK]
Re: Ascii or binary file
#16595
Author: Jim Dodd
Date: Thu, 08 May 2003 14:35
43 lines
2620 bytes
Andrue Cope wrote:
> Johnny Liwis,
> 
> 
>>How do I know if the file has binary or ascii content ?
>>
> 
> 
> All you can do is scan the entire files and determine if all the byte 
> values lie within the acceptable range for 'ascii content' whatever 
> that is.
> 
> FWIW we have a routine that does a pretty good job at this kind of 
> thing. We use it to decide whether to activate a hex view or text 
> view. The decision is based upon the following function:
> 
> bool TOTextLineIndexer::IsMainlyTextual()
> {
>   return !( ( MyFileSize-MyViewableText )>( 3*MyViewableText ) );
> }
> 
> Where MyViewableText is the count of bytes whose values lie within 
> the range 32 (space) to 0x7F.
> 
> I can't give particular statistics for success but we use it a lot 
> and it chooses the right mode most of the time.
> 
> Andrue Cope
> [Bicester, UK]
> 

I use a similar scheme to this but also include the values 9 (Tab), 10 
(Line Feed) and 13 (Carriage Return) in the list of values that are 
considered "text". Sometimes, I even include the value 8 (Backspace). 
And in some DOS files, you might want to include 26 (Ctrl-Z) in the list 
of "text" characters becuase it is sometimes used to mark the end of file.

In this way, we don't use Andrue's "times 3" fudge factor. We just say 
that if ANY character falls out side the range [8,9,10,13,26,32-127], 
the file is probably a binary file.

Jim Dodd
Onset Computer Corp.
Re: Ascii or binary file
#16597
Author: Andrue Cope
Date: Thu, 08 May 2003 15:26
21 lines
1950 bytes
Jim Dodd,

> In this way, we don't use Andrue's "times 3" fudge factor. We just say 
> that if ANY character falls out side the range [8,9,10,13,26,32-127], 
> the file is probably a binary file.
>

'Fudge factor'? How dare you sir. It's a kludge :)

In truth our routine is son-of-son-of-something-written-many-years-ago 
and hence my disclaimer about statistics. It's a definite example of code 
that has evolved to meet requirements. We don't claim to know why it's so 
good we just accept that it is <g,d&r>.

Another reason why I've never tried to rationalise it is that it counts 
lines and generates an index for fast seeking. It handles various methods 
of EOL marking (CR/LF, LF/CR, CR, LF) and I don't want to touch that code 
with a bargepole :)

Andrue Cope
[Bicester, UK]
Re: Ascii or binary file
#16609
Author: JOHNNY LIWIS
Date: Fri, 09 May 2003 09:59
32 lines
1968 bytes
Not so practical, especially when you have big files, anyway thank you
everybody!

"Andrue Cope" <not.a.valid@email.address.sorry> skrev i meddelandet
news:VA.00001c9d.01332329@email.address.sorry...
> Johnny Liwis,
>
> > How do I know if the file has binary or ascii content ?
> >
>
> All you can do is scan the entire files and determine if all the byte
> values lie within the acceptable range for 'ascii content' whatever
> that is.
>
> FWIW we have a routine that does a pretty good job at this kind of
> thing. We use it to decide whether to activate a hex view or text
> view. The decision is based upon the following function:
>
> bool TOTextLineIndexer::IsMainlyTextual()
> {
>   return !( ( MyFileSize-MyViewableText )>( 3*MyViewableText ) );
> }
>
> Where MyViewableText is the count of bytes whose values lie within
> the range 32 (space) to 0x7F.
>
> I can't give particular statistics for success but we use it a lot
> and it chooses the right mode most of the time.
>
> Andrue Cope
> [Bicester, UK]
>
Re: Ascii or binary file
#16623
Author: Remy Lebeau (TeamB)
Date: Fri, 09 May 2003 16:14
12 lines
1434 bytes
"JOHNNY LIWIS" <jone@chello.se> wrote in message
news:3ebb7c02$1@newsgroups.borland.com...
> Not so practical, especially when you have big files

You don't have to scan the entire file, just read in a fixed amount, say the
first 1K or so, and then scan that.  If it is ASCII text, then chances are
you won't run into any zeros.  If you do, then it is either UNICODE text or
non-text binary instead.


Gambit
Re: Ascii or binary file
#16625
Author: Jim Dodd
Date: Fri, 09 May 2003 16:44
34 lines
2342 bytes
Andrue Cope wrote:
> Jim Dodd,
> 
> 
>>In this way, we don't use Andrue's "times 3" fudge factor. We just say 
>>that if ANY character falls out side the range [8,9,10,13,26,32-127], 
>>the file is probably a binary file.
>>
> 
> 
> 'Fudge factor'? How dare you sir. It's a kludge :)
> 
> In truth our routine is son-of-son-of-something-written-many-years-ago 
> and hence my disclaimer about statistics. It's a definite example of code 
> that has evolved to meet requirements. We don't claim to know why it's so 
> good we just accept that it is <g,d&r>.
> 
> Another reason why I've never tried to rationalise it is that it counts 
> lines and generates an index for fast seeking. It handles various methods 
> of EOL marking (CR/LF, LF/CR, CR, LF) and I don't want to touch that code 
> with a bargepole :)
> 
> Andrue Cope
> [Bicester, UK]
> 
Very good :-)

Actually, I employ my own "factor" which I was reminded of when reading 
Gambit's later reply - I don't usually read the entire file. I pick a 
percentage of the file size and read that (usually 10-20%) and if I 
haven't found any non-text characters by that time, I declare it text.

Jim Dodd
Onset Computer Corp.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads