comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
To: <comp.lang.ada@ada-france.org>
Subject: RE: Text Processing in Ada 95
Date: Thu, 22 Feb 2007 23:19:58 -0600
Date: 2007-02-23T06:20:37+01:00	[thread overview]
Message-ID: <mailman.107.1172207939.18371.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <N_OdnWDOLKHm7kPYnZ2dnUVZ_segnZ2d@comcast.com>

Steve writes:

...
> Actually Direct_IO is an option, and probably the fastest way to
> handle the
> operation.
>
>   Step 1. Determine the initial file size
>   Step 2. Allocate a buffer that is the size of the file plus the size of
>     the string you want to add (including a line terminator)
>   Step 3. Create an instance Direct_IO that is the file size
>   Step 5. Read the file into the start of the allocated buffer in one gulp
>   Step 6. Insert your string in the buffer (a little tricky, but doable)
>   Step 7. Create an instance of Direct_IO that is the size of the buffer
>     with the new string
>   Step 8. Write the buffer to a file as one operation.

That's how you'd do it in Ada 83, but that's an awful lot of unnecessary
complication in Ada 95 (not to mention Ada 2007). Just use Stream_IO for
this, and you don't need instances to fill and write your buffer. (And you
can easily start in the middle of the file and only read part of it if that
works for your application.)

I.e.

>   Step 1. Determine the initial file size
    Use Stream_IO.Size(File).
>   Step 2. Allocate a buffer that is the size of the file plus the size of
>     the string you want to add (including a line terminator)
    Buffer : Stream_Element_Array (1 .. Size); -- But you can make it
bigger.
>   Step 3. Create an instance Direct_IO that is the file size
    null;
>   Step 5. Read the file into the start of the allocated buffer in one gulp
    Stream_IO.Read (File, Buffer, Last);
>   Step 6. Insert your string in the buffer (a little tricky, but doable)
    Exercise for the reader. ;-)
>   Step 7. Create an instance of Direct_IO that is the size of the buffer
>     with the new string
    null;
>   Step 8. Write the buffer to a file as one operation.
    Stream_IO.Set_Mode(File, Out_File); -- Or Reset.
    Stream_IO.Write(File, Buffer);

                          Randy.




  reply	other threads:[~2007-02-23  5:19 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-21 18:09 Text Processing in Ada 95 Rob Norris
2007-02-21 19:07 ` Pascal Obry
2007-02-22 14:02   ` Larry Kilgallen
2007-02-21 20:16 ` Randy Brukardt
2007-02-22  2:45 ` Jeffrey R. Carter
2007-02-22 11:56 ` Rob Norris
2007-02-23  4:30   ` Jeffrey R. Carter
2007-02-23 13:51   ` Stephen Leake
2007-02-26 12:11     ` Rob Norris
2007-02-23  4:55 ` Steve
2007-02-23  5:19   ` Randy Brukardt [this message]
2007-02-23  7:53 ` 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