Debian Codegraph @

progs/arm_db.adb

raw
1 -- with Ada.Text_IO; -- Debug.
2 with Ada.Unchecked_Deallocation,
3 Ada.Strings.Fixed,
4 Ada.Characters.Handling;
5 package body ARM_Database is
6
7 --
8 -- Ada reference manual formatter (ARM_Form).
9 --
10 -- This package contains the database to store items for non-normative
11 -- appendixes.
12 --
13 -- ---------------------------------------
14 -- Copyright 2000, 2004, 2005, 2006, 2009, 2011, 2012, 2019
15 -- AXE Consultants. All rights reserved.
16 -- P.O. Box 1512, Madison WI 53701
17 -- E-Mail: [email protected]
18 --
19 -- ARM_Form is free software: you can redistribute it and/or modify
20 -- it under the terms of the GNU General Public License version 3
21 -- as published by the Free Software Foundation.
22 --
23 -- AXE CONSULTANTS MAKES THIS TOOL AND SOURCE CODE AVAILABLE ON AN "AS IS"
24 -- BASIS AND MAKES NO WARRANTY, EXPRESS OR IMPLIED, AS TO THE ACCURACY,
25 -- CAPABILITY, EFFICIENCY, MERCHANTABILITY, OR FUNCTIONING OF THIS TOOL.
26 -- IN NO EVENT WILL AXE CONSULTANTS BE LIABLE FOR ANY GENERAL,
27 -- CONSEQUENTIAL, INDIRECT, INCIDENTAL, EXEMPLARY, OR SPECIAL DAMAGES,
28 -- EVEN IF AXE CONSULTANTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
29 -- DAMAGES.
30 --
31 -- A copy of the GNU General Public License is available in the file
32 -- gpl-3-0.txt in the standard distribution of the ARM_Form tool.
33 -- Otherwise, see <http://www.gnu.org/licenses/>.
34 --
35 -- If the GPLv3 license is not satisfactory for your needs, a commercial
36 -- use license is available for this tool. Contact Randy at AXE Consultants
37 -- for more information.
38 --
39 -- ---------------------------------------
40 --
41 -- Edit History:
42 --
43 -- 5/16/00 - RLB - Created package.
44 -- 8/28/00 - RLB - Added revision info to database.
45 -- 10/28/04 - RLB - Added Inserted_Normal_Number change kind.
46 -- 11/02/04 - RLB - Added Deleted_Inserted_Number change kind.
47 -- 12/06/04 - RLB - Added Revised_Inserted_Number change kind.
48 -- 12/14/04 - RLB - Made the hang item bigger.
49 -- 1/19/05 - RLB - Added Added_Version.
50 -- 10/17/05 - RLB - Fixed indexing of the Glossary.
51 -- 10/18/06 - RLB - Added No_Deleted_Paragraph_Messages to Report.
52 -- 11/30/09 - RLB - Made the hang item bigger again (to make room to
53 -- handle commands like @ChgAdded).
54 -- 10/18/11 - RLB - Changed to GPLv3 license.
55 -- 10/20/11 - RLB - Added Initial_Version parameter.
56 -- 3/19/12 - RLB - Added code to suppress indexing of deleted glossary items.
57 -- 1/27/19 - RLB - Lengthened components to allow Reduce attributes.
58
59 type String_Ptr is access String;
60 type Item is record
61 Next : Item_List;
62 Sort_Key : String(1 .. 55);
63 Hang : String(1 .. 85);
64 Hang_Len : Natural;
65 Text : String_Ptr;
66 Change_Kind : Paragraph_Change_Kind_Type;
67 Version : Character;
68 Initial_Version : Character;
69 end record;
70
71 procedure Free is new Ada.Unchecked_Deallocation (Item, Item_List);
72 procedure Free is new Ada.Unchecked_Deallocation (String, String_Ptr);
73
74 procedure Create (Database_Object : in out Database_Type) is
75 -- Initialize a database object.
76 begin
77 Database_Object.Is_Valid := True;
78 Database_Object.List := null;
79 Database_Object.Item_Count := 0;
80 end Create;
81
82
83 procedure Destroy (Database_Object : in out Database_Type) is
84 -- Destroy a database object, freeing any resources used.
85 Temp : Item_List;
86 begin
87 if not Database_Object.Is_Valid then
88 raise Not_Valid_Error;
89 end if;
90 while Database_Object.List /= null loop
91 Temp := Database_Object.List;
92 Database_Object.List := Temp.Next;
93 Free (Temp.Text);
94 Free (Temp);
95 end loop;
96 Database_Object.Is_Valid := False;
97 end Destroy;
98
99
100 procedure Insert (Database_Object : in out Database_Type;
101 Sort_Key : in String;
102 Hang_Item : in String;
103 Text : in String;
104 Change_Kind : in Paragraph_Change_Kind_Type := ARM_Database.None;
105 Version : in Character := '0';
106 Initial_Version : in Character := '0') is
107 -- Insert an item into the database object.
108 -- Sort_Key is the string on which this item will be sorted (if it
109 -- is sorted). Hang_Item is the item which hangs out for the item
110 -- in the report (if any). Text is the text for the item; the text
111 -- may include formatting codes. Change_Kind and Version are the
112 -- revision status for this item. Initial_Version is the version of
113 -- the initial text for this item.
114 Temp_Item : Item;
115 begin
116 if not Database_Object.Is_Valid then
117 raise Not_Valid_Error;
118 end if;
119 --if Sort_Key'Length > Temp_Item.Sort_Key'Length - 10 then
120 -- Ada.Text_IO.Put_Line ("?? Sort_Key near or beyond size limit; Length=" &
121 -- Natural'Image(Sort_Key'Length) & "; Limit=" &
122 -- Natural'Image(Temp_Item.Sort_Key'Length) & "; Sort_Key=" & Sort_Key);
123 --end if;
124 --if Hang_Item'Length > Temp_Item.Hang'Length - 10 then
125 -- Ada.Text_IO.Put_Line ("?? Hang_Item near or beyond size limit; Length=" &
126 -- Natural'Image(Hang_Item'Length) & "; Limit=" &
127 -- Natural'Image(Temp_Item.Hang'Length) & "; Hang_Item=" & Hang_Item);
128 --end if;
129
130 Ada.Strings.Fixed.Move (Target => Temp_Item.Sort_Key,
131 Source => Ada.Characters.Handling.To_Lower(Sort_Key),
132 Drop => Ada.Strings.Right,
133 Pad => ' ');
134 Ada.Strings.Fixed.Move (Target => Temp_Item.Hang,
135 Source => Hang_Item,
136 Drop => Ada.Strings.Error,
137 Pad => ' ');
138 Temp_Item.Hang_Len := Hang_Item'Length;
139 -- Note: If this second item doesn't fit, we error so we can make
140 -- the size larger.
141 Temp_Item.Text := new String'(Text);
142 Temp_Item.Change_Kind := Change_Kind;
143 Temp_Item.Version := Version;
144 Temp_Item.Initial_Version := Initial_Version;
145 Temp_Item.Next := Database_Object.List;
146 Database_Object.List := new Item'(Temp_Item);
147 Database_Object.Item_Count := Database_Object.Item_Count + 1;
148 end Insert;
149
150
151 --generic
152 -- with procedure Format_Text (Text : in String;
153 -- Text_Name : in String);
154 procedure Report (Database_Object : in out Database_Type;
155 In_Format : in Format_Type;
156 Sorted : in Boolean;
157 Added_Version : Character := '0';
158 No_Deleted_Paragraph_Messages : in Boolean := False) is
159 -- Output the items with the appropriate format to the
160 -- "Format_Text" routine. "Format_Text" allows all commands
161 -- for the full formatter. (Text_Name is an identifying name
162 -- for error messages). This is an added list for Added_Version
163 -- ('0' meaning it is not added); in that case, use normal numbers
164 -- for items with a version less than or equal to Added_Version.
165 -- (This is intended to be used to output the items to
166 -- appropriate Format and Output objects; but we can't do that
167 -- directly because that would make this unit recursive with
168 -- ARM_Format.
169 -- No paragraphs will be have deleted paragraph messages if
170 -- No_Deleted_Paragraph_Messages is True.
171 Temp : Item_List;
172
173 function Change_if_Needed (Item : in Item_List) return String is
174 begin
175 -- Note: In the report, we always decide inserted/not inserted
176 -- as determined by the initial version number, and not the
177 -- original class.
178 case Item.Change_Kind is
179 when None => return "";
180 when Inserted | Inserted_Normal_Number =>
181 if Item.Initial_Version <= Added_Version then
182 return "@ChgRef{Version=[" & Item.Version &
183 "],Kind=[AddedNormal]}";
184 else
185 return "@ChgRef{Version=[" & Item.Version &
186 "],Kind=[Added]}";
187 end if;
188 when Revised | Revised_Inserted_Number =>
189 if Item.Initial_Version <= Added_Version then
190 return "@ChgRef{Version=[" & Item.Version &
191 "],Kind=[Revised]}";
192 else
193 return "@ChgRef{Version=[" & Item.Version &
194 "],Kind=[RevisedAdded]}";
195 end if;
196 when Deleted | Deleted_Inserted_Number =>
197 if Item.Initial_Version <= Added_Version then
198 if No_Deleted_Paragraph_Messages then
199 return "@ChgRef{Version=[" & Item.Version &
200 "],Kind=[DeletedNoDelMsg]}";
201 else
202 return "@ChgRef{Version=[" & Item.Version &
203 "],Kind=[Deleted]}";
204 end if;
205 else
206 if No_Deleted_Paragraph_Messages then
207 return "@ChgRef{Version=[" & Item.Version &
208 "],Kind=[DeletedAddedNoDelMsg]}";
209 else
210 return "@ChgRef{Version=[" & Item.Version &
211 "],Kind=[DeletedAdded]}";
212 end if;
213 end if;
214 when Deleted_No_Delete_Message |
215 Deleted_Inserted_Number_No_Delete_Message =>
216 if Item.Initial_Version <= Added_Version then
217 return "@ChgRef{Version=[" & Item.Version &
218 "],Kind=[DeletedNoDelMsg]}";
219 else
220 return "@ChgRef{Version=[" & Item.Version &
221 "],Kind=[DeletedAddedNoDelMsg]}";
222 end if;
223 end case;
224 end Change_if_Needed;
225
226 begin
227 if not Database_Object.Is_Valid then
228 raise Not_Valid_Error;
229 end if;
230 if Sorted then
231 declare
232 Items : array (1..Database_Object.Item_Count) of Item_List;
233 begin
234 -- Load the items:
235 Temp := Database_Object.List;
236 for I in Items'range loop
237 Items(I) := Temp;
238 Temp := Temp.Next;
239 end loop;
240
241 -- Sort the items array (use an insertion sort because it is
242 -- stable):
243 declare
244 Left : Natural; -- Left sorting stop
245 begin
246 for Right In Items'First+1 .. Items'Last loop -- Right sorting stop
247 Temp := Items(Right);
248 Left := Right - 1;
249 while Temp.Sort_Key <= Items(Left).Sort_Key loop -- Switch items
250 Items(Left + 1) := Items(Left);
251 Left := Left - 1;
252 exit when Left = 0;
253 end loop;
254 Items(Left + 1) := Temp;
255 end loop;
256 end;
257
258 -- Relink the items in the sorted order:
259 for I in Items'First .. Items'Last - 1 loop
260 Items(I).Next := Items(I+1);
261 end loop;
262 if Items'Length > 0 then
263 Items(Items'Last).Next := null;
264 Database_Object.List := Items(1);
265 else
266 Database_Object.List := null;
267 end if;
268 end;
269 end if;
270 case In_Format is
271 when Hanging_List =>
272 Format_Text ("@begin(description)" & Ascii.LF, "Prefix");
273 Temp := Database_Object.List;
274 while Temp /= null loop
275 --** Debug:
276 --Ada.Text_IO.Put_Line ("^^ " & Paragraph_Change_Kind_Type'Image(Temp.Change_Kind) &
277 -- " for " & Temp.Hang(1..Temp.Hang_Len) & " ref=" & Change_if_Needed (Temp));
278 --Ada.Text_IO.Put_Line (" " & Change_if_Needed (Temp) &
279 --Temp.Hang(1..Temp.Hang_Len) & "@\" &
280 --Temp.Text.all & Ascii.LF & Ascii.LF);
281 Format_Text (Change_if_Needed (Temp) &
282 Temp.Hang(1..Temp.Hang_Len) & "@\" &
283 Temp.Text.all & Ascii.LF & Ascii.LF, Temp.Sort_Key);
284 Temp := Temp.Next;
285 end loop;
286 Format_Text ("@end(description)" & Ascii.LF, "Suffix");
287 when Bullet_List =>
288 Format_Text ("@begin(itemize)" & Ascii.LF, "Prefix");
289 Temp := Database_Object.List;
290 while Temp /= null loop
291 Format_Text (Change_if_Needed (Temp) &
292 Temp.Text.all & Ascii.LF & Ascii.LF, Temp.Sort_Key);
293 Temp := Temp.Next;
294 end loop;
295 Format_Text ("@end(itemize)" & Ascii.LF, "Suffix");
296 when Normal_List =>
297 Format_Text ("@begin(intro)" & Ascii.LF, "Prefix");
298 Temp := Database_Object.List;
299 while Temp /= null loop
300 Format_Text (Change_if_Needed (Temp) &
301 Temp.Text.all & Ascii.LF & Ascii.LF, Temp.Sort_Key);
302 Temp := Temp.Next;
303 end loop;
304 Format_Text ("@end(intro)" & Ascii.LF, "Suffix");
305 when Normal_Indexed_List =>
306 Format_Text ("@begin(intro)" & Ascii.LF, "Prefix");
307 Temp := Database_Object.List;
308 while Temp /= null loop
309 case Temp.Change_Kind is
310 when None |
311 Inserted | Inserted_Normal_Number |
312 Revised | Revised_Inserted_Number =>
313 --** Debug:
314 --Ada.Text_IO.Put_Line("Format " & Change_if_Needed (Temp) &
315 -- "@defn{" & Ada.Strings.Fixed.Trim (Temp.Sort_Key, Ada.Strings.Right) & "}" & Ascii.LF &
316 -- Temp.Text.all);
317 -- Index this item.
318 Format_Text (Change_if_Needed (Temp) &
319 "@defn{" & Ada.Strings.Fixed.Trim (Temp.Sort_Key, Ada.Strings.Right) & "}" & Ascii.LF &
320 Temp.Text.all & Ascii.LF & Ascii.LF, Temp.Sort_Key);
321 when Deleted | Deleted_Inserted_Number |
322 Deleted_No_Delete_Message |
323 Deleted_Inserted_Number_No_Delete_Message =>
324 --** Debug:
325 --Ada.Text_IO.Put_Line("Format " & Change_if_Needed (Temp) & Ascii.LF &
326 -- Temp.Text.all);
327 -- Don't index deleted items.
328 Format_Text (Change_if_Needed (Temp) & Ascii.LF &
329 Temp.Text.all & Ascii.LF & Ascii.LF, Temp.Sort_Key);
330 end case;
331 Temp := Temp.Next;
332 end loop;
333 Format_Text ("@end(intro)" & Ascii.LF, "Suffix");
334 end case;
335 end Report;
336
337 end ARM_Database;
338
339