-- $Id: sumcol.gnat,v 1.0 2003/06/11 12:06:00 dada Exp $
-- http://dada.perl.it/shootout/
-- Ada 95 code by C.C.
with Text_IO, Ada.Strings.Fixed, Ada.IO_Exceptions;
procedure SumCol is
package IE renames Ada.IO_Exceptions;
package AS renames Ada.Strings;
function Read_Line return String is
Buf : String (1 .. 4096);
Last : Natural;
begin -- (End_Error here (in ObjectAda) on unusual missing final "\n")
Text_IO.Get_Line (Text_IO.Standard_Input, Item => Buf, Last => Last);
if Last < Buf'Last or else
Text_IO.End_Of_File (Text_IO.Standard_Input) then
return Buf (1 .. Last);
else
return Buf & Read_Line;
end if;
end Read_Line;
Sum : Integer := 0;
begin
while not Text_IO.End_Of_File loop
declare
Line : String := AS.Fixed.Trim (Read_Line, Side => AS.Right);
begin
Sum := Sum + Integer'Value (Line); -- Fail 0 or 2 numbers per line
end;
end loop;
Text_IO.Put_Line (AS.Fixed.Trim (Natural'Image (Sum), Side => AS.Left));
exception -- Catch error from Get_Line Integer'Value
when Constraint_Error | IE.Device_Error | IE.End_Error =>
Text_IO.Put_Line ("> Error near line" &
Text_IO.Count'Image (Text_IO.Line));
end SumCol;