comp.lang.ada
 help / color / mirror / Atom feed
* Re: reading a file containing both text and binary data
       [not found] <9409022042.aa16676@paris.ics.uci.edu>
@ 1994-09-08 18:07 ` Norman H. Cohen
  1994-09-08 20:45   ` Gene McCulley
  0 siblings, 1 reply; 2+ messages in thread
From: Norman H. Cohen @ 1994-09-08 18:07 UTC (permalink / raw)


In article <9409022042.aa16676@paris.ics.uci.edu>,
self@bordeaux.ICS.UCI.EDU (John Self) writes: 

|> At any point I can tell what the format of the next data item should
|> be, but I can't think of an Ada-like way to read different data types
|> (including text) from the same file.  I can use rep specs to read any
|> single data type by using Sequential_IO, but is there any Ada-like way
|> to read a file containing multiple types?  The only ideas I've had so
|> far involve instantiating Sequential_IO to read 8 bits at a time and
|> pack things into the appropriate data types manually with
|> UNCHECKED_CONVERSION.  In some sense I really am doing an "unchecked
|> conversion" since the files really are just sequences of bytes that
|> were generated by a piece of hardware, but I'd like to have take
|> advantage of any nice structuring ideas Ada can give me.

Unchecked_Conversion is a feature of Ada.  Features of Ada are Ada-like.
Therefore Unchecked_Conversion is Ada-like.

When you say "Ada-like," I suspect you really mean "strongly typed."  The
problem is that your file is inherently weakly typed.  You need an
interface between the weakly typed file and the strongly typed program,
with operations like Read_16_Bit_LE_Integer and Read_32_Bit_BE_Integer.
It is reasonable to use Unchecked_Conversion, together with an
instantiation of Sequential_IO that reads a byte at a time, to IMPLEMENT
this low-level interface.  (On the other hand, to convert, say, a 16-bit
little-endian representation in the file to a value of type Integer in a
way that does not depend on whether Integer itself is represented
big-endian or little-endian, it would make sense to use a computation
like

   256*Second_Byte + First_Byte

instead of Unchecked_Conversion.  But the issue here is making your
program endian-independent rather than avoiding a feature because it is
not "Ada-like.")

--
Norman H. Cohen    ncohen@watson.ibm.com



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: reading a file containing both text and binary data
  1994-09-08 18:07 ` reading a file containing both text and binary data Norman H. Cohen
@ 1994-09-08 20:45   ` Gene McCulley
  0 siblings, 0 replies; 2+ messages in thread
From: Gene McCulley @ 1994-09-08 20:45 UTC (permalink / raw)


|> In article <9409022042.aa16676@paris.ics.uci.edu>,
|> self@bordeaux.ICS.UCI.EDU (John Self) writes: 
|> 
|> |> At any point I can tell what the format of the next data item should
|> |> be, but I can't think of an Ada-like way to read different data types
|> |> (including text) from the same file.  I can use rep specs to read any
|> |> single data type by using Sequential_IO, but is there any Ada-like way
|> |> to read a file containing multiple types?  The only ideas I've had so
|> |> far involve instantiating Sequential_IO to read 8 bits at a time and
|> |> pack things into the appropriate data types manually with
|> |> UNCHECKED_CONVERSION.  In some sense I really am doing an "unchecked
|> |> conversion" since the files really are just sequences of bytes that
|> |> were generated by a piece of hardware, but I'd like to have take
|> |> advantage of any nice structuring ideas Ada can give me.
|> 
I am on a project where we are successfully using some Posix bindings to do
just this sort of thing.  The Posix bindings have Generic_Read and Generic_Write
functions that can be instantiated on any type (even unconstrained array types)
and work with a posix file descriptor.  This allows us to open a file, read
500 bytes of one type, then 1000 bytes of another type without playing any
games with Unchecked_Conversion or closing and reopening files.  We had to read
in some very large files generated by a C program.  It was not much fun at all
until we started using the bindings.  These can probably be found at wuarchive.




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1994-09-08 20:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <9409022042.aa16676@paris.ics.uci.edu>
1994-09-08 18:07 ` reading a file containing both text and binary data Norman H. Cohen
1994-09-08 20:45   ` Gene McCulley

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