Ada

Hello world

hello.adb

   1 with Ada.Text_IO;
   2 -- comment 
   3 procedure Hello is
   4 begin
   5    Ada.Text_IO.Put_Line("Hello, world!");
   6 end Hello;

Hello date

hellodate.adb

   1 with ada.text_io;
   2 use ada.text_io;
   3 with gnat.calendar.time_io;
   4 use gnat.calendar.time_io;
   5 with ada.calendar;
   6 use ada.calendar;
   7 
   8 procedure HelloDate is
   9     datetime: string(1..26); -- has exactly 26 chars
  10 begin
  11     -- gets date to string with milliseconds
  12     datetime := image( clock, "[%Y-%m-%dT%H:%M:%S.%i] " );
  13     -- concatenetas string and shows date
  14     put_line( datetime & "stuff " );  
  15 end HelloDate;

Person

person.ads

   1 -- The package encapsulates the type and code (functions,procedures) for the type.
   2 package Person is
   3    type Data is tagged private;
   4    -- constructor
   5    function Init(Name : in String; Address : in String) return Data;
   6    -- getter name
   7    function GetName(self : in Data; extra : in String) return String;
   8    -- getter address
   9    function GetAddress(self : in Data; extra : in String) return String;
  10    -- setter name
  11    procedure SetName(self : in out Data; name : in String);
  12    -- setter address
  13    procedure SetAddress(self : in out Data; address : in String);
  14    
  15    private 
  16      type Data is tagged 
  17        record
  18          Name : String (1 .. 10);
  19          Address : String (1 .. 10);
  20        end record;
  21    
  22 end Person;

person.adb

   1 package body Person is
   2 
   3    -- constructor
   4    function Init(Name : in String ; Address : in String) return Data is
   5       p : Person.Data;-- := (Name => Name , Address => Address);
   6    begin 
   7      p.Name := Name;
   8      p.Address := Address;
   9      return p;
  10    end;
  11 
  12    -- getter name
  13    function GetName(self : in Data ; extra : in String) return String is
  14    begin
  15      return self.Name & extra;
  16    end;
  17 
  18    -- getter address
  19    function GetAddress(self : in Data ; extra : in String) return String is
  20    begin
  21      return self.Address & extra;
  22    end;
  23 
  24    -- setter name
  25    procedure SetName(self : in out Data ; name : String) is
  26    begin
  27      self.Name := name;
  28    end;
  29 
  30    -- setter address
  31    procedure SetAddress(self : in out Data ; address : String) is
  32    begin
  33      self.Address := address;
  34    end;
  35    
  36 end Person;

testperson.adb

   1 with ada.text_io;
   2 use ada.text_io;
   3 with Person;
   4 
   5 procedure TestPerson is
   6   p : Person.Data;
   7   q : Person.Data;
   8 begin
   9   p := Person.Init("0123456789", "9876543210");
  10   q := Person.Init("aaaaaaaaaa", "bbbbbbbbbb");
  11   q.SetName("zzzzzzzzzz");
  12 
  13   put_line( p.GetName("ex1") & " " & p.GetAddress("ex3") );
  14   put_line( q.GetName("ex2") & " " & q.GetAddress("ex4") );
  15 end TestPerson;

Show file

show_file.adb

   1 with Ada.Text_IO;
   2 with Utils;
   3 with Ada.Command_Line;
   4 
   5 procedure Show_File is
   6   package acl renames Ada.Command_Line; 
   7   package atio renames Ada.Text_IO;
   8 begin
   9     --Utils.Show_Content("/etc/hosts");
  10     if acl.Argument_Count >= 1 then
  11       atio.Put_Line(acl.Argument(1));
  12       Utils.Show_Content(acl.Argument(1));
  13     else
  14       atio.Put_Line("Please insert the file name as an argument");  
  15     end if;
  16 end Show_File;

utils.ads

   1 package Utils is
   2   procedure Show_Content(File_Name: in String);
   3 end Utils;

utils.adb

   1 with Ada.Text_IO;
   2 
   3 package body Utils is
   4 
   5   procedure Show_Content(File_Name: in String) is
   6     package atio renames Ada.Text_IO;
   7     FileHandler    : atio.File_Type;
   8     Text_Max_Size : Integer := 1024;
   9     Text : String(1 .. Text_Max_Size);
  10     Amount_Chars : Natural;
  11   begin
  12     atio.Open (FileHandler, Mode => atio.In_File, Name => File_Name);
  13     -- initialize the string
  14     for N in 1 .. Text_Max_Size loop
  15       Text(N) := '?'; 
  16     end loop;
  17 
  18     while not atio.End_Of_File (FileHandler) loop     
  19         atio.Get_Line(FileHandler, Text, Amount_Chars);
  20         for IDX in 1 .. Amount_Chars loop
  21             atio.Put( Text(IDX) );
  22         end loop;
  23         --atio.Put_Line(Text);
  24         atio.New_Line;
  25     end loop;
  26      
  27     atio.Close(FileHandler);
  28   end;
  29 end Utils;

Archive org - byte magazine

Ada (last edited 2019-11-27 17:56:59 by localhost)