comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Recommended way of reading file as byte blocks
Date: 1996/09/29
Date: 1996-09-29T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023180002909961333340001@news.ni.net> (raw)
In-Reply-To: dewar.843914492@schonberg


In article <dewar.843914492@schonberg>, dewar@schonberg.cs.nyu.edu (Robert
Dewar) wrote:

>Simon Johnston <skj@ACM.ORG> wrote in article
<01BBA702.69F8B220@idc213.rb.icl.co.uk>...
>Hi, I have tried a number of ways of doing this, reading a file as blocks
>of bytes. In most cases when the application is examined a better way is
>found, however I am now at a point where I am converting some C code which
>does:
>
>    fp = fopen(...)
>    i = fread(fp, buffer, 512)
>    while (!feof(fp)) {
>       for ( ; i > 0; i--) {
>          ..
>       }
>       handle buffer
>       i = fread(...)
>    }
>
>and it really matters that this logic is preserved. I can open the file as
>a Direct_IO(Element => BYTE) file which is nasty, I have tried using
>Streams but never quite get it right.

>Two obvious answers. First, use stream_io, second, call fread directly.
>Both allow you to duplicate the logic exactly.

Here's a 3rd option: write a small C subprogram (or just keep the one
you're converting) that does the algorithm you want, and write an Ada
binding to _that_.  That way, you don't have to write bindings to all the
OS calls required to implement the algorithm.

I prefer this approach, because you can't write a binding to everything,
for example to #define constants or macros.  You could find the value of
the C constant in the header file, and declare a constant in your Ada code
with the same value, but what happens if the OS changes, and the constant
has a different value?  By keeping all the OS calls in C, you avoid these
headaches.

My Ada package then really has 2 bodies: one written in a Ada, and the
other in C.  The Ada subprograms are implemented by calling the routines in
the C body (that in turn call the OS) and checking the return value, and
raising an appropriate exception when required.

This way I get the best of both worlds: I don't have to write OS bindings,
and I can handle problems using exceptions instead of status values.

Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




      reply	other threads:[~1996-09-29  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <01BBA702.69F8B220@idc213.rb.icl.co.uk>
1996-09-21  0:00 ` Recommended way of reading file as byte blocks Robert Dewar
1996-09-21  0:00 ` Robert Dewar
1996-09-21  0:00 ` David C. Hoos, Sr.
1996-09-21  0:00 ` David C. Hoos, Sr.
1996-09-28  0:00   ` Robert Dewar
1996-09-29  0:00     ` Matthew Heaney [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox