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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c9019477667b9c0f X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.190.2 with SMTP id gm2mr5695997pbc.4.1323973958853; Thu, 15 Dec 2011 10:32:38 -0800 (PST) Path: lh20ni26182pbb.0!nntp.google.com!news2.google.com!goblin2!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Writing Data to a file Date: Thu, 15 Dec 2011 19:32:37 +0100 Organization: A noiseless patient Spider Message-ID: <87zketwure.fsf@ludovic-brenta.org> References: <344d32d6-a667-407c-bb8e-e0c504e91688@u32g2000yqe.googlegroups.com> Mime-Version: 1.0 Injection-Info: mx04.eternal-september.org; posting-host="i4XNpZB8cDVUWjvJ5M//bQ"; logging-data="6437"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OZfo5byNSC7BwAsP4sAhQ" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) Cancel-Lock: sha1:jSi6QyzPe3hJ+KR09u22TTvvFhU= sha1:BcWwGbr9K/PMhqZFJSy0/xg3eg4= Content-Type: text/plain; charset=us-ascii Date: 2011-12-15T19:32:37+01:00 List-Id: awdorrin 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.