0
-- Date: 11/06/2022

with Ada.Text_IO; Use Ada.Text_Io;

procedure Masques is
  
    type XX is record
       X1 : character range 'A'..'D';
       X2 : character range 'E'..'H';
       X3 : character range 'I'..'L';
    end record;

begin

    Get_Line (XX);

end Masques;

I'm trying to write some sort of input masks to control the inputs as we do in IT. Of course the above example doesn't compile because Get_Line can't accept a record. Of course we can write an operation to put the characters together to create a string, thru a get or get_immediate.

-But the idea would be to use the language typing to control the input & to trap the errors by an exception by example.

-Some years ago, to the best of my recollection, i remember someone did this, but I'm unable to write it.. Thanks for the help.

2
  • I'm not clear what you're asking, but note that using stream I/O you can put whatever checking you want in the stream Read and Write procedures for your XX type.
    – user1818839
    Jun 11, 2022 at 14:29
  • You may be looking for the 'Valid attribute, examined here. See also Gem #83: Type-Based Security 2: Validating the Input.
    – trashgod
    Jun 11, 2022 at 17:02

2 Answers 2

2

One possibility is demonstrated below:

package input_masking is
   type Account_Number is private;
   function Get_Account return Account_Number;
   procedure Display (Account : Account_Number);
   Acct_Exception : Exception;
private
   subtype F1 is character range 'A' ..'D';
   subtype F2 is character range 'E' .. 'H';
   subtype F3 is character range 'I' .. 'L';
   subtype F4 is Integer range 1111 .. 9999;
   subtype F5 is character range 'S' .. 'U';
   type Account_Number is record
      Field_1 : F1;
      Field_2 : F2;
      Field_3 : F3;
      Field_4 : F4;
      Field_5 : F5;
   end record;
end input_masking;

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Exceptions; use Ada.Exceptions;

package body input_masking is

   -----------------
   -- Get_Account --
   -----------------

   function Get_Account return Account_Number is
      Result : Account_Number;
      Input : String(1..8);
   begin
      Input := Get_Line;
      begin
         Result.Field_1 := Input(1);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_1");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_2 := Input(2);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_2");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_3 := Input(3);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_3");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_4 := F4'Value(Input(4..7));
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_4");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_5 := Input(8);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_5");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
      return result;
   end Get_Account;

   -------------
   -- Display --
   -------------

   procedure Display (Account : Account_Number) is
      Temp : string(1..3);
      Temp2 : string(1..5);
   begin
      Temp := Account.Field_1'Image;
      Put(Temp(2));
      Temp := Account.Field_2'Image;
      Put(Temp(2));
      Temp := Account.Field_3'Image;
      Put(Temp(2));
      temp2 := Account.Field_4'Image;
      Put(Temp2(2..5));
      Temp := Account.Field_5'Image;
      Put(Temp(2));
      New_Line;
   end Display;

end input_masking;


with Input_Masking; use Input_Masking;

procedure Main is
   X : Account_Number;
begin
   X := Get_Account;
   Display(X);
end Main;
0
2

There are a lot of options for dealing with something like this, depending on what you're going to do with an XX. For example:

subtype XX is String (1 .. 3) with
   Dynamic_Predicate => XX (1) in 'A' .. 'D' and
                        XX (2) in 'E' .. 'H' and
                        XX (3) in 'I' .. 'L';

YY : XX;
Last : Natural;
...
Ada.Text_IO.Get_Line (Item => YY, Last => Last);

This may not do what you want if Last < 3 or the line being read is > 3 Characters.

Generally, you will want to define your own Get_Line to enforce your requirements:

procedure Get_Line (Item : out XX) is
   Line : constant String := Ada.Text_IO.Get_Line;
begin -- Get_Line
   Item.X1 := Line (line'First);
   Item.X2 := Line (line'First + 1);
   Item.X3 := Line (line'First + 2);
end Get_Line;

This raises Constraint_Error if Line'Length < 3 or any of the first 3 Characters of Line are invalid, and discards any extra Characters at the end.

Often, when dealing with input that may be invalid it is a good idea to input an entire line with the Get_Line function and then parse it, as here. Then it's a simple matter to check things like Line'Length = 3 or use a custom exception.

3
  • Thanks all for the answers. @simon on Gem83, i was able to use removing the private type of SQL_Input. I would be interested to use it with the private type. At Jeffrey Very elegant solution demonstrating the power of Ada. Is it possible to manipulate ''disjoint sets'' as 'A'..'E' OR 'H'..'K', please ? Do we need to use enumerative type by example? Thanks.
    – Mark
    Jun 13, 2022 at 21:02
  • Yes, disjoint sets can be manipulated.
    – Anh Vo
    Jun 15, 2022 at 16:12
  • subtype Disjoint is Character with Dynamic_Predicate => Disjoint in 'A' .. 'E' | 'H' .. 'K'; Jun 16, 2022 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.