69476696a6c1bf58b5a6d9174a1f5aaa34ef69e4
[gcc.git] / gcc / ada / a-direio.adb
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-2008, 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 ------------------------------------------------------------------------------
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.CRTL;
42 with System.File_Control_Block;
43 with System.File_IO;
44 with System.Direct_IO;
45 with System.Storage_Elements;
46 with Ada.Unchecked_Conversion;
47
48 use type System.Direct_IO.Count;
49
50 package body Ada.Direct_IO is
51
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
55
56 package FCB renames System.File_Control_Block;
57 package FIO renames System.File_IO;
58 package DIO renames System.Direct_IO;
59
60 SU : constant := System.Storage_Unit;
61
62 subtype AP is FCB.AFCB_Ptr;
63 subtype FP is DIO.File_Type;
64 subtype DPCount is DIO.Positive_Count;
65
66 function To_FCB is new Ada.Unchecked_Conversion (File_Mode, FCB.File_Mode);
67 function To_DIO is new Ada.Unchecked_Conversion (FCB.File_Mode, File_Mode);
68
69 use type System.CRTL.size_t;
70
71 -----------
72 -- Close --
73 -----------
74
75 procedure Close (File : in out File_Type) is
76 AFCB : aliased AP;
77 for AFCB'Address use File'Address;
78 pragma Import (Ada, AFCB);
79 begin
80 FIO.Close (AFCB'Access);
81 end Close;
82
83 ------------
84 -- Create --
85 ------------
86
87 procedure Create
88 (File : in out File_Type;
89 Mode : File_Mode := Inout_File;
90 Name : String := "";
91 Form : String := "")
92 is
93 begin
94 DIO.Create (FP (File), To_FCB (Mode), Name, Form);
95 File.Bytes := Bytes;
96 end Create;
97
98 ------------
99 -- Delete --
100 ------------
101
102 procedure Delete (File : in out File_Type) is
103 AFCB : aliased AP;
104 for AFCB'Address use File'Address;
105 pragma Import (Ada, AFCB);
106 begin
107 FIO.Delete (AFCB'Access);
108 end Delete;
109
110 -----------------
111 -- End_Of_File --
112 -----------------
113
114 function End_Of_File (File : File_Type) return Boolean is
115 begin
116 return DIO.End_Of_File (FP (File));
117 end End_Of_File;
118
119 ----------
120 -- Form --
121 ----------
122
123 function Form (File : File_Type) return String is
124 begin
125 return FIO.Form (AP (File));
126 end Form;
127
128 -----------
129 -- Index --
130 -----------
131
132 function Index (File : File_Type) return Positive_Count is
133 begin
134 return Positive_Count (DIO.Index (FP (File)));
135 end Index;
136
137 -------------
138 -- Is_Open --
139 -------------
140
141 function Is_Open (File : File_Type) return Boolean is
142 begin
143 return FIO.Is_Open (AP (File));
144 end Is_Open;
145
146 ----------
147 -- Mode --
148 ----------
149
150 function Mode (File : File_Type) return File_Mode is
151 begin
152 return To_DIO (FIO.Mode (AP (File)));
153 end Mode;
154
155 ----------
156 -- Name --
157 ----------
158
159 function Name (File : File_Type) return String is
160 begin
161 return FIO.Name (AP (File));
162 end Name;
163
164 ----------
165 -- Open --
166 ----------
167
168 procedure Open
169 (File : in out File_Type;
170 Mode : File_Mode;
171 Name : String;
172 Form : String := "")
173 is
174 begin
175 DIO.Open (FP (File), To_FCB (Mode), Name, Form);
176 File.Bytes := Bytes;
177 end Open;
178
179 ----------
180 -- Read --
181 ----------
182
183 procedure Read
184 (File : File_Type;
185 Item : out Element_Type;
186 From : Positive_Count)
187 is
188 begin
189 -- For a non-constrained variant record type, we read into an
190 -- intermediate buffer, since we may have the case of discriminated
191 -- records where a discriminant check is required, and we may need
192 -- to assign only part of the record buffer originally written.
193
194 -- Note: we have to turn warnings on/off because this use of
195 -- the Constrained attribute is an obsolescent feature.
196
197 pragma Warnings (Off);
198 if not Element_Type'Constrained then
199 pragma Warnings (On);
200
201 declare
202 Buf : Element_Type;
203
204 begin
205 DIO.Read (FP (File), Buf'Address, Bytes, DPCount (From));
206 Item := Buf;
207 end;
208
209 -- In the normal case, we can read straight into the buffer
210
211 else
212 DIO.Read (FP (File), Item'Address, Bytes, DPCount (From));
213 end if;
214 end Read;
215
216 procedure Read (File : File_Type; Item : out Element_Type) is
217 begin
218 -- Same processing for unconstrained case as above
219
220 -- Note: we have to turn warnings on/off because this use of
221 -- the Constrained attribute is an obsolescent feature.
222
223 pragma Warnings (Off);
224 if not Element_Type'Constrained then
225 pragma Warnings (On);
226
227 declare
228 Buf : Element_Type;
229
230 begin
231 DIO.Read (FP (File), Buf'Address, Bytes);
232 Item := Buf;
233 end;
234
235 else
236 DIO.Read (FP (File), Item'Address, Bytes);
237 end if;
238 end Read;
239
240 -----------
241 -- Reset --
242 -----------
243
244 procedure Reset (File : in out File_Type; Mode : File_Mode) is
245 begin
246 DIO.Reset (FP (File), To_FCB (Mode));
247 end Reset;
248
249 procedure Reset (File : in out File_Type) is
250 begin
251 DIO.Reset (FP (File));
252 end Reset;
253
254 ---------------
255 -- Set_Index --
256 ---------------
257
258 procedure Set_Index (File : File_Type; To : Positive_Count) is
259 begin
260 DIO.Set_Index (FP (File), DPCount (To));
261 end Set_Index;
262
263 ----------
264 -- Size --
265 ----------
266
267 function Size (File : File_Type) return Count is
268 begin
269 return Count (DIO.Size (FP (File)));
270 end Size;
271
272 -----------
273 -- Write --
274 -----------
275
276 procedure Write
277 (File : File_Type;
278 Item : Element_Type;
279 To : Positive_Count)
280 is
281 begin
282 DIO.Set_Index (FP (File), DPCount (To));
283 DIO.Write (FP (File), Item'Address, Item'Size / SU, Zeroes);
284 end Write;
285
286 procedure Write (File : File_Type; Item : Element_Type) is
287 begin
288 DIO.Write (FP (File), Item'Address, Item'Size / SU, Zeroes);
289 end Write;
290
291 end Ada.Direct_IO;