sciada_0.1.0_29e19539/examples/src/occurrences.adb

 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
-- --------------------------------------------------------------------
--  occurrences -- report occurrence of words in files
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--  SPDX-License-Identifier: Apache-2.0
-----------------------------------------------------------------------
with Ada.Text_IO;
with Ada.Command_Line;
with Ada.Directories;
with SCI.Occurrences.Arrays;
with Helpers;
procedure Occurrences is
   package AC renames Ada.Command_Line;

   package Word_Occurrences is
     new SCI.Occurrences.Arrays (Element_Type => Character,
                                 Index_Type => Positive,
                                 Array_Type => String,
                                 Occurrence_Type => Natural);

   procedure List_Occurrences is new Word_Occurrences.List;
   procedure Add is new Word_Occurrences.Add;
   function Sum is new Word_Occurrences.Sum;

   procedure Collect (Token : in String);

   Words : Word_Occurrences.Sets.Set;

   procedure Collect (Token : in String) is
   begin
      Add (Words, Token, 1);
   end Collect;

begin
   for I in 1 .. AC.Argument_Count loop
      declare
         Path : constant String := AC.Argument (I);
      begin
         if Ada.Directories.Exists (Path) then
            Helpers.Read_File (Path, Collect'Access);
         else
            Ada.Text_IO.Put (Path);
            Ada.Text_IO.Put_Line (": is not a file");
         end if;
      end;
   end loop;

   declare
      List  : Word_Occurrences.Vector;
      Total : Natural;
   begin
      List_Occurrences (Words, List);
      Total := Sum (List, 0);
      Ada.Text_IO.Put_Line ("Total:" & Total'Image);
      for Item of List loop
         Ada.Text_IO.Put (Item.Element);
         Ada.Text_IO.Put (" ");
         Ada.Text_IO.Put_Line (Item.Occurrence'Image);
      end loop;
   end;
end Occurrences;