From: "Jeffrey R.Carter" <spam.jrcarter.not@spam.acm.org.not>
Subject: Re: String view of file
Date: Mon, 21 Nov 2022 14:48:47 +0100 [thread overview]
Message-ID: <tlfvjv$3q8j4$1@dont-email.me> (raw)
In-Reply-To: <d1d4d819-8da2-4442-8bf9-a00c7ab6b4a5n@googlegroups.com>
On 2022-11-21 09:30, Jesper Quorning wrote:
>
> Is it possible to write something like this with ADA
"Ada" is a woman's name, not an acronymn.
> package my_rw_file is new file
> (name => "whatever"
> ,mode => read_write
> ,implementation => standard -- or portable or fast
> );
> package as_string is new. xxx(from => my_rw_file);
>
> -- parse (as_string);
> package data is new parse (as_string, format => markdown); -- or whatever
If I presume that the '.' in the declaration of As_String is a typo, what you
have here is a sequence of related generic pkg instantiations, so you can write
this in Ada if you have the corresponding generic pkgs. I have no idea what the
result would provide.
If you want to read the arbitrary contents of a file into a String, that's
easily done:
with Ada.Directories;
package String_A_File is
use type Ada.Directories.File_Size;
function File_As_String (Name : in String) return String with
Pre => Ada.Directories.Exists (Name) and then
Ada.Directories.Size (Name) <=
Ada.Directories.File_Size (Integer'Last),
Post => File_As_String'Result'First = 1 and
File_As_String'Result'Last =
Integer (Ada.Directories.Size (Name) );
end String_A_File;
with Ada.Sequential_IO;
package body String_A_File is
function File_As_String (Name : in String) return String is
subtype FAS is String (1 .. Integer (Ada.Directories.Size (Name) ) );
package FAS_IO is new Ada.Sequential_IO (Element_Type => FAS);
File : FAS_IO.File_Type;
Result : FAS;
begin -- File_As_String
FAS_IO.Open (File => File, Mode => FAS_IO.In_File, Name => Name);
FAS_IO.Read (File => File, Item => Result;
FAS_IO.Close (File => File);
return Result;
end File_As_String;
end String_A_File;
This presumes that Result will fit on the stack. If that's likely to be a
problem, then you will need to use Unbounded_String and read the file Character
by Character.
--
Jeff Carter
"I have a very small head and I had
better learn to live with it ..."
Edsger Dijkstra
158
next prev parent reply other threads:[~2022-11-21 13:48 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 [this message]
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
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