From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ad5bd2429a45288e X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Recommended way of reading file as byte blocks Date: 1996/09/29 Message-ID: #1/1 X-Deja-AN: 186110181 references: <01BBA702.69F8B220@idc213.rb.icl.co.uk> content-type: text/plain; charset=ISO-8859-1 organization: Estormza Software mime-version: 1.0 newsgroups: comp.lang.ada Date: 1996-09-29T00:00:00+00:00 List-Id: In article , dewar@schonberg.cs.nyu.edu (Robert Dewar) wrote: >Simon Johnston 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