1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S E Q U E N T I A L _ I O --
11 -- Copyright (C) 1992-1999, Free Software Foundation, Inc. --
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. --
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. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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
;
43 with System
.File_Control_Block
;
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
);
67 procedure Close
(File
: in out File_Type
) is
69 FIO
.Close
(AP
(File
));
77 (File
: in out File_Type
;
78 Mode
: in File_Mode
:= Out_File
;
79 Name
: in String := "";
80 Form
: in String := "")
83 SIO
.Create
(FP
(File
), To_FCB
(Mode
), Name
, Form
);
90 procedure Delete
(File
: in out File_Type
) is
92 FIO
.Delete
(AP
(File
));
99 function End_Of_File
(File
: in File_Type
) return Boolean is
101 return FIO
.End_Of_File
(AP
(File
));
108 function Form
(File
: in File_Type
) return String is
110 return FIO
.Form
(AP
(File
));
117 function Is_Open
(File
: in File_Type
) return Boolean is
119 return FIO
.Is_Open
(AP
(File
));
126 function Mode
(File
: in File_Type
) return File_Mode
is
128 return To_SIO
(FIO
.Mode
(AP
(File
)));
135 function Name
(File
: in File_Type
) return String is
137 return FIO
.Name
(AP
(File
));
145 (File
: in out File_Type
;
148 Form
: in String := "")
151 SIO
.Open
(FP
(File
), To_FCB
(Mode
), Name
, Form
);
158 procedure Read
(File
: in File_Type
; Item
: out Element_Type
) is
159 Siz
: constant size_t
:= (Item
'Size + SU
- 1) / SU
;
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
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
178 pragma Extensions_Allowed
(On
);
179 -- Needed to allow Constrained reference here
181 if Element_Type
'Has_Discriminants
182 and then Item
'Constrained
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
);
204 pragma Unsuppress
(Discriminant_Check
);
207 FIO
.Read_Buf
(AP
(File
), Buffer
'Address, Rsiz
);
208 Item
:= To_ItemP
(Buffer
'Access).all;
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
221 FIO
.Read_Buf
(AP
(File
), Item
'Address, Rsiz
);
223 -- For definite type without discriminants, use actual size of item
226 FIO
.Read_Buf
(AP
(File
), Item
'Address, Siz
);
234 procedure Reset
(File
: in out File_Type
; Mode
: in File_Mode
) is
236 FIO
.Reset
(AP
(File
), To_FCB
(Mode
));
239 procedure Reset
(File
: in out File_Type
) is
241 FIO
.Reset
(AP
(File
));
248 procedure Write
(File
: in File_Type
; Item
: in Element_Type
) is
249 Siz
: constant size_t
:= (Item
'Size + SU
- 1) / SU
;
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
260 (AP
(File
), Siz
'Address, size_t
'Size / System
.Storage_Unit
);
263 FIO
.Write_Buf
(AP
(File
), Item
'Address, Siz
);
266 end Ada
.Sequential_IO
;