1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- I N T E R F A C E S . C . S T R I N G S --
11 -- Copyright (C) 1992-2000 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 with System; use System;
37 with System.Address_To_Access_Conversions;
39 package body Interfaces.C.Strings is
41 package Char_Access is new Address_To_Access_Conversions (char);
43 -----------------------
44 -- Local Subprograms --
45 -----------------------
47 function Peek (From : chars_ptr) return char;
49 -- Given a chars_ptr value, obtain referenced character
51 procedure Poke (Value : char; Into : chars_ptr);
53 -- Given a chars_ptr, modify referenced Character value
55 function "+" (Left : chars_ptr; Right : size_t) return chars_ptr;
57 -- Address arithmetic on chars_ptr value
59 function Position_Of_Nul (Into : char_array) return size_t;
60 -- Returns position of the first Nul in Into or Into'Last + 1 if none
62 function C_Malloc (Size : size_t) return chars_ptr;
63 pragma Import (C, C_Malloc, "__gnat_malloc");
65 procedure C_Free (Address : chars_ptr);
66 pragma Import (C, C_Free, "__gnat_free");
72 function "+" (Left : chars_ptr; Right : size_t) return chars_ptr is
74 return Left + chars_ptr (Right);
81 procedure Free (Item : in out chars_ptr) is
83 if Item = Null_Ptr then
95 function New_Char_Array (Chars : in char_array) return chars_ptr is
100 -- Get index of position of null. If Index > Chars'last,
101 -- nul is absent and must be added explicitly.
103 Index := Position_Of_Nul (Into => Chars);
104 Pointer := C_Malloc ((Index - Chars'First + 1));
106 -- If nul is present, transfer string up to and including it.
108 if Index <= Chars'Last then
109 Update (Item => Pointer,
111 Chars => Chars (Chars'First .. Index),
114 -- If original string has no nul, transfer whole string and add
115 -- terminator explicitly.
117 Update (Item => Pointer,
121 Poke (nul, into => Pointer + size_t '(Chars'Length));
131 function New_String (Str : in String) return chars_ptr is
133 return New_Char_Array (To_C (Str));
140 function Peek (From : chars_ptr) return char is
143 return To_Pointer (Address (To_Address (From))).all;
150 procedure Poke (Value : char; Into : chars_ptr) is
153 To_Pointer (Address (To_Address (Into))).all := Value;
156 ---------------------
157 -- Position_Of_Nul --
158 ---------------------
160 function Position_Of_Nul (Into : char_array) return size_t is
162 for J in Into'Range loop
163 if Into (J) = nul then
168 return Into'Last + 1;
175 function Strlen (Item : in chars_ptr) return size_t is
176 Item_Index : size_t := 0;
179 if Item = Null_Ptr then
180 raise Dereference_Error;
184 if Peek (Item + Item_Index) = nul then
188 Item_Index := Item_Index + 1;
196 function To_Chars_Ptr
197 (Item : in char_array_access;
198 Nul_Check : in Boolean := False)
205 and then Position_Of_Nul (Into => Item.all) > Item'Last
207 raise Terminator_Error;
209 return To_Integer (Item (Item'First)'Address);
218 (Item : in chars_ptr;
220 Chars : in char_array;
221 Check : Boolean := True)
223 Index : chars_ptr := Item + Offset;
226 if Check and then Offset + Chars'Length > Strlen (Item) then
230 for J in Chars'Range loop
231 Poke (Chars (J), Into => Index);
232 Index := Index + size_t'(1);
237 (Item : in chars_ptr;
240 Check : in Boolean := True)
243 Update (Item, Offset, To_C (Str), Check);
250 function Value (Item : in chars_ptr) return char_array is
251 Result : char_array (0 .. Strlen (Item));
254 if Item = Null_Ptr then
255 raise Dereference_Error;
258 -- Note that the following loop will also copy the terminating Nul
260 for J in Result'Range loop
261 Result (J) := Peek (Item + J);
268 (Item : in chars_ptr;
273 if Item = Null_Ptr then
274 raise Dereference_Error;
277 -- ACATS cxb3010 checks that Constraint_Error gets raised when Length
278 -- is 0. Seems better to check that Length is not null before declaring
279 -- an array with size_t bounds of 0 .. Length - 1 anyway.
282 raise Constraint_Error;
286 Result : char_array (0 .. Length - 1);
289 for J in Result'Range loop
290 Result (J) := Peek (Item + J);
292 if Result (J) = nul then
293 return Result (0 .. J);
301 function Value (Item : in chars_ptr) return String is
303 return To_Ada (Value (Item));
306 -- As per AI-00177, this is equivalent to
307 -- To_Ada (Value (Item, Length) & nul);
309 function Value (Item : in chars_ptr; Length : in size_t) return String is
310 Result : char_array (0 .. Length);
313 if Item = Null_Ptr then
314 raise Dereference_Error;
317 for J in 0 .. Length - 1 loop
318 Result (J) := Peek (Item + J);
320 if Result (J) = nul then
321 return To_Ada (Result (0 .. J));
325 Result (Length) := nul;
326 return To_Ada (Result);
329 end Interfaces.C.Strings;