From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.5-pre1 X-Received: by 2002:a05:620a:1f7:: with SMTP id x23mr644330qkn.160.1623359573403; Thu, 10 Jun 2021 14:12:53 -0700 (PDT) X-Received: by 2002:a25:cb48:: with SMTP id b69mr1100977ybg.173.1623359573251; Thu, 10 Jun 2021 14:12:53 -0700 (PDT) Path: eternal-september.org!reader02.eternal-september.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 10 Jun 2021 14:12:53 -0700 (PDT) In-Reply-To: <331aa8fb-271a-4a3a-b95e-d10733cbc404n@googlegroups.com> Injection-Info: google-groups.googlegroups.com; posting-host=146.5.2.231; posting-account=lJ3JNwoAAAAQfH3VV9vttJLkThaxtTfC NNTP-Posting-Host: 146.5.2.231 References: <331aa8fb-271a-4a3a-b95e-d10733cbc404n@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <054f80ff-03b0-45d8-9bd5-6932bf67fc94n@googlegroups.com> Subject: Re: Is it possible to redirect text output to a String or Unbounded_String? From: Shark8 Injection-Date: Thu, 10 Jun 2021 21:12:53 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader02.eternal-september.org comp.lang.ada:62170 List-Id: On Wednesday, June 9, 2021 at 8:17:20 PM UTC-6, Jerry wrote: > Is it possible to redirect text output to a String or Unbounded_String rather than to a file? > > I know there are versions of Put that take a string as a first argument but I don't want to have to write a special-purpose version of a stretch of code just for this purpose, since I want to keep the ability to also output text to a terminal. > > I suppose I could redirect text to a file and then somehow read it back in as one string but that seems like a kludge. > > Jerry Well... ------------------------------------ Generic Type FILE_TYPE(<>) is limited private; Type FILE_MODE is (<>); Type Stream_Access is access all Ada.Streams.Root_Stream_Type'Class; with Procedure Indirect_Put_Line(Item : String); -- put all the subprograms you need here; prefix with Indirect_ Object : in out FILE_TYPE; Stream : in Stream_Access; Package Duplicating_IO is -- Declare all the subprograms used above w/o Indirect_ Procedure Put_Line(Item : String); End Duplicating_IO; ------------------------------------ Package Body Duplicating_IO is -- Implementations; if there's a lot consider generics+renames. Procedure Put_Line(Item : String) is Begin String'Output( Stream, Item ); Indirect_Put_Line( Item ); End Put_Line; End Duplicating_IO; ------------------------------------ OBJ : Ada.Text_IO.File_Type:= Ada.Text_IO.Standard_Output; STR : Ada.Text_IO.Text_Streams.Stream_Access renames Ada.Text_IO.Text_Streams.Stream( OBJ ); ------------------------------------ Package Split_IO is new Duplicating_IO ( File_Type => File_Type, File_Mode => File_Mode, Stream_Access => Ada.Text_IO.Text_Streams.Stream_Access, Indirect_Put_Line => Put_Line, Object => OBJ, Stream => STR );