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,56e5e06428166864 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!q36g2000yqn.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: problems with interfacing c Date: Fri, 28 Jan 2011 01:41:33 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <8b6a5b24-5ab0-4d38-9be8-86911aba7fcf@k22g2000yqh.googlegroups.com> <13d5wy5ni51xh$.xnhoi3uhm9ql.dlg@40tude.net> <85c958cc-8cd3-46be-b14d-3519ff207d87@b8g2000vbi.googlegroups.com> NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1296207693 28114 127.0.0.1 (28 Jan 2011 09:41:33 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 28 Jan 2011 09:41:33 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: q36g2000yqn.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:17733 Date: 2011-01-28T01:41:33-08:00 List-Id: Staszek Goldstein wrote on comp.lang.ada: > I am sending a solution to a problem to a site managing programming contests, and > the stdin is in fact redirected to some text file with data. I do not know how > big the data file will be, I know only the upper limit. Quick reading of the data is the > key to success, and the two lines of C work very well for the purpose. I have not > tried your solution, is it not going to cause constraint_error when it meets the end of the > file? If you really read from Standard_Input, there is no upper limit to the size of the file, so you should not depend on one. i.e. your program should work if you say: $ yes | my_program The proper way to read a potentially infinite amount of data from Standard_Input is to read one character at a time, buffer the input for processing, and discard the buffer from time to time so your program runs in constant memory. The points in time where you discard the buffer depend on the algorithm. Like Dmitry, I strongly suggest you use Ada.Streams, not Ada.Text_IO, because the latter is much slower (it does a lot of unnecessary bookkeeping behind the scenes). Here is a small example where I process the input buffer whenever it is full and then discard it. I also do that when I reach the end of the input stream. Note that I have not compiled this example, so it may contain (intentional :)) bugs. with Ada.IO_Exceptions; with Ada.Text_IO; with Ada.Strings.Bounded; procedure My_Program is Current_Input : constant Ada.Text_IO.Text_Streams.Stream_Access := Ada.Text_IO.Text_Streams.Stream (Ada.Text_IO.Current_Input); package Buffers is new Ada.Strings.Bounded.Generic_Bounded_Length (Max => 100_000); procedure Process_And_Discard (Buffer : in out Buffers.Bounded_String) is begin -- actual processing left as an exercise for the reader :) Buffers.Delete (Source => Buffer, From => 1, Through => Buffers.Max_Length); end Process_And_Discard; Buffer : Buffers.Bounded_String; begin loop declare C : Character; begin Character'Read (Current_Input, C); if Buffers.Length (Buffer) = Buffers.Max_Length then -- buffer is full Process_And_Discard (Buffer); end if; Buffers.Append (Source => Buffer, New_Item => C); exception when Ada.IO_Exceptions.End_Error => -- end of stream reached Process_And_Discard (Buffer); -- process whatever we read last exit; end; end loop; end My_Program; > The other question is - how to use the C bindings properly in such a case? Don't. -- Ludovic Brenta.