gnatprove_11.2.3_f7ece6d3/share/examples/spark/spark_book/Chapter-07/terminal.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
with Ada.Characters.Latin_1;
package body Terminal
   with Spark_Mode => On
is
   use type Serial_Port.Status_Type;

   procedure Get_Line (Buffer : out String;
                       Count  : out Natural;
                       Status : out Status_Type) is
      Value       : Serial_Port.Byte;
      Port_Status : Serial_Port.Status_Type;
   begin
      Buffer := (others => ' ');
      Count  := 0;
      Status := Success;
      loop
         pragma Loop_Invariant (Count <= Buffer'Length);

         -- Check to be sure there is space remaining in the buffer.
         if Count = Buffer'Length then
            Status := Insufficient_Space;
            exit;
         end if;

         Serial_Port.Read (Value, Port_Status);

         -- Check to be sure a byte was successfully read.
         if Port_Status /= Serial_Port.Success then
            Status := Port_Failure;
            exit;
         end if;

         -- We are done if a carriage return is read.
         exit when Character'Val (Value) = Ada.Characters.Latin_1.CR;

         -- Convert Value (a Byte) to a character and append to Buffer
         Buffer (Buffer'First + Count) := Character'Val (Value);
         Count := Count + 1;
      end loop;
   end Get_Line;

end Terminal;