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-2008, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Ada.Unchecked_Conversion;
39 package body Interfaces.C.Strings is
41 -- Note that the type chars_ptr has a pragma No_Strict_Aliasing in the
42 -- spec, to prevent any assumptions about aliasing for values of this type,
43 -- since arbitrary addresses can be converted, and it is quite likely that
44 -- this type will in fact be used for aliasing values of other types.
46 function To_chars_ptr is
47 new Ada.Unchecked_Conversion (Address, chars_ptr);
49 function To_Address is
50 new Ada.Unchecked_Conversion (chars_ptr, Address);
52 -----------------------
53 -- Local Subprograms --
54 -----------------------
56 function Peek (From : chars_ptr) return char;
58 -- Given a chars_ptr value, obtain referenced character
60 procedure Poke (Value : char; Into : chars_ptr);
62 -- Given a chars_ptr, modify referenced Character value
64 function "+" (Left : chars_ptr; Right : size_t) return chars_ptr;
66 -- Address arithmetic on chars_ptr value
68 function Position_Of_Nul (Into : char_array) return size_t;
69 -- Returns position of the first Nul in Into or Into'Last + 1 if none
71 -- We can't use directly System.Memory because the categorization is not
72 -- compatible, so we directly import here the malloc and free routines.
74 function Memory_Alloc (Size : size_t) return chars_ptr;
75 pragma Import (C, Memory_Alloc, "__gnat_malloc");
77 procedure Memory_Free (Address : chars_ptr);
78 pragma Import (C, Memory_Free, "__gnat_free");
84 function "+" (Left : chars_ptr; Right : size_t) return chars_ptr is
86 return To_chars_ptr (To_Address (Left) + Storage_Offset (Right));
93 procedure Free (Item : in out chars_ptr) is
95 if Item = Null_Ptr then
107 function New_Char_Array (Chars : char_array) return chars_ptr is
112 -- Get index of position of null. If Index > Chars'Last,
113 -- nul is absent and must be added explicitly.
115 Index := Position_Of_Nul (Into => Chars);
116 Pointer := Memory_Alloc ((Index - Chars'First + 1));
118 -- If nul is present, transfer string up to and including nul
120 if Index <= Chars'Last then
121 Update (Item => Pointer,
123 Chars => Chars (Chars'First .. Index),
126 -- If original string has no nul, transfer whole string and add
127 -- terminator explicitly.
129 Update (Item => Pointer,
133 Poke (nul, Into => Pointer + size_t'(Chars'Length));
143 function New_String (Str : String) return chars_ptr is
145 return New_Char_Array (To_C (Str));
152 function Peek (From : chars_ptr) return char is
154 return char (From.all);
161 procedure Poke (Value : char; Into : chars_ptr) is
163 Into.all := Character (Value);
166 ---------------------
167 -- Position_Of_Nul --
168 ---------------------
170 function Position_Of_Nul (Into : char_array) return size_t is
172 for J in Into'Range loop
173 if Into (J) = nul then
178 return Into'Last + 1;
185 function Strlen (Item : chars_ptr) return size_t is
186 Item_Index : size_t := 0;
189 if Item = Null_Ptr then
190 raise Dereference_Error;
194 if Peek (Item + Item_Index) = nul then
198 Item_Index := Item_Index + 1;
206 function To_Chars_Ptr
207 (Item : char_array_access;
208 Nul_Check : Boolean := False) return chars_ptr
214 and then Position_Of_Nul (Into => Item.all) > Item'Last
216 raise Terminator_Error;
218 return To_chars_ptr (Item (Item'First)'Address);
230 Check : Boolean := True)
232 Index : chars_ptr := Item + Offset;
235 if Check and then Offset + Chars'Length > Strlen (Item) then
239 for J in Chars'Range loop
240 Poke (Chars (J), Into => Index);
241 Index := Index + size_t'(1);
249 Check : Boolean := True)
252 -- Note: in RM 95, the Append_Nul => False parameter is omitted. But
253 -- this has the unintended consequence of truncating the string after
254 -- an update. As discussed in Ada 2005 AI-242, this was unintended,
255 -- and should be corrected. Since this is a clear error, it seems
256 -- appropriate to apply the correction in Ada 95 mode as well.
258 Update (Item, Offset, To_C (Str, Append_Nul => False), Check);
265 function Value (Item : chars_ptr) return char_array is
266 Result : char_array (0 .. Strlen (Item));
269 if Item = Null_Ptr then
270 raise Dereference_Error;
273 -- Note that the following loop will also copy the terminating Nul
275 for J in Result'Range loop
276 Result (J) := Peek (Item + J);
284 Length : size_t) return char_array
287 if Item = Null_Ptr then
288 raise Dereference_Error;
291 -- ACATS cxb3010 checks that Constraint_Error gets raised when Length
292 -- is 0. Seems better to check that Length is not null before declaring
293 -- an array with size_t bounds of 0 .. Length - 1 anyway.
296 raise Constraint_Error;
300 Result : char_array (0 .. Length - 1);
303 for J in Result'Range loop
304 Result (J) := Peek (Item + J);
306 if Result (J) = nul then
307 return Result (0 .. J);
315 function Value (Item : chars_ptr) return String is
317 return To_Ada (Value (Item));
320 function Value (Item : chars_ptr; Length : size_t) return String is
321 Result : char_array (0 .. Length);
324 -- As per AI-00177, this is equivalent to:
326 -- To_Ada (Value (Item, Length) & nul);
328 if Item = Null_Ptr then
329 raise Dereference_Error;
332 for J in 0 .. Length - 1 loop
333 Result (J) := Peek (Item + J);
335 if Result (J) = nul then
336 return To_Ada (Result (0 .. J));
340 Result (Length) := nul;
341 return To_Ada (Result);
344 end Interfaces.C.Strings;