Debian Codegraph @

progs/arm_str.adb

raw
1 with --ARM_Input,
2 Ada.Text_IO;
3 package body ARM_String is
4
5 --
6 -- Ada reference manual formatter (ARM_Form).
7 --
8 -- This package contains the definition of reading input from a string.
9 --
10 -- ---------------------------------------
11 -- Copyright 2000, 2011
12 -- AXE Consultants. All rights reserved.
13 -- P.O. Box 1512, Madison WI 53701
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
43 procedure Open (Input_Object : in out String_Input_Type;
44 Text : in String;
45 Text_Name : in String) is
46 -- Open an input object for a file.
47 begin
48 Input_Object.Line_Counter := 0;
49 if Text'Length > Input_Object.Buffer'Length then
50 Ada.Text_IO.Put_Line (" ** Text too long for string input object for " & Text_Name);
51 else
52 Input_Object.Buffer(1..Text'Length) := Text;
53 Input_Object.Buffer_Len := Text'Length;
54 end if;
55 Input_Object.Buffer_Index := 0;
56 Input_Object.Is_Valid := True;
57 if Text_Name'Length > Input_Object.Name'Length then
58 Input_Object.Name := Text_Name(Text_Name'First .. Text_Name'First + Input_Object.Name'Length - 1);
59 Input_Object.Name_Len := Input_Object.Name'Length;
60 else
61 Input_Object.Name(1..Text_Name'Length) := Text_Name;
62 Input_Object.Name_Len := Text_Name'Length;
63 end if;
64 end Open;
65
66
67 procedure Close (Input_Object : in out String_Input_Type) is
68 -- Close the input object (entity).
69 -- May propagate exceptions from the underlying implementation
70 -- (that is, I/O exceptions).
71 begin
72 if not Input_Object.Is_Valid then
73 raise ARM_Input.Not_Valid_Error;
74 end if;
75 Input_Object.Is_Valid := False;
76 end Close;
77
78
79 procedure Get_Char (Input_Object : in out String_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;
87 end if;
88 Input_Object.Buffer_Index := Input_Object.Buffer_Index + 1;
89 if Input_Object.Buffer_Index > Input_Object.Buffer_Len then
90 --Ada.Text_IO.Put_Line ("Get_Char EOS, index=" & Natural'Image(Input_Object.Buffer_Index));
91 Char := ASCII.Sub;
92 return;
93 end if;
94 Char := Input_Object.Buffer(Input_Object.Buffer_Index);
95 if Char = Ascii.LF then
96 Input_Object.Line_Counter := Input_Object.Line_Counter + 1;
97 --Ada.Text_IO.Put_Line ("Get_Char EOL, index=" & Natural'Image(Input_Object.Buffer_Index));
98 --else Ada.Text_IO.Put_Line ("Get_Char " & Char & ", index=" & Natural'Image(Input_Object.Buffer_Index));
99 end if;
100 end Get_Char;
101
102
103 procedure Replace_Char (Input_Object : in out String_Input_Type) is
104 -- Replaces the last character read (with Get_Char); the next call
105 -- to Get_Char will return it.
106 -- Raises: Not_Valid_Error if Input_Object is not valid (open).
107 begin
108 if not Input_Object.Is_Valid then
109 raise ARM_Input.Not_Valid_Error;
110 end if;
111 if Input_Object.Buffer_Index = 0 then
112 raise Program_Error; -- Called twice or before any calls to Get_Char.
113 end if;
114 if Input_Object.Buffer_Index <= Input_Object.Buffer_Len and then
115 Input_Object.Buffer(Input_Object.Buffer_Index) = Ascii.LF then
116 Input_Object.Line_Counter := Input_Object.Line_Counter - 1;
117 end if;
118 --Ada.Text_IO.Put_Line ("Replace_Char");
119 Input_Object.Buffer_Index := Input_Object.Buffer_Index - 1;
120 end Replace_Char;
121
122
123 function Line_String (Input_Object : in String_Input_Type) return String is
124 -- Returns a string representing the line number and entity.
125 -- Usually used in error messages.
126 -- Raises: Not_Valid_Error if Input_Object is not valid (open).
127 begin
128 if not Input_Object.Is_Valid then
129 raise ARM_Input.Not_Valid_Error;
130 end if;
131 return Natural'Image(Input_Object.Line_Counter) & " - " &
132 Input_Object.Name(1..Input_Object.Name_Len);
133 end Line_String;
134
135
136 procedure Start_Recording (Input_Object : in out String_Input_Type) is
137 -- Start recording all characters read into a local buffer.
138 -- Use this when text needs to be formatted into the output
139 -- file *and* be saved for future use.
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;
144 end if;
145 Input_Object.Recording := True;
146 Input_Object.Recording_Start := Input_Object.Buffer_Index + 1;
147 end Start_Recording;
148
149
150 procedure Stop_Recording_and_Read_Result
151 (Input_Object : in out String_Input_Type; Result : out String;
152 Len : out Natural) is
153 -- Stop recording characters read. Put the result into Result,
154 -- and the number of characters written into Len.
155 -- Raises: Not_Valid_Error if Input_Object is not valid (open).
156 begin
157 if not Input_Object.Is_Valid then
158 raise ARM_Input.Not_Valid_Error;
159 end if;
160 if Input_Object.Buffer_Index - Input_Object.Recording_Start + 1 >
161 Result'Length then
162 Ada.Text_IO.Put_Line (" ** Too many characters recorded on line " & Line_String (Input_Object));
163 Len := 0;
164 else
165 Len := (Input_Object.Buffer_Index - Input_Object.Recording_Start) + 1;
166 Result (Result'First .. Result'First + Len - 1) :=
167 Input_Object.Buffer (Input_Object.Recording_Start .. Input_Object.Buffer_Index);
168 end if;
169 Input_Object.Recording := False;
170 end Stop_Recording_and_Read_Result;
171
172 end ARM_String;