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-2009, 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 3,  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.                                     --
17--                                                                          --
18-- As a special exception under Section 7 of GPL version 3, you are granted --
19-- additional permissions described in the GCC Runtime Library Exception,   --
20-- version 3.1, as published by the Free Software Foundation.               --
21--                                                                          --
22-- You should have received a copy of the GNU General Public License and    --
23-- a copy of the GCC Runtime Library Exception along with this program;     --
24-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25-- <http://www.gnu.org/licenses/>.                                          --
26--                                                                          --
27-- GNAT was originally developed  by the GNAT team at  New York University. --
28-- Extensive contributions were provided by Ada Core Technologies Inc.      --
29--                                                                          --
30------------------------------------------------------------------------------
31
32--  This is the default version which just calls the C versions directly
33--  Note: the reason that we provide for specialization here is that on
34--  some systems, notably VMS, we may need to worry about buffering.
35
36with Ada.Unchecked_Conversion;
37
38package body Interfaces.C_Streams is
39
40   use type System.CRTL.size_t;
41
42   ----------------------------
43   -- Interfaced C functions --
44   ----------------------------
45
46   function C_fread
47     (buffer : voids;
48      size   : size_t;
49      count  : size_t;
50      stream : FILEs) return size_t;
51   pragma Import (C, C_fread, "fread");
52
53   function C_fwrite
54     (buffer : voids;
55      size   : size_t;
56      count  : size_t;
57      stream : FILEs) return size_t;
58   pragma Import (C, C_fwrite, "fwrite");
59
60   function C_setvbuf
61     (stream : FILEs;
62      buffer : chars;
63      mode   : int;
64      size   : size_t) return int;
65   pragma Import (C, C_setvbuf, "setvbuf");
66
67   ------------
68   -- fread --
69   ------------
70
71   function fread
72     (buffer : voids;
73      size   : size_t;
74      count  : size_t;
75      stream : FILEs) return size_t
76   is
77   begin
78      return C_fread (buffer, size, count, stream);
79   end fread;
80
81   ------------
82   -- fread --
83   ------------
84
85   --  The following declarations should really be nested within fread, but
86   --  limitations in front end inlining make this undesirable right now ???
87
88   type Byte_Buffer is array (0 .. size_t'Last / 2 - 1) of Unsigned_8;
89   --  This should really be 0 .. size_t'last, but there is a problem
90   --  in gigi in handling such types (introduced in GCC 3 Sep 2001)
91   --  since the size in bytes of this array overflows ???
92
93   type Acc_Bytes is access all Byte_Buffer;
94
95   function To_Acc_Bytes is new Ada.Unchecked_Conversion (voids, Acc_Bytes);
96
97   function fread
98     (buffer : voids;
99      index  : size_t;
100      size   : size_t;
101      count  : size_t;
102      stream : FILEs) return size_t
103   is
104   begin
105      return C_fread
106        (To_Acc_Bytes (buffer) (index * size)'Address, size, count, stream);
107   end fread;
108
109   ------------
110   -- fwrite --
111   ------------
112
113   function fwrite
114     (buffer : voids;
115      size   : size_t;
116      count  : size_t;
117      stream : FILEs) return size_t
118   is
119   begin
120      return C_fwrite (buffer, size, count, stream);
121   end fwrite;
122
123   -------------
124   -- setvbuf --
125   -------------
126
127   function setvbuf
128     (stream : FILEs;
129      buffer : chars;
130      mode   : int;
131      size   : size_t) return int
132   is
133   begin
134      return C_setvbuf (stream, buffer, mode, size);
135   end setvbuf;
136
137end Interfaces.C_Streams;
138