comp.lang.ada
 help / color / mirror / Atom feed
From: "John B. Matthews" <nospam@nospam.invalid>
Subject: Re: Preferred way to do binray I/O on standard input/output stream
Date: Tue, 27 Oct 2009 12:13:54 -0400
Date: 2009-10-27T12:13:54-04:00	[thread overview]
Message-ID: <nospam-009790.12135427102009@news.aioe.org> (raw)
In-Reply-To: a558f6b4-8f4c-459d-8706-4eb8125dae1e@w19g2000yqk.googlegroups.com

In article 
<a558f6b4-8f4c-459d-8706-4eb8125dae1e@w19g2000yqk.googlegroups.com>,
 Hibou57 (Yannick Duchêne) <yannick_duchene@yahoo.fr> wrote:

> The trouble with this, is that this force a look-ahead : an item
> must be read to know if an item is available, and this can lead
> into numerous logical traps (I prefer to keep distinct the action
> of reading and testing availability of data).

Ah, I see your point. I recall this problem going back to the days of 
UCSD Pascal, which distinguished between interactive and text type files 
for this very reason. My use is to allow command line utilities to read 
from standard input if no file name is supplied. For me, the data stream  
invariably arrives via redirection or a pipe, so the problem does not 
arise. For interactive programming, I typically use GtkAda.

This variation of Copy makes it easier to see what's happening. 
Prefacing the loop with the End_Of_File predicate exposes the problem 
for files with a terminal LF. If running interactively, control-D exits:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO.Text_Streams;

procedure Copy is
   Stream_Ptr : Text_Streams.Stream_Access;
   C : Character;

   function Hex(C : Character) return String is
      H : constant String := "0123456789ABCDEF";
      B : Natural := Character'Pos(C);
      S : String(1 .. 4);
   begin
      S(1) := '[';
      S(2) := H(B / 16 + 1);
      S(3) := H(B mod 16 + 1);
      S(4) := ']';
      return S;
   end Hex;

begin
   Stream_Ptr := Text_Streams.Stream(Current_Input);
-- while not End_Of_File loop
   loop
      Character'Read(Stream_Ptr, C);
      Put(Hex(C));
   end loop;
exception
   when End_Error => null;
end Copy;

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>



      reply	other threads:[~2009-10-27 16:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-24 22:07 Preferred way to do binray I/O on standard input/output stream Hibou57 (Yannick Duchêne)
2009-10-24 22:57 ` Jeffrey R. Carter
2009-10-24 23:22   ` Hibou57 (Yannick Duchêne)
2009-10-27  0:34   ` Hibou57 (Yannick Duchêne)
2009-10-27  1:14     ` John B. Matthews
2009-10-27  2:36       ` Hibou57 (Yannick Duchêne)
2009-10-27 16:13         ` John B. Matthews [this message]
replies disabled

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