Debian Codegraph @

progs/arm_file.adb

raw
1 --with ARM_Input,
2 -- Ada.Text_IO;
3 package body ARM_File is
4
5 --
6 -- Ada reference manual formatter (ARM_Form).
7 --
8 -- This package contains the definition of reading an input file.
9 --
10 -- ---------------------------------------
11 -- Copyright 2000, 2011, 2019
12 -- AXE Consultants. All rights reserved.
13 -- P.O. Box 1512, Madison WI 53704
14 -- E-Mail: [email protected]
15 --
16 -- ARM_Form is free software: you can redistribute it and/or modify
17 -- it under the terms of the GNU General Public License version 3
18 -- as published by the Free Software Foundation.
19 --
20 -- AXE CONSULTANTS MAKES THIS TOOL AND SOURCE CODE AVAILABLE ON AN "AS IS"
21 -- BASIS AND MAKES NO WARRANTY, EXPRESS OR IMPLIED, AS TO THE ACCURACY,
22 -- CAPABILITY, EFFICIENCY, MERCHANTABILITY, OR FUNCTIONING OF THIS TOOL.
23 -- IN NO EVENT WILL AXE CONSULTANTS BE LIABLE FOR ANY GENERAL,
24 -- CONSEQUENTIAL, INDIRECT, INCIDENTAL, EXEMPLARY, OR SPECIAL DAMAGES,
25 -- EVEN IF AXE CONSULTANTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
26 -- DAMAGES.
27 --
28 -- A copy of the GNU General Public License is available in the file
29 -- gpl-3-0.txt in the standard distribution of the ARM_Form tool.
30 -- Otherwise, see <http://www.gnu.org/licenses/>.
31 --
32 -- If the GPLv3 license is not satisfactory for your needs, a commercial
33 -- use license is available for this tool. Contact Randy at AXE Consultants
34 -- for more information.
35 --
36 -- ---------------------------------------
37 --
38 -- Edit History:
39 --
40 -- 5/15/00 - RLB - Created package.
41 -- 10/18/11 - RLB - Changed to GPLv3 license.
42 -- 2/15/19 - RLB - Improved error handling of recording buffer overflow.
43 -- 2/19/19 - RLB - Added replacement of previous line end.
44
45 procedure Open (Input_Object : in out File_Input_Type;
46 File_Name : in String) is
47 -- Open an input object for a file.
48 -- This may propagate file exceptions.
49 begin
50 Ada.Text_IO.Open (Input_Object.Fyle, Ada.Text_IO.In_File, File_Name);
51 Input_Object.Line_Counter := 0;
52 Input_Object.Buffer_Last := 0;
53 Input_Object.Buffer_Index := 0;
54 Input_Object.Extra_LF := False;
55 Input_Object.Is_Valid := True;
56 if File_Name'Length > Input_Object.Name'Length then
57 Input_Object.Name := File_Name(File_Name'First .. File_Name'First + Input_Object.Name'Length - 1);
58 Input_Object.Name_Len := Input_Object.Name'Length;
59 else
60 Input_Object.Name(1..File_Name'Length) := File_Name;
61 Input_Object.Name_Len := File_Name'Length;
62 end if;
63 end Open;
64
65
66 procedure Close (Input_Object : in out File_Input_Type) is
67 -- Close the input object (entity).
68 -- May propagate exceptions from the underlying implementation
69 -- (that is, I/O exceptions).
70 begin
71 if not Input_Object.Is_Valid then
72 raise ARM_Input.Not_Valid_Error;
73 end if;
74 Input_Object.Is_Valid := False;
75 Ada.Text_IO.Close (Input_Object.Fyle);
76 end Close;
77
78
79 procedure Get_Char (Input_Object : in out File_Input_Type;
80 Char : out Character) is
81 -- We represent end of line by Ascii.LF.
82 -- Raises: End_Error when the end of file is reached.
83 -- Not_Valid_Error if Input_Object is not valid (open).
84 begin
85 if not Input_Object.Is_Valid then
86 raise ARM_Input.Not_Valid_Error with "No file open";
87 end if;
88 if Input_Object.Extra_LF then
89 -- A special put-back of the previous line. We don't adjust
90 -- the line counter for this character, and it is already
91 -- in the recording buffer if necessary.
92 Char := Ascii.LF;
93 Input_Object.Extra_LF := False;
94 return;
95 elsif Input_Object.Buffer_Index >= Input_Object.Buffer_Last then
96 begin
97 Ada.Text_IO.Get_Line (Input_Object.Fyle,
98 Input_Object.Buffer,
99 Input_Object.Buffer_Last);
100 -- Raises End_Error when the end of the file is reached.
101 exception
102 when Ada.Text_IO.End_Error =>
103 -- Set so we can do a Replace_Char on this.
104 Input_Object.Buffer_Index := 1;
105 Input_Object.Buffer_Last := 1;
106 Input_Object.Buffer(1) := Ascii.SUB; -- File end marker.
107 Char := Ascii.SUB;
108 return;
109 end;
110 if Input_Object.Buffer_Last < Input_Object.Buffer'Last then
111 Input_Object.Buffer_Last := Input_Object.Buffer_Last + 1;
112 Input_Object.Buffer(Input_Object.Buffer_Last) := Ascii.LF; -- Line end marker.
113 -- else broken line, no end needed.
114 end if;
115 --Ada.Text_IO.Put(Natural'Image(Input_Object.Line_Counter) & ":");
116 --Ada.Text_IO.Put_Line ("&& " & Input_Object.Buffer(1..Input_Object.Buffer_Last));
117 Input_Object.Buffer_Index := 0;
118 Input_Object.Line_Counter := Input_Object.Line_Counter + 1;
119 end if;
120 Input_Object.Buffer_Index := Input_Object.Buffer_Index + 1;
121 if Input_Object.Recording then
122 Input_Object.Recording_Len := Input_Object.Recording_Len + 1;
123 if Input_Object.Recording_Len > Input_Object.Recording_Buffer'Last then
124 Ada.Text_IO.Put_Line (" ** Too many characters recorded on line " & Line_String (Input_Object));
125 Ada.Text_IO.Put_Line (" Recording started on line" & Natural'Image(Input_Object.Recording_Start_Line));
126 Input_Object.Recording_Len := Input_Object.Recording_Buffer'Last;
127 Input_Object.Recording := False;
128 else
129 Input_Object.Recording_Buffer(Input_Object.Recording_Len) :=
130 Input_Object.Buffer(Input_Object.Buffer_Index);
131 end if;
132 end if;
133 Char := Input_Object.Buffer(Input_Object.Buffer_Index);
134 end Get_Char;
135
136
137 procedure Replace_Char (Input_Object : in out File_Input_Type) is
138 -- Replaces the last character read (with Get_Char); the next call
139 -- to Get_Char will return it.
140 -- Raises: Not_Valid_Error if Input_Object is not valid (open).
141 begin
142 if not Input_Object.Is_Valid then
143 raise ARM_Input.Not_Valid_Error with "No file open";
144 end if;
145 if Input_Object.Buffer_Index = 0 then
146 if Input_Object.Extra_LF then -- Extra put back already done.
147 raise Program_Error with "Replace_Char called too many times";
148 else
149 Input_Object.Extra_LF := True;
150 -- We don't adjust the buffer or the recording in this case.
151 return;
152 end if;
153 end if;
154 Input_Object.Buffer_Index := Input_Object.Buffer_Index - 1;
155 if Input_Object.Recording then
156 Input_Object.Recording_Len := Input_Object.Recording_Len - 1;
157 end if;
158 end Replace_Char;
159
160
161 function Line_String (Input_Object : in File_Input_Type) return String is
162 -- Returns a string representing the line number and entity.
163 -- Usually used in error messages.
164 -- Raises: Not_Valid_Error if Input_Object is not valid (open).
165 begin
166 if not Input_Object.Is_Valid then
167 raise ARM_Input.Not_Valid_Error;
168 end if;
169 return Natural'Image(Input_Object.Line_Counter) & " - " &
170 Input_Object.Name(1..Input_Object.Name_Len);
171 end Line_String;
172
173
174 procedure Start_Recording (Input_Object : in out File_Input_Type) is
175 -- Start recording all characters read into a local buffer.
176 -- Use this when text needs to be formatted into the output
177 -- file *and* be saved for future use.
178 -- Raises: Not_Valid_Error if Input_Object is not valid (open).
179 begin
180 if not Input_Object.Is_Valid then
181 raise ARM_Input.Not_Valid_Error;
182 end if;
183 Input_Object.Recording := True;
184 Input_Object.Recording_Len := 0;
185 Input_Object.Recording_Start_Line := Input_Object.Line_Counter;
186 end Start_Recording;
187
188
189 procedure Stop_Recording_and_Read_Result
190 (Input_Object : in out File_Input_Type; Result : out String;
191 Len : out Natural) is
192 -- Stop recording characters read. Put the result into Result,
193 -- and the number of characters written into Len.
194 -- Raises: Not_Valid_Error if Input_Object is not valid (open).
195 begin
196 if not Input_Object.Is_Valid then
197 raise ARM_Input.Not_Valid_Error;
198 end if;
199 if Input_Object.Recording_Len > Result'Length then
200 Ada.Text_IO.Put_Line (" ** Too many characters recorded on line " & Line_String (Input_Object));
201 Ada.Text_IO.Put_Line (" Recording started on line" & Natural'Image(Input_Object.Recording_Start_Line));
202 Len := 0;
203 else
204 Result (Result'First .. Result'First + Input_Object.Recording_Len - 1) :=
205 Input_Object.Recording_Buffer (1 .. Input_Object.Recording_Len);
206 Len := Input_Object.Recording_Len;
207 end if;
208 Input_Object.Recording := False;
209 end Stop_Recording_and_Read_Result;
210
211 end ARM_File;