/usr/share/ada/adainclude/opentoken/opentoken-text_feeder-counted_gnat_os_lib.adb is in libopentoken5-dev 6.0b-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | -- Abstract :
--
-- see spec
--
-- Copyright (C) 2014 All Rights Reserved.
--
-- This program is free software; you can redistribute it and/or
-- modify it under terms of the GNU General Public License as
-- published by the Free Software Foundation; either version 3, or (at
-- your option) any later version. This program is distributed in the
-- hope that it will be useful, but WITHOUT ANY WARRANTY; without even
-- the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-- PURPOSE. See the GNU General Public License for more details. You
-- should have received a copy of the GNU General Public License
-- distributed with this program; see file COPYING. If not, write to
-- the Free Software Foundation, 51 Franklin Street, Suite 500, Boston,
-- MA 02110-1335, USA.
pragma License (GPL);
with Ada.Text_IO;
package body OpenToken.Text_Feeder.Counted_GNAT_OS_Lib is
function Create (File : in GNAT.OS_Lib.File_Descriptor) return Text_Feeder_Ptr
is begin
return new Instance'(File, 0, 0, 0);
end Create;
procedure Reset (Feeder : in out Instance; Max_Bytes : in Integer) is
begin
Feeder.Max_Bytes := Max_Bytes;
Feeder.Read_Bytes := 0;
Feeder.Get_Count := 0;
end Reset;
overriding procedure Get
(Feeder : in out Instance;
Text : out String;
Text_End : out Integer)
is
use GNAT.OS_Lib;
Bytes_To_Read : constant Integer := Integer'Min (Text'Length, Feeder.Max_Bytes - Feeder.Read_Bytes);
Read_Bytes : Integer;
begin
if Feeder.Read_Bytes >= Feeder.Max_Bytes then
Text_End := Text'First;
Text (Text_End) := EOF_Character;
else
Feeder.Get_Count := Feeder.Get_Count + 1;
Read_Bytes := Read (Feeder.File, Text'Address, Bytes_To_Read);
Feeder.Read_Bytes := Feeder.Read_Bytes + Read_Bytes;
if Trace_Parse > 0 and Read_Bytes > 0 then
Ada.Text_IO.Put_Line ("read" & Integer'Image (Read_Bytes));
Ada.Text_IO.Put_Line (Text (Text'First .. Text'First + Read_Bytes - 1));
end if;
Text_End := Text'First + Read_Bytes - 1;
-- Translate end of line to EOL_Character.
-- FIXME: what if a line end sequence crosses Text'last?
for I in Text'First .. Text_End loop
if I < Text_End and then (Text (I) = ASCII.LF and Text (I + 1) = ASCII.CR) then
-- DOS line end
-- We should delete the second character entirely, but
-- this is simpler. Unless the user code is trying to
-- reproduce the source exactly, it should be
-- harmless. We put the space at the end of the prev
-- line to preserve column numbers in the next.
Text (I) := ' ';
Text (I + 1) := EOL_Character;
elsif I < Text_End and then (Text (I) = ASCII.CR and Text (I + 1) = ASCII.LF) then
-- (Old) Mac line end
Text (I) := ' ';
Text (I + 1) := EOL_Character;
elsif Text (I) = ASCII.LF then
-- Unix line end
Text (I) := EOL_Character;
end if;
end loop;
if Read_Bytes < Bytes_To_Read then
-- premature end of file
Feeder.Read_Bytes := Feeder.Max_Bytes;
Text_End := Text_End + 1;
Text (Text_End) := EOF_Character;
end if;
end if;
end Get;
overriding function End_Of_Text (Feeder : in Instance) return Boolean is
begin
return Feeder.Read_Bytes = Feeder.Max_Bytes;
end End_Of_Text;
procedure Discard_Rest_Of_Input (Feeder : in out Instance)
is
use GNAT.OS_Lib;
use Ada.Text_IO;
Start_Bytes : constant Integer := Feeder.Read_Bytes;
First : Boolean := True;
Junk : String (1 .. 2048);
Read_Bytes : Integer;
Bytes_To_Read : Integer;
begin
if Trace_Parse > 0 then
New_Line;
Put_Line ("discarding text from" & Integer'Image (Feeder.Read_Bytes) & ":");
end if;
loop
Bytes_To_Read := Integer'Min (Junk'Length, Feeder.Max_Bytes - Feeder.Read_Bytes);
Read_Bytes := Read (Feeder.File, Junk'Address, Bytes_To_Read);
Feeder.Read_Bytes := Feeder.Read_Bytes + Read_Bytes;
if Read_Bytes > 0 and Trace_Parse > 0 and First then
First := False;
Put_Line (Junk (1 .. Integer'Min (80, Read_Bytes)));
end if;
exit when Read_Bytes < Bytes_To_Read or Feeder.Read_Bytes = Feeder.Max_Bytes;
end loop;
if Trace_Parse > 0 then
Put_Line ("...");
if Read_Bytes > 80 then
Put_Line (Junk (Read_Bytes - 80 .. Read_Bytes));
elsif Read_Bytes > 0 then
Put_Line (Junk (1 .. Read_Bytes));
end if;
Put_Line ("discarded" & Integer'Image (Feeder.Read_Bytes - Start_Bytes));
Put_Line ("total read" & Integer'Image (Feeder.Read_Bytes) & " of" & Integer'Image (Feeder.Max_Bytes));
end if;
Feeder.Read_Bytes := Feeder.Max_Bytes;
end Discard_Rest_Of_Input;
function Get_Count (Feeder : in Instance) return Integer
is begin
return Feeder.Get_Count;
end Get_Count;
end OpenToken.Text_Feeder.Counted_GNAT_OS_Lib;
|