1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- I N T E R F A C E S . C _ S T R E A M S --
9 -- Copyright (C) 1996-2005, 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 -- This is the Alpha/VMS version
36 with Unchecked_Conversion;
37 package body Interfaces.C_Streams is
39 use type System.CRTL.size_t;
41 -- As the functions fread, fwrite and setvbuf are too big to be inlined,
42 -- they are just wrappers to the following implementation functions.
48 stream : FILEs) return size_t;
55 stream : FILEs) return size_t;
61 stream : FILEs) return size_t;
67 size : size_t) return int;
77 stream : FILEs) return size_t
79 Get_Count : size_t := 0;
81 type Buffer_Type is array (size_t range 1 .. count,
82 size_t range 1 .. size) of Character;
83 type Buffer_Access is access Buffer_Type;
84 function To_BA is new Unchecked_Conversion (voids, Buffer_Access);
86 BA : constant Buffer_Access := To_BA (buffer);
90 -- This Fread goes with the Fwrite below. The C library fread sometimes
91 -- can't read fputc generated files.
93 for C in 1 .. count loop
94 for S in 1 .. size loop
101 BA.all (C, S) := Character'Val (Ch);
104 Get_Count := Get_Count + 1;
115 stream : FILEs) return size_t
117 Get_Count : size_t := 0;
119 type Buffer_Type is array (size_t range 1 .. count,
120 size_t range 1 .. size) of Character;
121 type Buffer_Access is access Buffer_Type;
122 function To_BA is new Unchecked_Conversion (voids, Buffer_Access);
124 BA : constant Buffer_Access := To_BA (buffer);
128 -- This Fread goes with the Fwrite below. The C library fread sometimes
129 -- can't read fputc generated files.
131 for C in 1 + index .. count + index loop
132 for S in 1 .. size loop
133 Ch := fgetc (stream);
139 BA.all (C, S) := Character'Val (Ch);
142 Get_Count := Get_Count + 1;
152 stream : FILEs) return size_t
155 return fread_impl (buffer, size, count, stream);
163 stream : FILEs) return size_t
166 return fread_impl (buffer, index, size, count, stream);
177 stream : FILEs) return size_t
179 Put_Count : size_t := 0;
181 type Buffer_Type is array (size_t range 1 .. count,
182 size_t range 1 .. size) of Character;
183 type Buffer_Access is access Buffer_Type;
184 function To_BA is new Unchecked_Conversion (voids, Buffer_Access);
186 BA : constant Buffer_Access := To_BA (buffer);
189 -- Fwrite on VMS has the undesirable effect of always generating at
190 -- least one record of output per call, regardless of buffering. To
191 -- get around this, we do multiple fputc calls instead.
193 for C in 1 .. count loop
194 for S in 1 .. size loop
195 if fputc (Character'Pos (BA.all (C, S)), stream) = EOF then
200 Put_Count := Put_Count + 1;
210 stream : FILEs) return size_t
213 return fwrite_impl (buffer, size, count, stream);
220 function setvbuf_impl
224 size : size_t) return int
226 use type System.Address;
229 -- In order for the above fwrite hack to work, we must always buffer
230 -- stdout and stderr. Is_regular_file on VMS cannot detect when
231 -- these are redirected to a file, so checking for that condition
235 and then (stream = stdout or else stream = stderr)
237 return System.CRTL.setvbuf
238 (stream, buffer, IOLBF, System.CRTL.size_t (size));
240 return System.CRTL.setvbuf
241 (stream, buffer, mode, System.CRTL.size_t (size));
249 size : size_t) return int
252 return setvbuf_impl (stream, buffer, mode, size);
255 end Interfaces.C_Streams;