comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Is it possible to redirect text output to a String or Unbounded_String?
Date: Thu, 10 Jun 2021 14:12:53 -0700 (PDT)	[thread overview]
Message-ID: <054f80ff-03b0-45d8-9bd5-6932bf67fc94n@googlegroups.com> (raw)
In-Reply-To: <331aa8fb-271a-4a3a-b95e-d10733cbc404n@googlegroups.com>

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
      );

      parent reply	other threads:[~2021-06-10 21:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-10  2:17 Is it possible to redirect text output to a String or Unbounded_String? Jerry
2021-06-10  6:50 ` Simon Wright
2021-06-10  7:00 ` Dmitry A. Kazakov
2021-06-11 13:07   ` Stephane Carrez
2021-06-10  8:31 ` ytomino
2021-06-10  9:09 ` Jeffrey R. Carter
2021-06-10 14:42 ` Stéphane Rivière
2021-06-11  6:51   ` Stéphane Rivière
2021-06-10 17:06 ` Marius Amado-Alves
2021-06-10 21:12 ` Shark8 [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox