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

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