1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- I N T E R F A C E S . C . S T R I N G S --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 with System; use System;
36 with System.Address_To_Access_Conversions;
38 package body Interfaces.C.Strings is
40 package Char_Access is new Address_To_Access_Conversions (char);
42 -----------------------
43 -- Local Subprograms --
44 -----------------------
46 function Peek (From : chars_ptr) return char;
48 -- Given a chars_ptr value, obtain referenced character
50 procedure Poke (Value : char; Into : chars_ptr);
52 -- Given a chars_ptr, modify referenced Character value
54 function "+" (Left : chars_ptr; Right : size_t) return chars_ptr;
56 -- Address arithmetic on chars_ptr value
58 function Position_Of_Nul (Into : char_array) return size_t;
59 -- Returns position of the first Nul in Into or Into'Last + 1 if none
61 -- We can't use directly System.Memory because the categorization is not
62 -- compatible, so we directly import here the malloc and free routines.
64 function Memory_Alloc (Size : size_t) return chars_ptr;
65 pragma Import (C, Memory_Alloc, "__gnat_malloc");
67 procedure Memory_Free (Address : chars_ptr);
68 pragma Import (C, Memory_Free, "__gnat_free");
74 function "+" (Left : chars_ptr; Right : size_t) return chars_ptr is
76 return Left + chars_ptr (Right);
83 procedure Free (Item : in out chars_ptr) is
85 if Item = Null_Ptr then
97 function New_Char_Array (Chars : in char_array) return chars_ptr is
102 -- Get index of position of null. If Index > Chars'last,
103 -- nul is absent and must be added explicitly.
105 Index := Position_Of_Nul (Into => Chars);
106 Pointer := Memory_Alloc ((Index - Chars'First + 1));
108 -- If nul is present, transfer string up to and including it.
110 if Index <= Chars'Last then
111 Update (Item => Pointer,
113 Chars => Chars (Chars'First .. Index),
116 -- If original string has no nul, transfer whole string and add
117 -- terminator explicitly.
119 Update (Item => Pointer,
123 Poke (nul, into => Pointer + size_t '(Chars'Length));
133 function New_String (Str : in String) return chars_ptr is
135 return New_Char_Array (To_C (Str));
142 function Peek (From : chars_ptr) return char is
145 return To_Pointer (Address (To_Address (From))).all;
152 procedure Poke (Value : char; Into : chars_ptr) is
155 To_Pointer (Address (To_Address (Into))).all := Value;
158 ---------------------
159 -- Position_Of_Nul --
160 ---------------------
162 function Position_Of_Nul (Into : char_array) return size_t is
164 for J in Into'Range loop
165 if Into (J) = nul then
170 return Into'Last + 1;
177 function Strlen (Item : in chars_ptr) return size_t is
178 Item_Index : size_t := 0;
181 if Item = Null_Ptr then
182 raise Dereference_Error;
186 if Peek (Item + Item_Index) = nul then
190 Item_Index := Item_Index + 1;
198 function To_Chars_Ptr
199 (Item : in char_array_access;
200 Nul_Check : in Boolean := False)
207 and then Position_Of_Nul (Into => Item.all) > Item'Last
209 raise Terminator_Error;
211 return To_Integer (Item (Item'First)'Address);
220 (Item : in chars_ptr;
222 Chars : in char_array;
223 Check : Boolean := True)
225 Index : chars_ptr := Item + Offset;
228 if Check and then Offset + Chars'Length > Strlen (Item) then
232 for J in Chars'Range loop
233 Poke (Chars (J), Into => Index);
234 Index := Index + size_t'(1);
239 (Item : in chars_ptr;
242 Check : in Boolean := True)
245 Update (Item, Offset, To_C (Str), Check);
252 function Value (Item : in chars_ptr) return char_array is
253 Result : char_array (0 .. Strlen (Item));
256 if Item = Null_Ptr then
257 raise Dereference_Error;
260 -- Note that the following loop will also copy the terminating Nul
262 for J in Result'Range loop
263 Result (J) := Peek (Item + J);
270 (Item : in chars_ptr;
275 if Item = Null_Ptr then
276 raise Dereference_Error;
279 -- ACATS cxb3010 checks that Constraint_Error gets raised when Length
280 -- is 0. Seems better to check that Length is not null before declaring
281 -- an array with size_t bounds of 0 .. Length - 1 anyway.
284 raise Constraint_Error;
288 Result : char_array (0 .. Length - 1);
291 for J in Result'Range loop
292 Result (J) := Peek (Item + J);
294 if Result (J) = nul then
295 return Result (0 .. J);
303 function Value (Item : in chars_ptr) return String is
305 return To_Ada (Value (Item));
308 -- As per AI-00177, this is equivalent to
309 -- To_Ada (Value (Item, Length) & nul);
311 function Value (Item : in chars_ptr; Length : in size_t) return String is
312 Result : char_array (0 .. Length);
315 if Item = Null_Ptr then
316 raise Dereference_Error;
319 for J in 0 .. Length - 1 loop
320 Result (J) := Peek (Item + J);
322 if Result (J) = nul then
323 return To_Ada (Result (0 .. J));
327 Result (Length) := nul;
328 return To_Ada (Result);
331 end Interfaces.C.Strings;