From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Writing Data to a file
Date: Thu, 15 Dec 2011 19:32:37 +0100
Date: 2011-12-15T19:32:37+01:00 [thread overview]
Message-ID: <87zketwure.fsf@ludovic-brenta.org> (raw)
In-Reply-To: 344d32d6-a667-407c-bb8e-e0c504e91688@u32g2000yqe.googlegroups.com
awdorrin <awdorrin@gmail.com> writes:
> I have code that imports the unix 'write' function in order to write a
> stream of byte data.
>
> I had thought that replacing this call with the POSIX.IO.Write would
> make more sense, so that I was using more of a standard method.
>
> Unfortunately, I cannot access the write function within POSIX.IO, so
> I'm stuck with using the Write procedures.
>
> I have the following code, but I'm not sure that I'm doing this the
> right way...
>
> declare
> Buf_Last : Stream_Element_Count;
> Buf_Addr : System.Address := AREA_PTR + Storage_Offset(READ_OFFSET);
> Buf : Stream_Element_Array( 1 .. Stream_Element_Count( WRITE_OFFSET
> - READ_OFFSET) );
> for Buf'Address use Buf_Addr;
> begin
> POSIX.IO.Write( File=>FileHandle, Buffer=>Buf, Last=>Buf_Last);
> end;
>
> The original code, using the imported function was:
>
> C_WRITE( File => FileHandle, Buffer=> (Q_AREA_PTR +
> Storage_Offset(READ_OFFSET), Num_Bytes=> (WRITE_OFFSET -
> READ_OFFSET));
>
> having to use the declare section to overlay the Stream_Element_Array
> onto the memory segment of the buffer seems much more complicated; so
> I feel like there is likely an easier way to do this, using standard
> Ada, and not having to import the C write function.
>
> Anyone have any suggestions?
with Ada.Streams.Stream_IO;
declare
File : Ada.Streams.Stream_IO.File_Type;
Stream : Ada.Streams.Stream_IO.Stream_Access;
begin
Ada.Streams.Stream_IO.Open (File => File,
Mode => Ada.Streams.Stream_IO.Out_File,
Name => "my_output.bin");
Stream := Ada.Streams.Stream_IO.Stream (File);
Ada.Streams.Write (Stream => Stream,
Item => Buf (1 .. Write_Offset - Read_Offset));
end;
See ARM A.12.1 and 13.13.
The problem you may (or may not) face is converting a POSIX file
descriptor (File_Handle) to a Ada.Streams.Stream_IO.File_Type. If you
know the name of the file, then the problem disappears, as above.
Note that you have not specified what it is that Buf is supposed to
contain; you seem to assume that an explicit conversion to
Stream_Element_Array is necessary. It is not; given a stream you might
as well use the stream-oriented attributes (ARM 13.13.2):
package Data is
type T is private;
private
-- omitted
end Data;
with Ada.Streams.Stream_IO;
with Data;
declare
-- as above
Item : Data.T;
begin
Data.T'Output (Stream, Item); -- emits bounds and discriminants
Data.T'Write (Stream, Item); -- does not emit bounds or discriminants
end;
--
Ludovic Brenta.
next prev parent reply other threads:[~2011-12-15 18:32 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-15 18:13 Writing Data to a file awdorrin
2011-12-15 18:32 ` Ludovic Brenta [this message]
2011-12-15 19:23 ` awdorrin
2011-12-16 0:55 ` Randy Brukardt
2011-12-16 13:30 ` Jacob Sparre Andersen
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox