--  Interfaces.C (RM B.3): the C types of the platform (LP64: macOS and Linux on 64-bit
--  machines) and the conversions between Ada strings and C character arrays.
package Interfaces.C with Pure is
   type int is range -2 ** 31 .. 2 ** 31 - 1;
   type short is range -2 ** 15 .. 2 ** 15 - 1;
   type long is range -2 ** 63 .. 2 ** 63 - 1;
   type signed_char is range -128 .. 127;
   type unsigned is mod 2 ** 32;
   type unsigned_short is mod 2 ** 16;
   type unsigned_long is mod 2 ** 64;
   type unsigned_char is mod 2 ** 8;
   type size_t is mod 2 ** 64;
   type C_float is digits 6;
   type double is digits 15;

   type char is new Character;
   nul : constant char := char'First;

   function To_C (Item : Character) return char is (char (Item));
   function To_Ada (Item : char) return Character is (Character (Item));

   type char_array is array (size_t range <>) of char;

   --  The characters of Item from index 0, then a nul unless Append_Nul is False
   function To_C (Item : String; Append_Nul : Boolean := True) return char_array
     with Global => null,
          Pre  => Append_Nul or Item'Length > 0,
          Post => To_C'Result'First = 0
                  and To_C'Result'Length = Item'Length + (if Append_Nul then 1 else 0)
                  and (if Append_Nul then To_C'Result (To_C'Result'Last) = nul);

   --  The characters of Item up to its first nul (or all of them unless Trim_Nul is True)
   function To_Ada (Item : char_array; Trim_Nul : Boolean := True) return String
     with Global => null,
          Pre => (Item'Last < Item'First or else Item'Last - Item'First < size_t (Integer'Last) - 1)
                 and (if Trim_Nul then (for some I in Item'Range => Item (I) = nul)),
          --  (from 1, and no longer than Item: RM B.3(44))
          Post => To_Ada'Result'First = 1 and To_Ada'Result'Length <= Item'Length;
end Interfaces.C;
