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-2004 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 -- Note that the type chars_ptr has a pragma No_Strict_Aliasing in
42 -- the spec, to prevent any assumptions about aliasing for values
43 -- of this type, since arbitrary addresses can be converted, and it
44 -- is quite likely that this type will in fact be used for aliasing
45 -- values of other types.
47 function To_chars_ptr
is
48 new Unchecked_Conversion
(Address
, chars_ptr
);
50 function To_Address
is
51 new Unchecked_Conversion
(chars_ptr
, Address
);
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
57 function Peek
(From
: chars_ptr
) return char
;
59 -- Given a chars_ptr value, obtain referenced character
61 procedure Poke
(Value
: char
; Into
: chars_ptr
);
63 -- Given a chars_ptr, modify referenced Character value
65 function "+" (Left
: chars_ptr
; Right
: size_t
) return chars_ptr
;
67 -- Address arithmetic on chars_ptr value
69 function Position_Of_Nul
(Into
: char_array
) return size_t
;
70 -- Returns position of the first Nul in Into or Into'Last + 1 if none
72 -- We can't use directly System.Memory because the categorization is not
73 -- compatible, so we directly import here the malloc and free routines.
75 function Memory_Alloc
(Size
: size_t
) return chars_ptr
;
76 pragma Import
(C
, Memory_Alloc
, "__gnat_malloc");
78 procedure Memory_Free
(Address
: chars_ptr
);
79 pragma Import
(C
, Memory_Free
, "__gnat_free");
85 function "+" (Left
: chars_ptr
; Right
: size_t
) return chars_ptr
is
87 return To_chars_ptr
(To_Address
(Left
) + Storage_Offset
(Right
));
94 procedure Free
(Item
: in out chars_ptr
) is
96 if Item
= Null_Ptr
then
108 function New_Char_Array
(Chars
: char_array
) return chars_ptr
is
113 -- Get index of position of null. If Index > Chars'last,
114 -- nul is absent and must be added explicitly.
116 Index
:= Position_Of_Nul
(Into
=> Chars
);
117 Pointer
:= Memory_Alloc
((Index
- Chars
'First + 1));
119 -- If nul is present, transfer string up to and including it.
121 if Index
<= Chars
'Last then
122 Update
(Item
=> Pointer
,
124 Chars
=> Chars
(Chars
'First .. Index
),
127 -- If original string has no nul, transfer whole string and add
128 -- terminator explicitly.
130 Update
(Item
=> Pointer
,
134 Poke
(nul
, into
=> Pointer
+ size_t
'(Chars'Length));
144 function New_String (Str : String) return chars_ptr is
146 return New_Char_Array (To_C (Str));
153 function Peek (From : chars_ptr) return char is
155 return char (From.all);
162 procedure Poke (Value : char; Into : chars_ptr) is
164 Into.all := Character (Value);
167 ---------------------
168 -- Position_Of_Nul --
169 ---------------------
171 function Position_Of_Nul (Into : char_array) return size_t is
173 for J in Into'Range loop
174 if Into (J) = nul then
179 return Into'Last + 1;
186 function Strlen (Item : chars_ptr) return size_t is
187 Item_Index : size_t := 0;
190 if Item = Null_Ptr then
191 raise Dereference_Error;
195 if Peek (Item + Item_Index) = nul then
199 Item_Index := Item_Index + 1;
207 function To_Chars_Ptr
208 (Item : char_array_access;
209 Nul_Check : Boolean := False) return chars_ptr
215 and then Position_Of_Nul (Into => Item.all) > Item'Last
217 raise Terminator_Error;
219 return To_chars_ptr (Item (Item'First)'Address);
231 Check : Boolean := True)
233 Index : chars_ptr := Item + Offset;
236 if Check and then Offset + Chars'Length > Strlen (Item) then
240 for J in Chars'Range loop
241 Poke (Chars (J), Into => Index);
242 Index := Index + size_t'(1);
250 Check
: Boolean := True)
253 -- Note: in RM 95, the Append_Nul => False parameter is omitted. But
254 -- this has the unintended consequence of truncating the string after
255 -- an update. As discussed in Ada 2005 AI-242, this was unintended,
256 -- and should be corrected. Since this is a clear error, it seems
257 -- appropriate to apply the correction in Ada 95 mode as well.
259 Update
(Item
, Offset
, To_C
(Str
, Append_Nul
=> False), Check
);
266 function Value
(Item
: chars_ptr
) return char_array
is
267 Result
: char_array
(0 .. Strlen
(Item
));
270 if Item
= Null_Ptr
then
271 raise Dereference_Error
;
274 -- Note that the following loop will also copy the terminating Nul
276 for J
in Result
'Range loop
277 Result
(J
) := Peek
(Item
+ J
);
285 Length
: size_t
) return char_array
288 if Item
= Null_Ptr
then
289 raise Dereference_Error
;
292 -- ACATS cxb3010 checks that Constraint_Error gets raised when Length
293 -- is 0. Seems better to check that Length is not null before declaring
294 -- an array with size_t bounds of 0 .. Length - 1 anyway.
297 raise Constraint_Error
;
301 Result
: char_array
(0 .. Length
- 1);
304 for J
in Result
'Range loop
305 Result
(J
) := Peek
(Item
+ J
);
307 if Result
(J
) = nul
then
308 return Result
(0 .. J
);
316 function Value
(Item
: chars_ptr
) return String is
318 return To_Ada
(Value
(Item
));
321 function Value
(Item
: chars_ptr
; Length
: size_t
) return String is
322 Result
: char_array
(0 .. Length
);
325 -- 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
;