utilada_unit_2.1.0_56b45091/samples/csv_city.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
-----------------------------------------------------------------------
--  csv_city -- Read CSV file which contains city mapping
--  Copyright (C) 2011, 2017 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Text_IO;
with Ada.Command_Line;
with Ada.Containers;
with Ada.Strings.Unbounded;

with Util.Log.Loggers;
with Util.Serialize.IO.CSV;
with Util.Serialize.Mappers;

with City_Mapping;
procedure CSV_City is

   use Ada.Containers;
   use Ada.Strings.Unbounded;
   use Ada.Text_IO;
   use Util.Serialize.IO.CSV;

   Count  : constant Natural := Ada.Command_Line.Argument_Count;
begin
   Util.Log.Loggers.Initialize ("samples/log4j.properties");

   if Count = 0 then
      Ada.Text_IO.Put_Line ("Usage: csv_city file [city...]");
      Ada.Text_IO.Put_Line ("Example: csv_city samples/cities.csv albertville");
      return;
   end if;
   declare
      File   : constant String := Ada.Command_Line.Argument (1);
      List   : aliased City_Mapping.City_Vector.Vector;
      Mapper : aliased Util.Serialize.Mappers.Processing;
      Reader : Util.Serialize.IO.CSV.Parser;
   begin
      Mapper.Add_Mapping ("", City_Mapping.Get_City_Vector_Mapper.all'Access);
      City_Mapping.City_Vector_Mapper.Set_Context (Mapper, List'Unchecked_Access);
      Reader.Parse (File, Mapper);

      if List.Length = 0 then
         Ada.Text_IO.Put_Line ("No city found.");

      elsif List.Length = 1 then
         Ada.Text_IO.Put_Line ("Found only one city.");

      else
         Ada.Text_IO.Put_Line ("Found " & Count_Type'Image (List.Length) & " cities");

      end if;
      for I in 2 .. Count loop
         declare
            Name  : constant String := Ada.Command_Line.Argument (I);
            Found : Boolean := False;

            procedure Print (City : in City_Mapping.City);

            procedure Print (City : in City_Mapping.City) is
            begin
               Found := City.City = Name;
               if Found then
                  Ada.Text_IO.Put_Line ("City        : " & To_String (City.Name));
                  Ada.Text_IO.Put_Line ("Country code: " & To_String (City.Country));
                  Ada.Text_IO.Put_Line ("Region      : " & To_String (City.Region));
                  Ada.Text_IO.Put_Line ("Latitude    : " & Float'Image (City.Latitude));
                  Ada.Text_IO.Put_Line ("Longitude   : " & Float'Image (City.Longitude));
               end if;
            end Print;

         begin
            for J in 1 .. Positive (List.Length) loop
               List.Query_Element (J, Print'Access);
               exit when Found;
            end loop;
            if not Found then
               Ada.Text_IO.Put_Line ("City '" & Name & "' not found");
            end if;
         end;
      end loop;
   end;
end CSV_City;