OSDN Git Service

2003-12-11 Ed Falis <falis@gnat.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-direio.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUNTIME COMPONENTS                          --
4 --                                                                          --
5 --                        A D A . D I R E C T _ I O                         --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2003 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 generic template for Direct_IO, i.e. the code that gets
35 --  duplicated. We absolutely minimize this code by either calling routines
36 --  in System.File_IO (for common file functions), or in System.Direct_IO
37 --  (for specialized Direct_IO functions)
38
39 with Interfaces.C_Streams; use Interfaces.C_Streams;
40 with System;               use System;
41 with System.File_Control_Block;
42 with System.File_IO;
43 with System.Direct_IO;
44 with System.Storage_Elements;
45 with Unchecked_Conversion;
46
47 use type System.Direct_IO.Count;
48
49 package body Ada.Direct_IO is
50
51    Zeroes : constant System.Storage_Elements.Storage_Array :=
52               (1 .. System.Storage_Elements.Storage_Offset (Bytes) => 0);
53    --  Buffer used to fill out partial records.
54
55    package FCB renames System.File_Control_Block;
56    package FIO renames System.File_IO;
57    package DIO renames System.Direct_IO;
58
59    SU : constant := System.Storage_Unit;
60
61    subtype AP      is FCB.AFCB_Ptr;
62    subtype FP      is DIO.File_Type;
63    subtype DPCount is DIO.Positive_Count;
64
65    function To_FCB is new Unchecked_Conversion (File_Mode, FCB.File_Mode);
66    function To_DIO is new Unchecked_Conversion (FCB.File_Mode, File_Mode);
67
68    -----------
69    -- Close --
70    -----------
71
72    procedure Close (File : in out File_Type) is
73    begin
74       FIO.Close (AP (File));
75    end Close;
76
77    ------------
78    -- Create --
79    ------------
80
81    procedure Create
82      (File : in out File_Type;
83       Mode : in File_Mode := Inout_File;
84       Name : in String := "";
85       Form : in String := "")
86    is
87    begin
88       DIO.Create (FP (File), To_FCB (Mode), Name, Form);
89       File.Bytes := Bytes;
90    end Create;
91
92    ------------
93    -- Delete --
94    ------------
95
96    procedure Delete (File : in out File_Type) is
97    begin
98       FIO.Delete (AP (File));
99    end Delete;
100
101    -----------------
102    -- End_Of_File --
103    -----------------
104
105    function End_Of_File (File : in File_Type) return Boolean is
106    begin
107       return DIO.End_Of_File (FP (File));
108    end End_Of_File;
109
110    ----------
111    -- Form --
112    ----------
113
114    function Form (File : in File_Type) return String is
115    begin
116       return FIO.Form (AP (File));
117    end Form;
118
119    -----------
120    -- Index --
121    -----------
122
123    function Index (File : in File_Type) return Positive_Count is
124    begin
125       return Positive_Count (DIO.Index (FP (File)));
126    end Index;
127
128    -------------
129    -- Is_Open --
130    -------------
131
132    function Is_Open (File : in File_Type) return Boolean is
133    begin
134       return FIO.Is_Open (AP (File));
135    end Is_Open;
136
137    ----------
138    -- Mode --
139    ----------
140
141    function Mode (File : in File_Type) return File_Mode is
142    begin
143       return To_DIO (FIO.Mode (AP (File)));
144    end Mode;
145
146    ----------
147    -- Name --
148    ----------
149
150    function Name (File : in File_Type) return String is
151    begin
152       return FIO.Name (AP (File));
153    end Name;
154
155    ----------
156    -- Open --
157    ----------
158
159    procedure Open
160      (File : in out File_Type;
161       Mode : in File_Mode;
162       Name : in String;
163       Form : in String := "")
164    is
165    begin
166       DIO.Open (FP (File), To_FCB (Mode), Name, Form);
167       File.Bytes := Bytes;
168    end Open;
169
170    ----------
171    -- Read --
172    ----------
173
174    procedure Read
175      (File : in File_Type;
176       Item : out Element_Type;
177       From : in Positive_Count)
178    is
179    begin
180       --  For a non-constrained variant record type, we read into an
181       --  intermediate buffer, since we may have the case of discriminated
182       --  records where a discriminant check is required, and we may need
183       --  to assign only part of the record buffer originally written
184
185       if not Element_Type'Constrained then
186          declare
187             Buf : Element_Type;
188
189          begin
190             DIO.Read (FP (File), Buf'Address, Bytes, DPCount (From));
191             Item := Buf;
192          end;
193
194       --  In the normal case, we can read straight into the buffer
195
196       else
197          DIO.Read (FP (File), Item'Address, Bytes, DPCount (From));
198       end if;
199    end Read;
200
201    procedure Read (File : in File_Type; Item : out Element_Type) is
202    begin
203       --  Same processing for unconstrained case as above
204
205       if not Element_Type'Constrained then
206          declare
207             Buf : Element_Type;
208
209          begin
210             DIO.Read (FP (File), Buf'Address, Bytes);
211             Item := Buf;
212          end;
213
214       else
215          DIO.Read (FP (File), Item'Address, Bytes);
216       end if;
217    end Read;
218
219    -----------
220    -- Reset --
221    -----------
222
223    procedure Reset (File : in out File_Type; Mode : in File_Mode) is
224    begin
225       DIO.Reset (FP (File), To_FCB (Mode));
226    end Reset;
227
228    procedure Reset (File : in out File_Type) is
229    begin
230       DIO.Reset (FP (File));
231    end Reset;
232
233    ---------------
234    -- Set_Index --
235    ---------------
236
237    procedure Set_Index (File : in File_Type; To : in Positive_Count) is
238    begin
239       DIO.Set_Index (FP (File), DPCount (To));
240    end Set_Index;
241
242    ----------
243    -- Size --
244    ----------
245
246    function Size (File : in File_Type) return Count is
247    begin
248       return Count (DIO.Size (FP (File)));
249    end Size;
250
251    -----------
252    -- Write --
253    -----------
254
255    procedure Write
256      (File : in File_Type;
257       Item : in Element_Type;
258       To   : in Positive_Count)
259    is
260    begin
261       DIO.Set_Index (FP (File), DPCount (To));
262       DIO.Write (FP (File), Item'Address, Item'Size / SU, Zeroes);
263    end Write;
264
265    procedure Write (File : in File_Type; Item : in Element_Type) is
266    begin
267       DIO.Write (FP (File), Item'Address, Item'Size / SU, Zeroes);
268    end Write;
269
270 end Ada.Direct_IO;