From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader01.eternal-september.org!aioe.org!gy7opw3oYmwPg+L8gXeD2w.user.46.165.242.75.POSTED!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: String view of file Date: Mon, 02 Jan 2023 10:57:11 -0800 Organization: Aioe.org NNTP Server Message-ID: <86h6x8pxx4.fsf@stephe-leake.org> References: <10f5dfec-32fb-4333-a9b6-2ee71c1871c0n@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: gioia.aioe.org; logging-data="56535"; posting-host="gy7opw3oYmwPg+L8gXeD2w.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; User-Agent: Gnus/5.13 (Gnus v5.13) X-Notice: Filtered by postfilter v. 0.9.2 Cancel-Lock: sha1:yN5V1Vbzh70OEdheWu+oKciE+P8= Xref: reader01.eternal-september.org comp.lang.ada:64766 List-Id: Jesper Quorning 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