From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,624c77c4e3ffaa2,start X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!l33g2000pri.googlegroups.com!not-for-mail From: deadlyhead Newsgroups: comp.lang.ada Subject: Defaulting to Standard_Output Date: Thu, 23 Oct 2008 16:10:32 -0700 (PDT) Organization: http://groups.google.com Message-ID: NNTP-Posting-Host: 216.57.220.9 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1224803433 24429 127.0.0.1 (23 Oct 2008 23:10:33 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 23 Oct 2008 23:10:33 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: l33g2000pri.googlegroups.com; posting-host=216.57.220.9; posting-account=snJuNwoAAABnc8T9lYkBlDQrDdSjOjG2 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727),gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:2475 Date: 2008-10-23T16:10:32-07:00 List-Id: 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