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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,cbf5e026169c215b X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,UTF8 Path: g2news2.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!usenet-fr.net!gegeweb.org!aioe.org!nospam From: "John B. Matthews" Newsgroups: comp.lang.ada Subject: Re: Preferred way to do binray I/O on standard input/output stream Date: Tue, 27 Oct 2009 12:13:54 -0400 Organization: The Wasteland Message-ID: References: <8ec042f2-5308-4a7c-8f48-a6b72ec8feba@p9g2000vbl.googlegroups.com> <0d02cd9f-2cf1-48b6-a10f-6ea84427139d@k4g2000yqb.googlegroups.com> NNTP-Posting-Host: LQJtZWzu+iKlBROuDg+IUg.user.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org X-Notice: Filtered by postfilter v. 0.8.0 Cancel-Lock: sha1:ayKTSMao3c0DIL46oNvyKEj0SKU= User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Xref: g2news2.google.com comp.lang.ada:8809 Date: 2009-10-27T12:13:54-04:00 List-Id: In article , Hibou57 (Yannick DuchĂȘne) 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