1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- I N T E R F A C E S . C --
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 package body Interfaces
.C
is
37 -----------------------
38 -- Is_Nul_Terminated --
39 -----------------------
43 function Is_Nul_Terminated
(Item
: char_array
) return Boolean is
45 for J
in Item
'Range loop
46 if Item
(J
) = nul
then
52 end Is_Nul_Terminated
;
54 -- Case of wchar_array
56 function Is_Nul_Terminated
(Item
: wchar_array
) return Boolean is
58 for J
in Item
'Range loop
59 if Item
(J
) = wide_nul
then
65 end Is_Nul_Terminated
;
71 -- Convert char to Character
73 function To_Ada
(Item
: char
) return Character is
75 return Character'Val (char
'Pos (Item
));
78 -- Convert char_array to String (function form)
82 Trim_Nul
: Boolean := True)
93 if From
> Item
'Last then
94 raise Terminator_Error
;
95 elsif Item
(From
) = nul
then
102 Count
:= Natural (From
- Item
'First);
105 Count
:= Item
'Length;
109 R
: String (1 .. Count
);
112 for J
in R
'Range loop
113 R
(J
) := To_Ada
(Item
(size_t
(J
) + (Item
'First - 1)));
120 -- Convert char_array to String (procedure form)
126 Trim_Nul
: Boolean := True)
135 if From
> Item
'Last then
136 raise Terminator_Error
;
137 elsif Item
(From
) = nul
then
144 Count
:= Natural (From
- Item
'First);
147 Count
:= Item
'Length;
150 if Count
> Target
'Length then
151 raise Constraint_Error
;
157 for J
in 1 .. Count
loop
158 Target
(To
) := Character (Item
(From
));
166 -- Convert wchar_t to Wide_Character
168 function To_Ada
(Item
: wchar_t
) return Wide_Character is
170 return Wide_Character (Item
);
173 -- Convert wchar_array to Wide_String (function form)
177 Trim_Nul
: Boolean := True)
188 if From
> Item
'Last then
189 raise Terminator_Error
;
190 elsif Item
(From
) = wide_nul
then
197 Count
:= Natural (From
- Item
'First);
200 Count
:= Item
'Length;
204 R
: Wide_String (1 .. Count
);
207 for J
in R
'Range loop
208 R
(J
) := To_Ada
(Item
(size_t
(J
) + (Item
'First - 1)));
215 -- Convert wchar_array to Wide_String (procedure form)
219 Target
: out Wide_String;
221 Trim_Nul
: Boolean := True)
230 if From
> Item
'Last then
231 raise Terminator_Error
;
232 elsif Item
(From
) = wide_nul
then
239 Count
:= Natural (From
- Item
'First);
242 Count
:= Item
'Length;
245 if Count
> Target
'Length then
246 raise Constraint_Error
;
252 for J
in 1 .. Count
loop
253 Target
(To
) := To_Ada
(Item
(From
));
265 -- Convert Character to char
267 function To_C
(Item
: Character) return char
is
269 return char
'Val (Character'Pos (Item
));
272 -- Convert String to char_array (function form)
276 Append_Nul
: Boolean := True)
282 R
: char_array
(0 .. Item
'Length);
285 for J
in Item
'Range loop
286 R
(size_t
(J
- Item
'First)) := To_C
(Item
(J
));
293 else -- Append_Nul is False
295 -- A nasty case, if the string is null, we must return
296 -- a null char_array. The lower bound of this array is
297 -- required to be zero (RM B.3(50)) but that is of course
298 -- impossible given that size_t is unsigned. This needs
299 -- ARG resolution, but for now GNAT returns bounds 1 .. 0
301 if Item
'Length = 0 then
303 R
: char_array
(1 .. 0);
311 R
: char_array
(0 .. Item
'Length - 1);
314 for J
in Item
'Range loop
315 R
(size_t
(J
- Item
'First)) := To_C
(Item
(J
));
324 -- Convert String to char_array (procedure form)
328 Target
: out char_array
;
330 Append_Nul
: Boolean := True)
335 if Target
'Length < Item
'Length then
336 raise Constraint_Error
;
340 for From
in Item
'Range loop
341 Target
(To
) := char
(Item
(From
));
346 if To
> Target
'Last then
347 raise Constraint_Error
;
350 Count
:= Item
'Length + 1;
354 Count
:= Item
'Length;
359 -- Convert Wide_Character to wchar_t
361 function To_C
(Item
: Wide_Character) return wchar_t
is
363 return wchar_t
(Item
);
366 -- Convert Wide_String to wchar_array (function form)
370 Append_Nul
: Boolean := True)
376 R
: wchar_array
(0 .. Item
'Length);
379 for J
in Item
'Range loop
380 R
(size_t
(J
- Item
'First)) := To_C
(Item
(J
));
383 R
(R
'Last) := wide_nul
;
388 -- A nasty case, if the string is null, we must return
389 -- a null char_array. The lower bound of this array is
390 -- required to be zero (RM B.3(50)) but that is of course
391 -- impossible given that size_t is unsigned. This needs
392 -- ARG resolution, but for now GNAT returns bounds 1 .. 0
394 if Item
'Length = 0 then
396 R
: wchar_array
(1 .. 0);
404 R
: wchar_array
(0 .. Item
'Length - 1);
407 for J
in size_t
range 0 .. Item
'Length - 1 loop
408 R
(J
) := To_C
(Item
(Integer (J
) + Item
'First));
417 -- Convert Wide_String to wchar_array (procedure form)
421 Target
: out wchar_array
;
423 Append_Nul
: Boolean := True)
428 if Target
'Length < Item
'Length then
429 raise Constraint_Error
;
433 for From
in Item
'Range loop
434 Target
(To
) := To_C
(Item
(From
));
439 if To
> Target
'Last then
440 raise Constraint_Error
;
442 Target
(To
) := wide_nul
;
443 Count
:= Item
'Length + 1;
447 Count
:= Item
'Length;