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: 103376,b5cd7bf26d091c6f X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!goblin1!goblin3!goblin.stu.neva.ru!exi-transit.telstra.net!news.telstra.net!exi-spool.telstra.net!exi-reader.telstra.net!not-for-mail From: "robin" Newsgroups: comp.lang.ada References: Subject: Re: Reading the while standard input into a String Date: Mon, 6 Jun 2011 11:49:40 +1000 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6090 Message-ID: <4dec323f$0$12378$c30e37c6@exi-reader.telstra.net> NNTP-Posting-Host: 123.2.70.40 X-Trace: 1307324991 exi-reader.telstra.net 12378 123.2.70.40:10028 Xref: g2news2.google.com comp.lang.ada:20595 Date: 2011-06-06T11:49:40+10:00 List-Id: "Natasha Kerensikova" wrote in message news:slrniunb6n.i18.lithiumcat@sigil.instinctive.eu... | Hello, | | I have encountered what was a very easily solved problem in C: dump the | whole contents of the standard input into some place in memory, in order | to process it afterwards. It's also trivial in PL/I: do forever; allocate line; get edit (line) (L); end; or read each line into an array element or whatever. | I was quite unhappy with the preprocessing performed by Ada.Text_IO, so | I went on using the text stream interface. However I still read | character by character into a temporary buffer, and it feels ugly. Here | is the code: | | with Ada.Strings.Unbounded; | with Ada.Text_IO.Text_Streams; | | Source : Ada.Text_IO.Text_Streams.Stream_Access | := Ada.Text_IO.Text_Streams.Stream (Ada.Text_IO.Standard_Input); | | procedure Read_Chunk (To : out String; Last : out Natural) is | begin | Last := To'First - 1; | for J in To'Range loop | Character'Read (Source, To (J)); | Last := J; | end loop; | exception | when Ada.Text_IO.End_Error => Null; | end Read_Chunk; | | | procedure The_Program is | -- some tuff | Input : Ada.Strings.Unbounded.Unbounded_String; | begin | declare | Chunk : String (1 .. 256); | Last : Natural; | begin | loop | Read_Chunk (Chunk, Last); | Ada.Strings.Unbounded.Append (Input, Chunk (1 .. Last)); | exit when Last < Chunk'Last; | end loop; | end; | | -- Process Input or To_String (Input); | end The_Program; | | Is there a better/smarter/less ugly way to achieve the same result?