comp.lang.ada
 help / color / mirror / Atom feed
* Is it possible to redirect text output to a String or Unbounded_String?
@ 2021-06-10  2:17 Jerry
  2021-06-10  6:50 ` Simon Wright
                   ` (6 more replies)
  0 siblings, 7 replies; 10+ messages in thread
From: Jerry @ 2021-06-10  2:17 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Is it possible to redirect text output to a String or Unbounded_String?
  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
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Simon Wright @ 2021-06-10  6:50 UTC (permalink / raw)


Jerry <list_email@icloud.com> writes:

> Is it possible to redirect text output to a String or Unbounded_String
> rather than to a file?

I think you could use Ada.Text_IO.Text_Streams, except it's the wrong
way round (treat a file as a stream). If you could treat a stream as a
file you could use Text_IO to write to the file view and suck the stream
view out into whatever format you wanted. Maybe.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Is it possible to redirect text output to a String or Unbounded_String?
  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
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 10+ messages in thread
From: Dmitry A. Kazakov @ 2021-06-10  7:00 UTC (permalink / raw)


On 2021-06-10 04:17, Jerry wrote:
> Is it possible to redirect text output to a String or Unbounded_String rather than to a file?

 From Ada.Text_IO? No you cannot, File_Type is not tagged. For a stream 
you could.

> 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 would say that one should never use anything but Put_Line and never 
instantiate any I/O packages. First format an output line in a string, 
and then output the string.

I would suggest to define logging/tracing package, instead of doing 
direct output. It always pays off.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Is it possible to redirect text output to a String or Unbounded_String?
  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-10  8:31 ` ytomino
  2021-06-10  9:09 ` Jeffrey R. Carter
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: ytomino @ 2021-06-10  8:31 UTC (permalink / raw)


On Thursday, June 10, 2021 at 11:17:20 AM UTC+9, 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

If you are not particular about use of String or Unbounded_String, how about open_memstream(3) of libc?
Import and call open_memstream to create a C-style on-memory file at first, then you can create an Ada-style File_Type from it with Ada.Text_IO.C_Streams.Open.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Is it possible to redirect text output to a String or Unbounded_String?
  2021-06-10  2:17 Is it possible to redirect text output to a String or Unbounded_String? Jerry
                   ` (2 preceding siblings ...)
  2021-06-10  8:31 ` ytomino
@ 2021-06-10  9:09 ` Jeffrey R. Carter
  2021-06-10 14:42 ` Stéphane Rivière
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Jeffrey R. Carter @ 2021-06-10  9:09 UTC (permalink / raw)


On 6/10/21 4:17 AM, Jerry wrote:
> Is it possible to redirect text output to a String or Unbounded_String rather than to a file?

This is an abstraction that does not exist in the language, presumably because 
it is not commonly needed. But you can easily implement this abstraction in Ada 
if you need it.

-- 
Jeff Carter
"I'll get broads up here like you wouldn't believe.
Swingers. Freaks. Nymphomaniacs. Dental hygienists."
Play It Again, Sam
125

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Is it possible to redirect text output to a String or Unbounded_String?
  2021-06-10  2:17 Is it possible to redirect text output to a String or Unbounded_String? Jerry
                   ` (3 preceding siblings ...)
  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
  6 siblings, 1 reply; 10+ messages in thread
From: Stéphane Rivière @ 2021-06-10 14:42 UTC (permalink / raw)



Le 10/06/2021 à 04:17, Jerry a écrit :
> Is it possible to redirect text output to a String or Unbounded_String
rather than to a file?

Hi Jerry,

I'm not shure to yunderstand your need... If your goal is to redirect
(catch) the text output of a command system to an Ada variable...


Page 33 in :
https://stef.genesix.org/pub/ada/aide/sow%20-%20v20%20Ada%20Library%20User%20Manual%20v28.pdf

7.9     Shell_Execute

• Description

Executes shell command. Return the exit code if passed from the executed
com-mand. Without Output parameter, the command console output is
displayed by default but can be redirected. If Output is used, then the
executed command output is return in this parameter.


code is in v20.sys.ad* in https://stef.genesix.org/pub/ada/aide/v20-0.3.zip


Notes :
   VString is Unbounded_String
   package GE renames GNAT.Expect;
   package GOL renames GNAT.OS_Lib;



Body (abstract)

procedure Shell_Execute_Output (Command : String;
                                   Result : out Integer;
                                   Output : out VString) is
      Arguments : GOL.Argument_List_Access;
      Command_Exit_Code : aliased Integer; --  Must reside in memory
(pointer)
   begin
      if Command /= "" then
         Arguments := GOL.Argument_String_To_List (Command);
         Output := To_VString (GE.Get_Command_Output
               (Command => Arguments (Arguments'First).all,
              Arguments => Arguments (Arguments'First + 1 ..
Arguments'Last),
              Input => "",
              Status => Command_Exit_Code'Access));
         GOL.Free (Arguments);
      else
         Output := +"";
      end if;
      Result := Command_Exit_Code;
   end Shell_Execute_Output;

   procedure Shell_Execute_Output (Command : VString;
                                   Result : out Integer;
                                   Output : out VString) is
   begin
      Shell_Execute_Output (To_String (Command), Result, Output);
   end Shell_Execute_Output;

-- 
Be Seeing You
Number Six

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Is it possible to redirect text output to a String or Unbounded_String?
  2021-06-10  2:17 Is it possible to redirect text output to a String or Unbounded_String? Jerry
                   ` (4 preceding siblings ...)
  2021-06-10 14:42 ` Stéphane Rivière
@ 2021-06-10 17:06 ` Marius Amado-Alves
  2021-06-10 21:12 ` Shark8
  6 siblings, 0 replies; 10+ messages in thread
From: Marius Amado-Alves @ 2021-06-10 17:06 UTC (permalink / raw)


So many overcomplicated "solutions" given here, streams, command calls...
Simply write your own Put procedure which output you can redirect either to the terminal or to an Unbounded_String.

package Redirectionable_Output is
   Data: Unbounded_String;
   To_Variable: Boolean;
   procedure Put (C: Character);
end;

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Is it possible to redirect text output to a String or Unbounded_String?
  2021-06-10  2:17 Is it possible to redirect text output to a String or Unbounded_String? Jerry
                   ` (5 preceding siblings ...)
  2021-06-10 17:06 ` Marius Amado-Alves
@ 2021-06-10 21:12 ` Shark8
  6 siblings, 0 replies; 10+ messages in thread
From: Shark8 @ 2021-06-10 21:12 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Is it possible to redirect text output to a String or Unbounded_String?
  2021-06-10 14:42 ` Stéphane Rivière
@ 2021-06-11  6:51   ` Stéphane Rivière
  0 siblings, 0 replies; 10+ messages in thread
From: Stéphane Rivière @ 2021-06-11  6:51 UTC (permalink / raw)


(sorry for typos in the previous post)

Not shure to understand your need... Do your your goal is to redirect
the text output of a system command to an Ada variable ?

-- 
Be Seeing You
Number Six

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Is it possible to redirect text output to a String or Unbounded_String?
  2021-06-10  7:00 ` Dmitry A. Kazakov
@ 2021-06-11 13:07   ` Stephane Carrez
  0 siblings, 0 replies; 10+ messages in thread
From: Stephane Carrez @ 2021-06-11 13:07 UTC (permalink / raw)


Hi!

Le jeudi 10 juin 2021 à 09:00:31 UTC+2, Dmitry A. Kazakov a écrit :
> On 2021-06-10 04:17, Jerry wrote: 
> > Is it possible to redirect text output to a String or Unbounded_String rather than to a file?
> From Ada.Text_IO? No you cannot, File_Type is not tagged. For a stream 
> you could.

Tero Koskinen has made an interesting implementation in Ahven for that.

Basically he redirects the standard output to a file for the execution of the unit test method.
The unit test method executes and can use the Ada.Text_IO without change.
After the unit test completion, it reads the file.

If I'm not wrong, have a look at the Ahven.Temporary_Output package:

https://hg.sr.ht/~tkoskine/ahven/browse/src/ahven-temporary_output.ads?rev=tip
https://hg.sr.ht/~tkoskine/ahven/browse/src/ahven-temporary_output.adb?rev=tip

Best regards,

Stephane

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-06-11 13:07 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox