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=-3.2 required=3.0 tests=BAYES_00,NICE_REPLY_A, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R.Carter" Newsgroups: comp.lang.ada Subject: Re: String view of file Date: Mon, 21 Nov 2022 14:48:47 +0100 Organization: A noiseless patient Spider Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 21 Nov 2022 13:48:48 -0000 (UTC) Injection-Info: reader01.eternal-september.org; posting-host="656c6272d9b5e625a532946caa503524"; logging-data="4006500"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+UsaqU5lSQCHdFDfyb3DtP0MalyOvWtHw=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Cancel-Lock: sha1:AxDxf1p3tsgjEMOA4KzlfooeuYE= In-Reply-To: Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:64648 List-Id: 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