Daily bump.
[official-gcc.git] / gcc / ada / a-sequio.adb
blob32add77d3698df5e9f8cac239a870759dc18c62f
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUNTIME COMPONENTS --
4 -- --
5 -- A D A . S E Q U E N T I A L _ I O --
6 -- --
7 -- B o d y --
8 -- --
9 -- $Revision: 1.1 $
10 -- --
11 -- Copyright (C) 1992-1999, Free Software Foundation, Inc. --
12 -- --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
30 -- --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 -- --
34 ------------------------------------------------------------------------------
36 -- This is the generic template for Sequential_IO, i.e. the code that gets
37 -- duplicated. We absolutely minimize this code by either calling routines
38 -- in System.File_IO (for common file functions), or in System.Sequential_IO
39 -- (for specialized Sequential_IO functions)
41 with Interfaces.C_Streams; use Interfaces.C_Streams;
42 with System;
43 with System.File_Control_Block;
44 with System.File_IO;
45 with System.Storage_Elements;
46 with Unchecked_Conversion;
48 package body Ada.Sequential_IO is
50 package FIO renames System.File_IO;
51 package FCB renames System.File_Control_Block;
52 package SIO renames System.Sequential_IO;
53 package SSE renames System.Storage_Elements;
55 SU : constant := System.Storage_Unit;
57 subtype AP is FCB.AFCB_Ptr;
58 subtype FP is SIO.File_Type;
60 function To_FCB is new Unchecked_Conversion (File_Mode, FCB.File_Mode);
61 function To_SIO is new Unchecked_Conversion (FCB.File_Mode, File_Mode);
63 -----------
64 -- Close --
65 -----------
67 procedure Close (File : in out File_Type) is
68 begin
69 FIO.Close (AP (File));
70 end Close;
72 ------------
73 -- Create --
74 ------------
76 procedure Create
77 (File : in out File_Type;
78 Mode : in File_Mode := Out_File;
79 Name : in String := "";
80 Form : in String := "")
82 begin
83 SIO.Create (FP (File), To_FCB (Mode), Name, Form);
84 end Create;
86 ------------
87 -- Delete --
88 ------------
90 procedure Delete (File : in out File_Type) is
91 begin
92 FIO.Delete (AP (File));
93 end Delete;
95 -----------------
96 -- End_Of_File --
97 -----------------
99 function End_Of_File (File : in File_Type) return Boolean is
100 begin
101 return FIO.End_Of_File (AP (File));
102 end End_Of_File;
104 ----------
105 -- Form --
106 ----------
108 function Form (File : in File_Type) return String is
109 begin
110 return FIO.Form (AP (File));
111 end Form;
113 -------------
114 -- Is_Open --
115 -------------
117 function Is_Open (File : in File_Type) return Boolean is
118 begin
119 return FIO.Is_Open (AP (File));
120 end Is_Open;
122 ----------
123 -- Mode --
124 ----------
126 function Mode (File : in File_Type) return File_Mode is
127 begin
128 return To_SIO (FIO.Mode (AP (File)));
129 end Mode;
131 ----------
132 -- Name --
133 ----------
135 function Name (File : in File_Type) return String is
136 begin
137 return FIO.Name (AP (File));
138 end Name;
140 ----------
141 -- Open --
142 ----------
144 procedure Open
145 (File : in out File_Type;
146 Mode : in File_Mode;
147 Name : in String;
148 Form : in String := "")
150 begin
151 SIO.Open (FP (File), To_FCB (Mode), Name, Form);
152 end Open;
154 ----------
155 -- Read --
156 ----------
158 procedure Read (File : in File_Type; Item : out Element_Type) is
159 Siz : constant size_t := (Item'Size + SU - 1) / SU;
160 Rsiz : size_t;
162 begin
163 FIO.Check_Read_Status (AP (File));
165 -- For non-definite type or type with discriminants, read size and
166 -- raise Program_Error if it is larger than the size of the item.
168 if not Element_Type'Definite
169 or else Element_Type'Has_Discriminants
170 then
171 FIO.Read_Buf
172 (AP (File), Rsiz'Address, size_t'Size / System.Storage_Unit);
174 -- For a type with discriminants, we have to read into a temporary
175 -- buffer if Item is constrained, to check that the discriminants
176 -- are correct.
178 pragma Extensions_Allowed (On);
179 -- Needed to allow Constrained reference here
181 if Element_Type'Has_Discriminants
182 and then Item'Constrained
183 then
184 declare
185 RsizS : constant SSE.Storage_Offset :=
186 SSE.Storage_Offset (Rsiz - 1);
188 subtype SA is SSE.Storage_Array (0 .. RsizS);
189 type SAP is access all SA;
190 type ItemP is access all Element_Type;
192 pragma Warnings (Off);
193 -- We have to turn warnings off for this function, because
194 -- it gets analyzed for all types, including ones which
195 -- can't possibly come this way, and for which the size
196 -- of the access types differs.
198 function To_ItemP is new Unchecked_Conversion (SAP, ItemP);
200 pragma Warnings (On);
202 Buffer : aliased SA;
204 pragma Unsuppress (Discriminant_Check);
206 begin
207 FIO.Read_Buf (AP (File), Buffer'Address, Rsiz);
208 Item := To_ItemP (Buffer'Access).all;
209 return;
210 end;
211 end if;
213 -- In the case of a non-definite type, make sure the length is OK.
214 -- We can't do this in the variant record case, because the size is
215 -- based on the current discriminant, so may be apparently wrong.
217 if not Element_Type'Has_Discriminants and then Rsiz > Siz then
218 raise Program_Error;
219 end if;
221 FIO.Read_Buf (AP (File), Item'Address, Rsiz);
223 -- For definite type without discriminants, use actual size of item
225 else
226 FIO.Read_Buf (AP (File), Item'Address, Siz);
227 end if;
228 end Read;
230 -----------
231 -- Reset --
232 -----------
234 procedure Reset (File : in out File_Type; Mode : in File_Mode) is
235 begin
236 FIO.Reset (AP (File), To_FCB (Mode));
237 end Reset;
239 procedure Reset (File : in out File_Type) is
240 begin
241 FIO.Reset (AP (File));
242 end Reset;
244 -----------
245 -- Write --
246 -----------
248 procedure Write (File : in File_Type; Item : in Element_Type) is
249 Siz : constant size_t := (Item'Size + SU - 1) / SU;
251 begin
252 FIO.Check_Write_Status (AP (File));
254 -- For non-definite types or types with discriminants, write the size
256 if not Element_Type'Definite
257 or else Element_Type'Has_Discriminants
258 then
259 FIO.Write_Buf
260 (AP (File), Siz'Address, size_t'Size / System.Storage_Unit);
261 end if;
263 FIO.Write_Buf (AP (File), Item'Address, Siz);
264 end Write;
266 end Ada.Sequential_IO;