1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- I N T E R F A C E S . C . S T R I N G S --
9 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with System; use System;
35 with System.Storage_Elements; use System.Storage_Elements;
37 with Unchecked_Conversion;
39 package body Interfaces.C.Strings is
41 function To_chars_ptr is
42 new Unchecked_Conversion (Address, chars_ptr);
44 function To_Address is
45 new Unchecked_Conversion (chars_ptr, Address);
47 -----------------------
48 -- Local Subprograms --
49 -----------------------
51 function Peek (From : chars_ptr) return char;
53 -- Given a chars_ptr value, obtain referenced character
55 procedure Poke (Value : char; Into : chars_ptr);
57 -- Given a chars_ptr, modify referenced Character value
59 function "+" (Left : chars_ptr; Right : size_t) return chars_ptr;
61 -- Address arithmetic on chars_ptr value
63 function Position_Of_Nul (Into : char_array) return size_t;
64 -- Returns position of the first Nul in Into or Into'Last + 1 if none
66 -- We can't use directly System.Memory because the categorization is not
67 -- compatible, so we directly import here the malloc and free routines.
69 function Memory_Alloc (Size : size_t) return chars_ptr;
70 pragma Import (C, Memory_Alloc, "__gnat_malloc");
72 procedure Memory_Free (Address : chars_ptr);
73 pragma Import (C, Memory_Free, "__gnat_free");
79 function "+" (Left : chars_ptr; Right : size_t) return chars_ptr is
81 return To_chars_ptr (To_Address (Left) + Storage_Offset (Right));
88 procedure Free (Item : in out chars_ptr) is
90 if Item = Null_Ptr then
102 function New_Char_Array (Chars : in char_array) return chars_ptr is
107 -- Get index of position of null. If Index > Chars'last,
108 -- nul is absent and must be added explicitly.
110 Index := Position_Of_Nul (Into => Chars);
111 Pointer := Memory_Alloc ((Index - Chars'First + 1));
113 -- If nul is present, transfer string up to and including it.
115 if Index <= Chars'Last then
116 Update (Item => Pointer,
118 Chars => Chars (Chars'First .. Index),
121 -- If original string has no nul, transfer whole string and add
122 -- terminator explicitly.
124 Update (Item => Pointer,
128 Poke (nul, into => Pointer + size_t'(Chars'Length));
138 function New_String (Str : in String) return chars_ptr is
140 return New_Char_Array (To_C (Str));
147 function Peek (From : chars_ptr) return char is
149 return char (From.all);
156 procedure Poke (Value : char; Into : chars_ptr) is
158 Into.all := Character (Value);
161 ---------------------
162 -- Position_Of_Nul --
163 ---------------------
165 function Position_Of_Nul (Into : char_array) return size_t is
167 for J in Into'Range loop
168 if Into (J) = nul then
173 return Into'Last + 1;
180 function Strlen (Item : in chars_ptr) return size_t is
181 Item_Index : size_t := 0;
184 if Item = Null_Ptr then
185 raise Dereference_Error;
189 if Peek (Item + Item_Index) = nul then
193 Item_Index := Item_Index + 1;
201 function To_Chars_Ptr
202 (Item : in char_array_access;
203 Nul_Check : in Boolean := False)
210 and then Position_Of_Nul (Into => Item.all) > Item'Last
212 raise Terminator_Error;
214 return To_chars_ptr (Item (Item'First)'Address);
224 (Item : in chars_ptr;
226 Chars : in char_array;
227 Check : Boolean := True)
229 Index : chars_ptr := Item + Offset;
232 if Check and then Offset + Chars'Length > Strlen (Item) then
236 for J in Chars'Range loop
237 Poke (Chars (J), Into => Index);
238 Index := Index + size_t'(1);
243 (Item : in chars_ptr;
246 Check : in Boolean := True)
249 Update (Item, Offset, To_C (Str), Check);
256 function Value (Item : in chars_ptr) return char_array is
257 Result : char_array (0 .. Strlen (Item));
260 if Item = Null_Ptr then
261 raise Dereference_Error;
264 -- Note that the following loop will also copy the terminating Nul
266 for J in Result'Range loop
267 Result (J) := Peek (Item + J);
274 (Item : in chars_ptr;
279 if Item = Null_Ptr then
280 raise Dereference_Error;
283 -- ACATS cxb3010 checks that Constraint_Error gets raised when Length
284 -- is 0. Seems better to check that Length is not null before declaring
285 -- an array with size_t bounds of 0 .. Length - 1 anyway.
288 raise Constraint_Error;
292 Result : char_array (0 .. Length - 1);
295 for J in Result'Range loop
296 Result (J) := Peek (Item + J);
298 if Result (J) = nul then
299 return Result (0 .. J);
307 function Value (Item : in chars_ptr) return String is
309 return To_Ada (Value (Item));
312 -- As per AI-00177, this is equivalent to
313 -- To_Ada (Value (Item, Length) & nul);
315 function Value (Item : in chars_ptr; Length : in size_t) return String is
316 Result : char_array (0 .. Length);
319 if Item = Null_Ptr then
320 raise Dereference_Error;
323 for J in 0 .. Length - 1 loop
324 Result (J) := Peek (Item + J);
326 if Result (J) = nul then
327 return To_Ada (Result (0 .. J));
331 Result (Length) := nul;
332 return To_Ada (Result);
335 end Interfaces.C.Strings;