Debian Codegraph @

progs/para_cnt.adb

raw
1 -- Count numbered paragraphs in a version of the RM, and store
2 -- store the results into a CVS file. Run this program in the directory
3 -- with the RM*.html files.
4
5 -- Edit History:
6 --
7 -- 2/21/19 - RLB - Created program.
8
9 with Ada.Text_IO;
10 with Ada.Directories;
11 package Para_Cnt_Pkg is
12
13 Result_File : Ada.Text_IO.File_Type;
14
15 procedure Count_File (Item : in Ada.Directories.Directory_Entry_Type);
16 end Para_Cnt_Pkg;
17
18
19 with Ada.Strings.Fixed;
20 with Ada.Strings.Maps.Constants;
21 package body Para_Cnt_Pkg is
22
23 procedure Count_File (Item : in Ada.Directories.Directory_Entry_Type) is
24 Cnts : array ('0'..'9') of Natural := (others => 0);
25 Total : Natural := 0;
26 Inserted : Natural := 0;
27 The_File : Ada.Text_IO.File_Type;
28
29 Buffer : String (1..1024);
30 BLen : Natural;
31 LCnt : Long_Integer := 0;
32
33 PStart : Natural;
34 PEnd : Natural;
35 begin
36 Ada.Text_IO.Put_Line (" Counting file " & Ada.Directories.Simple_Name (Item));
37
38 Ada.Text_IO.Open (The_File, Ada.Text_IO.In_File,
39 Ada.Directories.Full_Name (Item));
40
41 begin
42 loop
43 Ada.Text_IO.Get_Line (The_File, Buffer, BLen);
44 LCnt := LCnt + 1;
45
46 -- In the absence of a bug, there should be only one
47 -- paragraph per line. A paragraph number looks like:
48 -- <div class="paranum"><a name="p20">20/5</a></div>
49 -- Older files are missing the anchor (the "a" command),
50 -- and they sometimes have the markup in upper case.
51
52 Ada.Strings.Fixed.Translate (Buffer(1..BLen),
53 Ada.Strings.Maps.Constants.Lower_Case_Map);
54
55 PStart := Ada.Strings.Fixed.Index (Buffer(1..BLen),
56 "<div class=""paranum"">");
57 if PStart /= 0 then -- A paragraph number found.
58 PEnd := Ada.Strings.Fixed.Index (Buffer(PStart..BLen),
59 "</div>");
60 if PEnd = 0 then
61 Ada.Text_IO.Put_Line ("** Unable to find paranum end on line" & LCnt'Image);
62 elsif Ada.Strings.Fixed.Index (Buffer(PEnd..BLen),
63 "<div class=""paranum"">") /= 0 then
64 Ada.Text_IO.Put_Line ("** Multiple paranums on line" & LCnt'Image);
65 else
66 Total := Total + 1;
67 if Ada.Strings.Fixed.Index (Buffer(PStart+22..PEnd+4),
68 ".") /= 0 then
69 Inserted := Inserted + 1;
70 -- else not inserted.
71 end if;
72 if Ada.Strings.Fixed.Index (Buffer(PStart+22..PEnd+4),
73 "/1<") /= 0 then
74 Cnts('1') := Cnts('1') + 1;
75 elsif Ada.Strings.Fixed.Index (Buffer(PStart+22..PEnd+4),
76 "/2<") /= 0 then
77 Cnts('2') := Cnts('2') + 1;
78 elsif Ada.Strings.Fixed.Index (Buffer(PStart+22..PEnd+4),
79 "/3<") /= 0 then
80 Cnts('3') := Cnts('3') + 1;
81 elsif Ada.Strings.Fixed.Index (Buffer(PStart+22..PEnd+4),
82 "/4<") /= 0 then
83 Cnts('4') := Cnts('4') + 1;
84 elsif Ada.Strings.Fixed.Index (Buffer(PStart+22..PEnd+4),
85 "/5<") /= 0 then
86 Cnts('5') := Cnts('5') + 1;
87 elsif Ada.Strings.Fixed.Index (Buffer(PStart+22..PEnd+4),
88 "/6<") /= 0 then
89 Cnts('6') := Cnts('6') + 1;
90 elsif Ada.Strings.Fixed.Index (Buffer(PStart+22..PEnd+4),
91 "/7<") /= 0 then
92 Cnts('7') := Cnts('7') + 1;
93 elsif Ada.Strings.Fixed.Index (Buffer(PStart+22..PEnd+4),
94 "/8<") /= 0 then
95 Cnts('8') := Cnts('8') + 1;
96 elsif Ada.Strings.Fixed.Index (Buffer(PStart+22..PEnd+4),
97 "/9<") /= 0 then
98 Cnts('9') := Cnts('9') + 1;
99 else -- No expected / number.
100 Cnts('0') := Cnts('0') + 1;
101 end if;
102 end if;
103 -- else no paragraph number.
104 end if;
105
106 end loop;
107
108 exception
109 when Ada.Text_IO.End_Error => -- Reached the end of the file.
110 Ada.Text_IO.Close (The_File);
111 end;
112
113
114 -- Write the results:
115 Ada.Text_IO.Put (Result_File, Ada.Directories.Simple_Name (Item));
116 Ada.Text_IO.Put (Result_File, "," & Total'Image);
117 Ada.Text_IO.Put (Result_File, "," & Inserted'Image);
118 for I in Cnts'Range loop
119 Ada.Text_IO.Put (Result_File, "," & Cnts(I)'Image);
120 end loop;
121 Ada.Text_IO.New_Line (Result_File);
122
123 Ada.Text_IO.Put_Line (" Saw" & Total'Image & " paragraphs, with" &
124 Inserted'Image & " inserted paragraphs");
125 end Count_File;
126
127 end Para_Cnt_Pkg;
128
129
130 with Ada.Text_IO;
131 with Ada.Directories;
132 with Ada.Command_Line;
133 with Para_Cnt_Pkg;
134 procedure Para_Cnt is
135
136 begin
137 Ada.Text_IO.Put_Line ("Paragraph Count v. 1.0");
138 if Ada.Command_Line.Argument_Count /= 1 then
139 Ada.Text_IO.Put_Line ("** No output file specified on command line!");
140 return;
141 -- else OK.
142 end if;
143 Ada.Text_IO.Put_Line (" Write results to " & Ada.Command_Line.Argument(1) & ".csv");
144
145 Ada.Text_IO.Create (Para_Cnt_Pkg.Result_File, Ada.Text_IO.Out_File,
146 Ada.Command_Line.Argument(1) & ".csv");
147
148 -- Put a CVS header:
149 Ada.Text_IO.Put_Line (Para_Cnt_Pkg.Result_File, "File, Total, Inserted, Org, '/1, '/2, '/3, '/4, '/5, '/6, '/7, '/8, '/9");
150
151 Ada.Directories.Search (Directory => ".",
152 Pattern => "RM-*.html",
153 Process => Para_Cnt_Pkg.Count_File'Access);
154
155 Ada.Text_IO.Close (Para_Cnt_Pkg.Result_File);
156 Ada.Text_IO.Put_Line ("Paragraph Counting completed");
157
158 end Para_Cnt;