comp.lang.ada
 help / color / mirror / Atom feed
* Defaulting to Standard_Output
@ 2008-10-23 23:10 deadlyhead
  2008-10-24  0:16 ` Jeffrey R. Carter
  0 siblings, 1 reply; 7+ messages in thread
From: deadlyhead @ 2008-10-23 23:10 UTC (permalink / raw)


I'm writing a text processing program in the Unix tradition where, if
given no specific output file, all output is directed to
Standard_Output.  I've found this to be confusing as File_Type is
limited private.  Thus operations such as Outfile := Standard_Output
don't work.  I tried using File_Access for my Outfile object, but that
proved to be cumbersome as I was having a lot of trouble with
scoping.  My solution is as follows:

-------------------
-- begin example --
-------------------

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded;
use  Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO;
use  Ada.Text_IO.Unbounded_IO;

procedure Default_Output is

   --  our output file
   Outfile : File_Type;

   --  for getting the file name
   Name : Unbounded_String;

   -- rename the Text_IO function Put
   procedure TIO_Put (File : in File_Type;
		      Item : in String) renames Ada.Text_IO.Put;

   procedure Put (File : in File_Type; Item : in String) is
   begin
      -- Check to see if File has been Open'ed
      -- If not, send to Standard_Output
      if Is_Open (File) then
	 TIO_Put (File, Item);
      else
	 Put (Item);			--  to Standard_Output
      end if;
   end Put;

begin  -- Default_Output

   Put ("Name of file to process: ");
   Get_Line (Name);

   --  Open the file if we have a file name
   if Name /= "" then
      Create (Outfile, Out_File,
	      Slice (Name, 1,
		     Index (Name, " ") - 1));
   end if;

   Put (Outfile, "Default_Output output!");

   if Is_Open (Outfile) then
      Close (Outfile);
   end if;

end Default_Output;

-----------------
-- end example --
-----------------

I did not actually use the Ada.Strings.Unbounded procedures is my
code, and I'm posting this from a machine where I cannot validate what
I've written, but it looks right to me.

If anybody has a better way of handling defaulting to Standard_Output,
I'd like to see it.  This works for me, and seems elegant enough.  It
avoids a lot of messy conditionals in the body, anyway.

-- deadlyhead



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

* Re: Defaulting to Standard_Output
  2008-10-23 23:10 Defaulting to Standard_Output deadlyhead
@ 2008-10-24  0:16 ` Jeffrey R. Carter
  2008-10-24  1:14   ` deadlyhead
  0 siblings, 1 reply; 7+ messages in thread
From: Jeffrey R. Carter @ 2008-10-24  0:16 UTC (permalink / raw)


deadlyhead wrote:
> 
> If anybody has a better way of handling defaulting to Standard_Output,
> I'd like to see it.  This works for me, and seems elegant enough.  It
> avoids a lot of messy conditionals in the body, anyway.

This is what Ada.Text_IO.Set_Output is for.

-- 
Jeff Carter
"When danger reared its ugly head, he bravely
turned his tail and fled."
Monty Python and the Holy Grail
60



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

* Re: Defaulting to Standard_Output
  2008-10-24  0:16 ` Jeffrey R. Carter
@ 2008-10-24  1:14   ` deadlyhead
  2008-10-24  1:49     ` Adam Beneschan
  0 siblings, 1 reply; 7+ messages in thread
From: deadlyhead @ 2008-10-24  1:14 UTC (permalink / raw)


On Oct 23, 5:16 pm, "Jeffrey R. Carter"
<spam.jrcarter....@spam.acm.org> wrote:
> deadlyhead wrote:
>
> > If anybody has a better way of handling defaulting to Standard_Output,
> > I'd like to see it.  This works for me, and seems elegant enough.  It
> > avoids a lot of messy conditionals in the body, anyway.
>
> This is what Ada.Text_IO.Set_Output is for.
>
> --
> Jeff Carter
> "When danger reared its ugly head, he bravely
> turned his tail and fled."
> Monty Python and the Holy Grail
> 60

I looked over Set_Output in the ARM and I went back looking for it in
the comp.lang.ada, and it looks to me that Set_Output will direct all
Put statements to Outfile if a file isn't designated, rather than
having calls of Put (Outfile, Item) appear in Standard_Output.

This doesn't appear to be the functionality I'm looking for.  If no
file is specifically named for Outfile, I want all output to appear on
the terminal.  If a file is named for Outfile, then direct output to
that.  Using Set_Output (Outfile), wouldn't I still need a Create
statement for Outfile?

-- deadlyhead



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

* Re: Defaulting to Standard_Output
  2008-10-24  1:14   ` deadlyhead
@ 2008-10-24  1:49     ` Adam Beneschan
  2008-10-24  2:57       ` deadlyhead
  0 siblings, 1 reply; 7+ messages in thread
From: Adam Beneschan @ 2008-10-24  1:49 UTC (permalink / raw)


On Oct 23, 6:14 pm, deadlyhead <deadlyh...@gmail.com> wrote:
> On Oct 23, 5:16 pm, "Jeffrey R. Carter"
>
> <spam.jrcarter....@spam.acm.org> wrote:
> > deadlyhead wrote:
>
> > > If anybody has a better way of handling defaulting to Standard_Output,
> > > I'd like to see it.  This works for me, and seems elegant enough.  It
> > > avoids a lot of messy conditionals in the body, anyway.
>
> > This is what Ada.Text_IO.Set_Output is for.

> I looked over Set_Output in the ARM and I went back looking for it in
> the comp.lang.ada, and it looks to me that Set_Output will direct all
> Put statements to Outfile if a file isn't designated, rather than
> having calls of Put (Outfile, Item) appear in Standard_Output.
>
> This doesn't appear to be the functionality I'm looking for.  If no
> file is specifically named for Outfile, I want all output to appear on
> the terminal.  If a file is named for Outfile, then direct output to
> that.  Using Set_Output (Outfile), wouldn't I still need a Create
> statement for Outfile?

Well, yes.  If you want to create a file, you need to call Create.
There isn't anything in the Ada libraries that I know of that will
create a file without you telling it to.  But why would that be a
problem?  If Name /= "", call Create, then call Set_Output.

                              -- Adam




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

* Re: Defaulting to Standard_Output
  2008-10-24  1:49     ` Adam Beneschan
@ 2008-10-24  2:57       ` deadlyhead
  2008-10-24  3:57         ` Jeffrey R. Carter
  0 siblings, 1 reply; 7+ messages in thread
From: deadlyhead @ 2008-10-24  2:57 UTC (permalink / raw)


On Oct 23, 6:49 pm, Adam Beneschan <a...@irvine.com> wrote:

>
> Well, yes.  If you want to create a file, you need to call Create.
> There isn't anything in the Ada libraries that I know of that will
> create a file without you telling it to.  But why would that be a
> problem?  If Name /= "", call Create, then call Set_Output.
>
>                               -- Adam

Maybe I'm missing something here.  If I use Set_Output (Outfile), all
calls to Put or Put_Line without a File parameter go to Outfile,
correct?  They do not got to the Terminal/Standard_Output, right?

This is not what I want.  I want all output of the program to go to
Standard_Output if no name is given for Outfile.  It's like if `cat`
had an option '-o' that designated an output file.  `cat something.txt
-o outfile.txt` = `cat something.txt > outfile.txt`, but `cat
something.txt` outputs to the terminal.  That's the type of
functionality that I want.

Maybe it's just a pain to do, which is why no other programs are set
up like this.

-- deadlyhead



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

* Re: Defaulting to Standard_Output
  2008-10-24  2:57       ` deadlyhead
@ 2008-10-24  3:57         ` Jeffrey R. Carter
  2008-10-24  6:28           ` deadlyhead
  0 siblings, 1 reply; 7+ messages in thread
From: Jeffrey R. Carter @ 2008-10-24  3:57 UTC (permalink / raw)


deadlyhead wrote:
> 
> This is not what I want.  I want all output of the program to go to
> Standard_Output if no name is given for Outfile.  It's like if `cat`
> had an option '-o' that designated an output file.  `cat something.txt
> -o outfile.txt` = `cat something.txt > outfile.txt`, but `cat
> something.txt` outputs to the terminal.  That's the type of
> functionality that I want.

It works like this:

if File_Name /= "" then
    Create (File);
    Set_Output (File);
end if;

-- call Put, Put_Line, and New_Line without a File parameter

if File_Name /= "" then
    Close (File);
end if;

If the user specifies a file, then output goes to that file. If he doesn't, then 
output goes to Standard_Output.

-- 
Jeff Carter
"When danger reared its ugly head, he bravely
turned his tail and fled."
Monty Python and the Holy Grail
60



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

* Re: Defaulting to Standard_Output
  2008-10-24  3:57         ` Jeffrey R. Carter
@ 2008-10-24  6:28           ` deadlyhead
  0 siblings, 0 replies; 7+ messages in thread
From: deadlyhead @ 2008-10-24  6:28 UTC (permalink / raw)


On Oct 23, 8:57 pm, "Jeffrey R. Carter"
<spam.jrcarter....@spam.acm.org> wrote:
> deadlyhead wrote:
>
> > This is not what I want.  I want all output of the program to go to
> > Standard_Output if no name is given for Outfile.  It's like if `cat`
> > had an option '-o' that designated an output file.  `cat something.txt
> > -o outfile.txt` = `cat something.txt > outfile.txt`, but `cat
> > something.txt` outputs to the terminal.  That's the type of
> > functionality that I want.
>
> It works like this:
>
> if File_Name /= "" then
>     Create (File);
>     Set_Output (File);
> end if;
>
> -- call Put, Put_Line, and New_Line without a File parameter
>
> if File_Name /= "" then
>     Close (File);
> end if;
>
> If the user specifies a file, then output goes to that file. If he doesn't, then
> output goes to Standard_Output.
>
> --
> Jeff Carter
> "When danger reared its ugly head, he bravely
> turned his tail and fled."
> Monty Python and the Holy Grail
> 60

All right, now I feel like an idiot.

That's pretty damn simple.  I was thinking of it in terms of Scheme
(which is really the only language that I've done serious file
handling with).  There, of course, there's no restriction on passing
around references to stdout and the like, thus I could just (set!
outfile (current-output-port)).  The method you have been advocating
makes a lot of sense.  I'll be changing my code for that, then.

Thanks!

-- deadlyhead



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

end of thread, other threads:[~2008-10-24  6:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-23 23:10 Defaulting to Standard_Output deadlyhead
2008-10-24  0:16 ` Jeffrey R. Carter
2008-10-24  1:14   ` deadlyhead
2008-10-24  1:49     ` Adam Beneschan
2008-10-24  2:57       ` deadlyhead
2008-10-24  3:57         ` Jeffrey R. Carter
2008-10-24  6:28           ` deadlyhead

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