OSDN Git Service

2003-12-11 Ed Falis <falis@gnat.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / 6vcstrea.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                 I N T E R F A C E S . C _ S T R E A M S                  --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1996-2002 Free Software Foundation, Inc.          --
10 --                                                                          --
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.                                                      --
21 --                                                                          --
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.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This is the Alpha/VMS version.
35
36 with Unchecked_Conversion;
37 package body Interfaces.C_Streams is
38
39    ------------
40    -- fread --
41    ------------
42
43    function fread
44      (buffer : voids;
45       size   : size_t;
46       count  : size_t;
47       stream : FILEs)
48       return   size_t
49    is
50       Get_Count : size_t := 0;
51       type Buffer_Type is array (size_t range 1 .. count,
52                                  size_t range 1 .. size) of Character;
53       type Buffer_Access is access Buffer_Type;
54       function To_BA is new Unchecked_Conversion (voids, Buffer_Access);
55       BA : Buffer_Access := To_BA (buffer);
56       Ch : int;
57    begin
58
59       --  This Fread goes with the Fwrite below.
60       --  The C library fread sometimes can't read fputc generated files.
61
62       for C in 1 .. count loop
63          for S in 1 .. size loop
64             Ch := fgetc (stream);
65             if Ch = EOF then
66                return Get_Count;
67             end if;
68             BA.all (C, S) := Character'Val (Ch);
69          end loop;
70          Get_Count := Get_Count + 1;
71       end loop;
72       return Get_Count;
73    end fread;
74
75    ------------
76    -- fread --
77    ------------
78
79    function fread
80      (buffer : voids;
81       index  : size_t;
82       size   : size_t;
83       count  : size_t;
84       stream : FILEs)
85       return   size_t
86    is
87       Get_Count : size_t := 0;
88       type Buffer_Type is array (size_t range 1 .. count,
89                                  size_t range 1 .. size) of Character;
90       type Buffer_Access is access Buffer_Type;
91       function To_BA is new Unchecked_Conversion (voids, Buffer_Access);
92       BA : Buffer_Access := To_BA (buffer);
93       Ch : int;
94    begin
95
96       --  This Fread goes with the Fwrite below.
97       --  The C library fread sometimes can't read fputc generated files.
98
99       for C in 1 + index .. count + index loop
100          for S in 1 .. size loop
101             Ch := fgetc (stream);
102             if Ch = EOF then
103                return Get_Count;
104             end if;
105             BA.all (C, S) := Character'Val (Ch);
106          end loop;
107          Get_Count := Get_Count + 1;
108       end loop;
109       return Get_Count;
110    end fread;
111
112    ------------
113    -- fwrite --
114    ------------
115
116    function fwrite
117      (buffer : voids;
118       size   : size_t;
119       count  : size_t;
120       stream : FILEs)
121       return   size_t
122    is
123       Put_Count : size_t := 0;
124       type Buffer_Type is array (size_t range 1 .. count,
125                                  size_t range 1 .. size) of Character;
126       type Buffer_Access is access Buffer_Type;
127       function To_BA is new Unchecked_Conversion (voids, Buffer_Access);
128       BA : Buffer_Access := To_BA (buffer);
129    begin
130
131       --  Fwrite on VMS has the undesirable effect of always generating at
132       --  least one record of output per call, regardless of buffering.  To
133       --  get around this, we do multiple fputc calls instead.
134
135       for C in 1 .. count loop
136          for S in 1 .. size loop
137             if fputc (Character'Pos (BA.all (C, S)), stream) = EOF then
138                return Put_Count;
139             end if;
140          end loop;
141          Put_Count := Put_Count + 1;
142       end loop;
143       return Put_Count;
144    end fwrite;
145
146    -------------
147    -- setvbuf --
148    -------------
149
150    function setvbuf
151      (stream : FILEs;
152       buffer : chars;
153       mode   : int;
154       size   : size_t)
155       return   int
156    is
157       function C_setvbuf
158         (stream : FILEs;
159          buffer : chars;
160          mode   : int;
161          size   : size_t)
162          return   int;
163       pragma Import (C, C_setvbuf, "setvbuf");
164
165       use type System.Address;
166    begin
167
168       --  In order for the above fwrite hack to work, we must always buffer
169       --  stdout and stderr. Is_regular_file on VMS cannot detect when
170       --  these are redirected to a file, so checking for that condition
171       --  doesnt help.
172
173       if mode = IONBF
174         and then (stream = stdout or else stream = stderr)
175       then
176          return C_setvbuf (stream, buffer, IOLBF, size);
177       else
178          return C_setvbuf (stream, buffer, mode, size);
179       end if;
180    end setvbuf;
181
182 end Interfaces.C_Streams;