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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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: g2news1.google.com!news3.google.com!feeder3.cambriumusenet.nl!feeder2.cambriumusenet.nl!feed.tweaknews.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!news-1.dfn.de!news.dfn.de!news.uni-weimar.de!not-for-mail From: stefan-lucks@see-the.signature Newsgroups: comp.lang.ada Subject: Re: Reading the while standard input into a String Date: Mon, 6 Jun 2011 10:09:30 +0200 Organization: Bauhaus-Universitaet Weimar Message-ID: References: Reply-To: stefan-lucks@see-the.signature NNTP-Posting-Host: medsec1.medien.uni-weimar.de Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Trace: tigger.scc.uni-weimar.de 1307347061 15926 141.54.178.228 (6 Jun 2011 07:57:41 GMT) X-Complaints-To: news@tigger.scc.uni-weimar.de NNTP-Posting-Date: Mon, 6 Jun 2011 07:57:41 +0000 (UTC) X-X-Sender: lucks@medsec1.medien.uni-weimar.de In-Reply-To: Xref: g2news1.google.com comp.lang.ada:19627 Date: 2011-06-06T10:09:30+02:00 List-Id: > 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. With good the good old function Ada.Text_IO.Get_Line, you can read the input line by line: with Ada.Text_IO; with Ada.Strings.Unbounded; procedure Process_File is use Ada.Strings.Unbounded; function Input_To_String(Between_Lines: String) return String is Representative_Of_File: Unbounded_String; U: Unbounded_String; begin begin loop declare Chunk: String := Ada.Text_IO.Get_Line; begin Append(U, Between_Lines & Chunk); end; end loop; exception when Ada.Text_IO.End_Error => null; end; return To_String(U); end Input_To_String; begin Ada.Text_IO.Put_Line("

" & Input_To_String("

") & "

"); end Process_File; This works nicely. But, due to the usage of Ada.Text_IO and Ada.Strings.Unbounded, the efficiency is likely to be mediocre only. (I think, gnat will reallocate a larger unbounded string, copy the old string into the new one, and then free the old string, each time it appends something to U.) Running the program on a sample 4-line input file gives

Line 1 is good.

Line 2 is better!

Line 4 follows an empty line.

-- ------ Stefan Lucks -- Bauhaus-University Weimar -- Germany ------ Stefan dot Lucks at uni minus weimar dot de ------ I love the taste of Cryptanalysis in the morning! ------