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!news.szaf.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: String view of file Date: Mon, 21 Nov 2022 17:52:14 +0200 Organization: Tidorum Ltd Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net UrD2FniMJDFXwpyVlJqKMA3GuQPzf5EvfhpWNAfEa69QiZfLG4 Cancel-Lock: sha1:dD4nMTodxvvHkMQLGVNJD51Cxds= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Content-Language: en-US In-Reply-To: Xref: reader01.eternal-september.org comp.lang.ada:64650 List-Id: On 2022-11-21 15:48, Jeffrey R.Carter wrote: > 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. For the OP's benefit (Jeffrey of course knows this): an alternative to Unbounded_String is to allocate the Result string on the heap, and return an access to the heap string. With that method, you can still read the entire string with one call of FAS_IO.Read instead of Character by Character.