Merge from mainline
[official-gcc.git] / gcc / ada / a-direio.adb
blob5674339e156e9abd3bb469960c8c8cbc31726138
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME 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-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 ------------------------------------------------------------------------------
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)
39 with Interfaces.C_Streams; use Interfaces.C_Streams;
40 with System; use System;
41 with System.CRTL;
42 with System.File_Control_Block;
43 with System.File_IO;
44 with System.Direct_IO;
45 with System.Storage_Elements;
46 with Unchecked_Conversion;
48 use type System.Direct_IO.Count;
50 package body Ada.Direct_IO is
52 Zeroes : constant System.Storage_Elements.Storage_Array :=
53 (1 .. System.Storage_Elements.Storage_Offset (Bytes) => 0);
54 -- Buffer used to fill out partial records
56 package FCB renames System.File_Control_Block;
57 package FIO renames System.File_IO;
58 package DIO renames System.Direct_IO;
60 SU : constant := System.Storage_Unit;
62 subtype AP is FCB.AFCB_Ptr;
63 subtype FP is DIO.File_Type;
64 subtype DPCount is DIO.Positive_Count;
66 function To_FCB is new Unchecked_Conversion (File_Mode, FCB.File_Mode);
67 function To_DIO is new Unchecked_Conversion (FCB.File_Mode, File_Mode);
69 use type System.CRTL.size_t;
71 -----------
72 -- Close --
73 -----------
75 procedure Close (File : in out File_Type) is
76 begin
77 FIO.Close (AP (File));
78 end Close;
80 ------------
81 -- Create --
82 ------------
84 procedure Create
85 (File : in out File_Type;
86 Mode : File_Mode := Inout_File;
87 Name : String := "";
88 Form : String := "")
90 begin
91 DIO.Create (FP (File), To_FCB (Mode), Name, Form);
92 File.Bytes := Bytes;
93 end Create;
95 ------------
96 -- Delete --
97 ------------
99 procedure Delete (File : in out File_Type) is
100 begin
101 FIO.Delete (AP (File));
102 end Delete;
104 -----------------
105 -- End_Of_File --
106 -----------------
108 function End_Of_File (File : File_Type) return Boolean is
109 begin
110 return DIO.End_Of_File (FP (File));
111 end End_Of_File;
113 ----------
114 -- Form --
115 ----------
117 function Form (File : File_Type) return String is
118 begin
119 return FIO.Form (AP (File));
120 end Form;
122 -----------
123 -- Index --
124 -----------
126 function Index (File : File_Type) return Positive_Count is
127 begin
128 return Positive_Count (DIO.Index (FP (File)));
129 end Index;
131 -------------
132 -- Is_Open --
133 -------------
135 function Is_Open (File : File_Type) return Boolean is
136 begin
137 return FIO.Is_Open (AP (File));
138 end Is_Open;
140 ----------
141 -- Mode --
142 ----------
144 function Mode (File : File_Type) return File_Mode is
145 begin
146 return To_DIO (FIO.Mode (AP (File)));
147 end Mode;
149 ----------
150 -- Name --
151 ----------
153 function Name (File : File_Type) return String is
154 begin
155 return FIO.Name (AP (File));
156 end Name;
158 ----------
159 -- Open --
160 ----------
162 procedure Open
163 (File : in out File_Type;
164 Mode : File_Mode;
165 Name : String;
166 Form : String := "")
168 begin
169 DIO.Open (FP (File), To_FCB (Mode), Name, Form);
170 File.Bytes := Bytes;
171 end Open;
173 ----------
174 -- Read --
175 ----------
177 procedure Read
178 (File : File_Type;
179 Item : out Element_Type;
180 From : Positive_Count)
182 begin
183 -- For a non-constrained variant record type, we read into an
184 -- intermediate buffer, since we may have the case of discriminated
185 -- records where a discriminant check is required, and we may need
186 -- to assign only part of the record buffer originally written.
188 -- Note: we have to turn warnings on/off because this use of
189 -- the Constrained attribute is an obsolescent feature.
191 pragma Warnings (Off);
192 if not Element_Type'Constrained then
193 pragma Warnings (On);
195 declare
196 Buf : Element_Type;
198 begin
199 DIO.Read (FP (File), Buf'Address, Bytes, DPCount (From));
200 Item := Buf;
201 end;
203 -- In the normal case, we can read straight into the buffer
205 else
206 DIO.Read (FP (File), Item'Address, Bytes, DPCount (From));
207 end if;
208 end Read;
210 procedure Read (File : File_Type; Item : out Element_Type) is
211 begin
212 -- Same processing for unconstrained case as above
214 -- Note: we have to turn warnings on/off because this use of
215 -- the Constrained attribute is an obsolescent feature.
217 pragma Warnings (Off);
218 if not Element_Type'Constrained then
219 pragma Warnings (On);
221 declare
222 Buf : Element_Type;
224 begin
225 DIO.Read (FP (File), Buf'Address, Bytes);
226 Item := Buf;
227 end;
229 else
230 DIO.Read (FP (File), Item'Address, Bytes);
231 end if;
232 end Read;
234 -----------
235 -- Reset --
236 -----------
238 procedure Reset (File : in out File_Type; Mode : File_Mode) is
239 begin
240 DIO.Reset (FP (File), To_FCB (Mode));
241 end Reset;
243 procedure Reset (File : in out File_Type) is
244 begin
245 DIO.Reset (FP (File));
246 end Reset;
248 ---------------
249 -- Set_Index --
250 ---------------
252 procedure Set_Index (File : File_Type; To : Positive_Count) is
253 begin
254 DIO.Set_Index (FP (File), DPCount (To));
255 end Set_Index;
257 ----------
258 -- Size --
259 ----------
261 function Size (File : File_Type) return Count is
262 begin
263 return Count (DIO.Size (FP (File)));
264 end Size;
266 -----------
267 -- Write --
268 -----------
270 procedure Write
271 (File : File_Type;
272 Item : Element_Type;
273 To : Positive_Count)
275 begin
276 DIO.Set_Index (FP (File), DPCount (To));
277 DIO.Write (FP (File), Item'Address, Item'Size / SU, Zeroes);
278 end Write;
280 procedure Write (File : File_Type; Item : Element_Type) is
281 begin
282 DIO.Write (FP (File), Item'Address, Item'Size / SU, Zeroes);
283 end Write;
285 end Ada.Direct_IO;