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-2014, 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
32with Ada.Unchecked_Conversion;
33
34package body Interfaces.C_Streams is
35
36   use type System.CRTL.size_t;
37
38   ----------------------------
39   -- Interfaced C functions --
40   ----------------------------
41
42   function C_fread
43     (buffer : voids;
44      size   : size_t;
45      count  : size_t;
46      stream : FILEs) return size_t;
47   pragma Import (C, C_fread, "fread");
48
49   function C_fwrite
50     (buffer : voids;
51      size   : size_t;
52      count  : size_t;
53      stream : FILEs) return size_t;
54   pragma Import (C, C_fwrite, "fwrite");
55
56   function C_setvbuf
57     (stream : FILEs;
58      buffer : chars;
59      mode   : int;
60      size   : size_t) return int;
61   pragma Import (C, C_setvbuf, "setvbuf");
62
63   ------------
64   -- fread --
65   ------------
66
67   function fread
68     (buffer : voids;
69      size   : size_t;
70      count  : size_t;
71      stream : FILEs) return size_t
72   is
73   begin
74      return C_fread (buffer, size, count, stream);
75   end fread;
76
77   ------------
78   -- fread --
79   ------------
80
81   --  The following declarations should really be nested within fread, but
82   --  limitations in front end inlining make this undesirable right now ???
83
84   type Byte_Buffer is array (0 .. size_t'Last / 2 - 1) of Unsigned_8;
85   --  This should really be 0 .. size_t'last, but there is a problem
86   --  in gigi in handling such types (introduced in GCC 3 Sep 2001)
87   --  since the size in bytes of this array overflows ???
88
89   type Acc_Bytes is access all Byte_Buffer;
90
91   function To_Acc_Bytes is new Ada.Unchecked_Conversion (voids, Acc_Bytes);
92
93   function fread
94     (buffer : voids;
95      index  : size_t;
96      size   : size_t;
97      count  : size_t;
98      stream : FILEs) return size_t
99   is
100   begin
101      return C_fread
102        (To_Acc_Bytes (buffer) (index * size)'Address, size, count, stream);
103   end fread;
104
105   ------------
106   -- fwrite --
107   ------------
108
109   function fwrite
110     (buffer : voids;
111      size   : size_t;
112      count  : size_t;
113      stream : FILEs) return size_t
114   is
115   begin
116      return C_fwrite (buffer, size, count, stream);
117   end fwrite;
118
119   -------------
120   -- setvbuf --
121   -------------
122
123   function setvbuf
124     (stream : FILEs;
125      buffer : chars;
126      mode   : int;
127      size   : size_t) return int
128   is
129   begin
130      return C_setvbuf (stream, buffer, mode, size);
131   end setvbuf;
132
133end Interfaces.C_Streams;
134