(4)
package Interfaces.C is
pragma Pure(C);
(5)
-- Declarations based on C's <limits.h>
(6)
CHAR_BIT : constant := implementation-defined; -- typically 8
SCHAR_MIN : constant := implementation-defined; -- typically -128
SCHAR_MAX : constant := implementation-defined; -- typically 127
UCHAR_MAX : constant := implementation-defined; -- typically 255
(7)
-- Signed and Unsigned Integers
type int is range implementation-defined;
type short is range implementation-defined;
type long is range implementation-defined;
(8)
type signed_char is range SCHAR_MIN .. SCHAR_MAX;
for signed_char'Size use CHAR_BIT;
(9)
type unsigned is mod implementation-defined;
type unsigned_short is mod implementation-defined;
type unsigned_long is mod implementation-defined;
(10)
type unsigned_char is mod (UCHAR_MAX+1);
for unsigned_char'Size use CHAR_BIT;
(11)
subtype plain_char is implementation-defined;
(12)
type ptrdiff_t is range implementation-defined;
(13)
type size_t is mod implementation-defined;
(14)
-- Floating Point
(15)
type C_float is digits implementation-defined;
(16)
type double is digits implementation-defined;
(17)
type long_double is digits implementation-defined;
(18)
-- Characters and Strings
(19)
type char is <implementation-defined character type>;
(20)
nul : constant char := char'First;
(21)
function To_C (Item : in Character) return char;
(22)
function To_Ada (Item : in char) return Character;
(23)
type char_array is array (size_t range <>) of aliased char;
pragma Pack(char_array);
for char_array'Component_Size use CHAR_BIT;
(24)
function Is_Nul_Terminated (Item : in char_array) return Boolean;
(25)
function To_C (Item : in String;
Append_Nul : in Boolean := True)
return char_array;
(26)
function To_Ada (Item : in char_array;
Trim_Nul : in Boolean := True)
return String;
(27)
procedure To_C (Item : in String;
Target : out char_array;
Count : out size_t;
Append_Nul : in Boolean := True);
(28)
procedure To_Ada (Item : in char_array;
Target : out String;
Count : out Natural;
Trim_Nul : in Boolean := True);
(29)
-- Wide Character and Wide String
(30)
type wchar_t is implementation-defined;
(31)
wide_nul : constant wchar_t := wchar_t'First;
(32)
function To_C (Item : in Wide_Character) return wchar_t;
function To_Ada (Item : in wchar_t ) return Wide_Character;
(33)
type wchar_array is array (size_t range <>) of aliased wchar_t;
(34)
pragma Pack(wchar_array);
(35)
function Is_Nul_Terminated (Item : in wchar_array) return Boolean;
(36)
function To_C (Item : in Wide_String;
Append_Nul : in Boolean := True)
return wchar_array;
(37)
function To_Ada (Item : in wchar_array;
Trim_Nul : in Boolean := True)
return Wide_String;
(38)
procedure To_C (Item : in Wide_String;
Target : out wchar_array;
Count : out size_t;
Append_Nul : in Boolean := True);
(39)
procedure To_Ada (Item : in wchar_array;
Target : out Wide_String;
Count : out Natural;
Trim_Nul : in Boolean := True);
(40)
Terminator_Error : exception;
(41)
end Interfaces.C;
(45)
function To_C (Item : in Character) return char;
function To_Ada (Item : in char ) return Character;
(47)
function Is_Nul_Terminated (Item : in char_array) return Boolean;
(49)
function To_C (Item : in String; Append_Nul : in Boolean := True)
return char_array;
function To_Ada (Item : in char_array; Trim_Nul : in Boolean := True)
return String;
(52)
procedure To_C (Item : in String;
Target : out char_array;
Count : out size_t;
Append_Nul : in Boolean := True);
procedure To_Ada (Item : in char_array;
Target : out String;
Count : out Natural;
Trim_Nul : in Boolean := True);
(55)
function Is_Nul_Terminated (Item : in wchar_array) return Boolean;
(57)
function To_C (Item : in Wide_Character) return wchar_t;
function To_Ada (Item : in wchar_t ) return Wide_Character;
(59)
function To_C (Item : in Wide_String;
Append_Nul : in Boolean := True)
return wchar_array;
function To_Ada (Item : in wchar_array;
Trim_Nul : in Boolean := True)
return Wide_String;
procedure To_C (Item : in Wide_String;
Target : out wchar_array;
Count : out size_t;
Append_Nul : in Boolean := True);
procedure To_Ada (Item : in wchar_array;
Target : out Wide_String;
Count : out Natural;
Trim_Nul : in Boolean := True);
(77)
--Calling the C Library Function strcpy
with Interfaces.C;
procedure Test is
package C renames Interfaces.C;
use type C.char_array;
-- Call <string.h>strcpy:
-- C definition of strcpy: char *strcpy(char *s1, const char *s2);
-- This function copies the string pointed to by s2 (including the terminating null character)
-- into the array pointed to by s1. If copying takes place between objects that overlap,
-- the behavior is undefined. The strcpy function returns the valueof s1.
(78)
-- Note: since the C function's return value is of no interest, the Ada interface is a procedure
procedure Strcpy (Target : out C.char_array;
Source : in C.char_array);
(79)
pragma Import(C, Strcpy, "strcpy");
(80)
Chars1 : C.char_array(1..20);
Chars2 : C.char_array(1..20);
(81)
begin
Chars2(1..6) := "qwert" & C.nul;
(82)
Strcpy(Chars1, Chars2);
(83)
-- Now Chars1(1..6) = "qwert" & C.Nul
(84)
end Test;
-- Email comments, additions, corrections, gripes, kudos, etc. to: