with System; use System; Package body C_String_Pkg is function C_String_Length( C_Str : in C_String; Include_Terminator : in boolean := FALSE) return natural is Length: natural := 0; Start : System.Address; Char : character; begin if (C_Str = NULL) then return ( 0 ); end if; Start := C_Str.all'ADDRESS; loop -- Get the next char by calculating its position relative to the start Char := Fetch_Char(Start + OFFSET(Length)); exit when Char = Ascii.NUL; Length := Length + 1; end loop; if Include_Terminator then Length := Length + 1; end if; return ( Length ); end C_String_Length; function C_to_Ada_String( C_Str : in C_String; Include_Terminator : in boolean := FALSE) return Ada_String is Ada_Str : Ada_String := NULL; Length : natural := 0; C_Start : System.Address; Char : character; begin Length := C_String_Length(C_Str, Include_Terminator); if (Length = 0) then return ( NULL ); end if; Ada_Str := new string(1 .. Length); C_Start := C_Str.all'ADDRESS; for Pos in 0 .. (Length - 1) loop Char := Fetch_Char(C_Start + OFFSET(Pos)); Ada_Str.all(Pos + 1) := Char; end loop; return ( Ada_Str ); exception when STORAGE_ERROR => return NULL; end C_to_Ada_String; procedure C_to_Ada_String( C_Str : in C_String; The_String : out string; Include_Term : in boolean := FALSE) is Length : natural := 0; begin Length := C_String_Length(C_Str, Include_Term); if Length > The_String'LENGTH then Length := The_String'LENGTH; end if; if Length > 0 then Length := Length - 1; for Index in 0 .. Length loop The_String(Index+1) := Get_Char(C_Str, Index); end loop; end if; end C_to_Ada_String; function Get_Char( C_Str : in C_String; Position : in natural) return character is C_Start : System.Address; Char : character; begin if (Position > C_String_Length(C_Str, FALSE)) then return ( Ascii.NUL ); end if; C_Start := C_Str.all'ADDRESS; Char := Fetch_Char(C_Start + OFFSET(Position)); return ( Char ); end Get_Char; end C_String_Pkg;