comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Re: String view of file
Date: Mon, 02 Jan 2023 10:57:11 -0800	[thread overview]
Message-ID: <86h6x8pxx4.fsf@stephe-leake.org> (raw)
In-Reply-To: 10f5dfec-32fb-4333-a9b6-2ee71c1871c0n@googlegroups.com

Jesper Quorning <jesper.quorning@gmail.com> writes:

> The original post was a bit of a mess, and I am not quite new to Ada. But I lack some skills regarding software construction and software engineering.
>
> What I want goes something like this:
> - Zero copy access to a (read-only) file.

You can use gnatcoll mmap to get a memory-mapped view of a file. To get
a zero-copy string, you'll have to find the line boundaries yourself,
and use unchecked-conversion on a string pointer, or an address clause
on a local variable.

> - Get lines of file one by one as a stream of lines.

'stream' has a specific meaning in Ada; better say "collection of lines"
here.

You can define an iterator for your File type.

- Use a package instance as file representation. 

Why do you want to do this? The alternative is to define your own File
type, and provide an Open; then you can reuse that package for multiple
files. Closer to Ada standard practice for files.

- Stretch goal: Multiple implementations of File Instance (Standard, Fast, Compatible ..)

If you want to choose the implementation at compile time, you can use
separate bodies in implementation-specific directories, and choose the
directory in the project file.

If you want to chose the implementation at run time, you have to make
File a dispatching type. Then you pick the implementation via the
package name:

My_File : Files.Implementation_1.File;

> for Line of File.As_Line_Stream loop
>    Parse (Line);
> end loop;

You probably know that you can get close to this in standard Ada, at the
cost of a copy:

while not End_of_file (File) loop
   declare
      Line : get_line (file);
   begin
      Parse (line);
   end;
end loop;

An really aggressive optimizing compiler could get rid of the local
variable Line to avoid the copy, but it's not very likely.

This violates your requirement, but it could easily be that the copy is
very cheap; have you measured it in your actual application, and is
eliminating it worth the bother?

-- 
-- Stephe

  reply	other threads:[~2023-01-02 18:57 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-21  8:30 String view of file Jesper Quorning
2022-11-21 13:01 ` G.B.
2022-11-21 13:48 ` Jeffrey R.Carter
2022-11-21 15:52   ` Niklas Holsti
2022-11-21 16:42     ` Jeffrey R.Carter
2022-11-21 17:29       ` Niklas Holsti
2022-11-21 15:18 ` Dmitry A. Kazakov
2022-11-21 16:11 ` Marius Amado-Alves
2022-11-21 17:29 ` Qunying
2022-11-21 21:43 ` Gautier write-only address
2023-01-01 23:36 ` Jesper Quorning
2023-01-02 18:57   ` Stephen Leake [this message]
2023-01-03 16:37   ` Jeffrey R.Carter
2023-01-03 17:02     ` Dmitry A. Kazakov
replies disabled

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