comp.lang.ada
 help / color / mirror / Atom feed
From: deadlyhead <deadlyhead@gmail.com>
Subject: Defaulting to Standard_Output
Date: Thu, 23 Oct 2008 16:10:32 -0700 (PDT)
Date: 2008-10-23T16:10:32-07:00	[thread overview]
Message-ID: <de5a5558-106c-47bd-8eef-bc99abe27b4f@l33g2000pri.googlegroups.com> (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



             reply	other threads:[~2008-10-23 23:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-23 23:10 deadlyhead [this message]
2008-10-24  0:16 ` Defaulting to Standard_Output 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
replies disabled

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