1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- I N T E R F A C E S . C --
11 -- Copyright (C) 1992-2001 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 package body Interfaces.C is
38 -----------------------
39 -- Is_Nul_Terminated --
40 -----------------------
44 function Is_Nul_Terminated (Item : char_array) return Boolean is
46 for J in Item'Range loop
47 if Item (J) = nul then
53 end Is_Nul_Terminated;
55 -- Case of wchar_array
57 function Is_Nul_Terminated (Item : wchar_array) return Boolean is
59 for J in Item'Range loop
60 if Item (J) = wide_nul then
66 end Is_Nul_Terminated;
72 -- Convert char to Character
74 function To_Ada (Item : char) return Character is
76 return Character'Val (char'Pos (Item));
79 -- Convert char_array to String (function form)
83 Trim_Nul : Boolean := True)
94 if From > Item'Last then
95 raise Terminator_Error;
96 elsif Item (From) = nul then
103 Count := Natural (From - Item'First);
106 Count := Item'Length;
110 R : String (1 .. Count);
113 for J in R'Range loop
114 R (J) := To_Ada (Item (size_t (J) + (Item'First - 1)));
121 -- Convert char_array to String (procedure form)
127 Trim_Nul : Boolean := True)
136 if From > Item'Last then
137 raise Terminator_Error;
138 elsif Item (From) = nul then
145 Count := Natural (From - Item'First);
148 Count := Item'Length;
151 if Count > Target'Length then
152 raise Constraint_Error;
158 for J in 1 .. Count loop
159 Target (To) := Character (Item (From));
167 -- Convert wchar_t to Wide_Character
169 function To_Ada (Item : wchar_t) return Wide_Character is
171 return Wide_Character (Item);
174 -- Convert wchar_array to Wide_String (function form)
178 Trim_Nul : Boolean := True)
189 if From > Item'Last then
190 raise Terminator_Error;
191 elsif Item (From) = wide_nul then
198 Count := Natural (From - Item'First);
201 Count := Item'Length;
205 R : Wide_String (1 .. Count);
208 for J in R'Range loop
209 R (J) := To_Ada (Item (size_t (J) + (Item'First - 1)));
216 -- Convert wchar_array to Wide_String (procedure form)
220 Target : out Wide_String;
222 Trim_Nul : Boolean := True)
231 if From > Item'Last then
232 raise Terminator_Error;
233 elsif Item (From) = wide_nul then
240 Count := Natural (From - Item'First);
243 Count := Item'Length;
246 if Count > Target'Length then
247 raise Constraint_Error;
253 for J in 1 .. Count loop
254 Target (To) := To_Ada (Item (From));
266 -- Convert Character to char
268 function To_C (Item : Character) return char is
270 return char'Val (Character'Pos (Item));
273 -- Convert String to char_array (function form)
277 Append_Nul : Boolean := True)
283 R : char_array (0 .. Item'Length);
286 for J in Item'Range loop
287 R (size_t (J - Item'First)) := To_C (Item (J));
294 else -- Append_Nul is False
296 -- A nasty case, if the string is null, we must return
297 -- a null char_array. The lower bound of this array is
298 -- required to be zero (RM B.3(50)) but that is of course
299 -- impossible given that size_t is unsigned. This needs
300 -- ARG resolution, but for now GNAT returns bounds 1 .. 0
302 if Item'Length = 0 then
304 R : char_array (1 .. 0);
312 R : char_array (0 .. Item'Length - 1);
315 for J in Item'Range loop
316 R (size_t (J - Item'First)) := To_C (Item (J));
325 -- Convert String to char_array (procedure form)
329 Target : out char_array;
331 Append_Nul : Boolean := True)
336 if Target'Length < Item'Length then
337 raise Constraint_Error;
341 for From in Item'Range loop
342 Target (To) := char (Item (From));
347 if To > Target'Last then
348 raise Constraint_Error;
351 Count := Item'Length + 1;
355 Count := Item'Length;
360 -- Convert Wide_Character to wchar_t
362 function To_C (Item : Wide_Character) return wchar_t is
364 return wchar_t (Item);
367 -- Convert Wide_String to wchar_array (function form)
371 Append_Nul : Boolean := True)
377 R : wchar_array (0 .. Item'Length);
380 for J in Item'Range loop
381 R (size_t (J - Item'First)) := To_C (Item (J));
384 R (R'Last) := wide_nul;
389 -- A nasty case, if the string is null, we must return
390 -- a null char_array. The lower bound of this array is
391 -- required to be zero (RM B.3(50)) but that is of course
392 -- impossible given that size_t is unsigned. This needs
393 -- ARG resolution, but for now GNAT returns bounds 1 .. 0
395 if Item'Length = 0 then
397 R : wchar_array (1 .. 0);
405 R : wchar_array (0 .. Item'Length - 1);
408 for J in size_t range 0 .. Item'Length - 1 loop
409 R (J) := To_C (Item (Integer (J) + Item'First));
418 -- Convert Wide_String to wchar_array (procedure form)
422 Target : out wchar_array;
424 Append_Nul : Boolean := True)
429 if Target'Length < Item'Length then
430 raise Constraint_Error;
434 for From in Item'Range loop
435 Target (To) := To_C (Item (From));
440 if To > Target'Last then
441 raise Constraint_Error;
443 Target (To) := wide_nul;
444 Count := Item'Length + 1;
448 Count := Item'Length;