1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- S Y S T E M . D I R E C T _ I O --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 with Ada
.IO_Exceptions
; use Ada
.IO_Exceptions
;
36 with Interfaces
.C_Streams
; use Interfaces
.C_Streams
;
37 with System
; use System
;
39 with System
.Soft_Links
;
40 with Unchecked_Deallocation
;
42 package body System
.Direct_IO
is
44 package FIO
renames System
.File_IO
;
45 package SSL
renames System
.Soft_Links
;
47 subtype AP
is FCB
.AFCB_Ptr
;
48 use type FCB
.Shared_Status_Type
;
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
54 procedure Set_Position
(File
: in File_Type
);
55 -- Sets file position pointer according to value of current index
61 function AFCB_Allocate
(Control_Block
: Direct_AFCB
) return FCB
.AFCB_Ptr
is
62 pragma Warnings
(Off
, Control_Block
);
65 return new Direct_AFCB
;
72 -- No special processing required for Direct_IO close
74 procedure AFCB_Close
(File
: access Direct_AFCB
) is
75 pragma Warnings
(Off
, File
);
85 procedure AFCB_Free
(File
: access Direct_AFCB
) is
87 type FCB_Ptr
is access all Direct_AFCB
;
89 FT
: FCB_Ptr
:= FCB_Ptr
(File
);
92 Unchecked_Deallocation
(Direct_AFCB
, FCB_Ptr
);
103 (File
: in out File_Type
;
104 Mode
: in FCB
.File_Mode
:= FCB
.Inout_File
;
105 Name
: in String := "";
106 Form
: in String := "")
108 File_Control_Block
: Direct_AFCB
;
111 FIO
.Open
(File_Ptr
=> AP
(File
),
112 Dummy_FCB
=> File_Control_Block
,
125 function End_Of_File
(File
: in File_Type
) return Boolean is
127 FIO
.Check_Read_Status
(AP
(File
));
128 return Count
(File
.Index
) > Size
(File
);
135 function Index
(File
: in File_Type
) return Positive_Count
is
137 FIO
.Check_File_Open
(AP
(File
));
138 return Count
(File
.Index
);
146 (File
: in out File_Type
;
147 Mode
: in FCB
.File_Mode
;
149 Form
: in String := "")
151 File_Control_Block
: Direct_AFCB
;
154 FIO
.Open
(File_Ptr
=> AP
(File
),
155 Dummy_FCB
=> File_Control_Block
,
169 (File
: in File_Type
;
171 Size
: in Interfaces
.C_Streams
.size_t
;
172 From
: in Positive_Count
)
175 Set_Index
(File
, From
);
176 Read
(File
, Item
, Size
);
180 (File
: in File_Type
;
182 Size
: in Interfaces
.C_Streams
.size_t
)
185 FIO
.Check_Read_Status
(AP
(File
));
187 -- If last operation was not a read, or if in file sharing mode,
188 -- then reset the physical pointer of the file to match the index
189 -- We lock out task access over the two operations in this case.
191 if File
.Last_Op
/= Op_Read
192 or else File
.Shared_Status
= FCB
.Yes
194 if End_Of_File
(File
) then
198 Locked_Processing
: begin
201 FIO
.Read_Buf
(AP
(File
), Item
, Size
);
208 end Locked_Processing
;
211 FIO
.Read_Buf
(AP
(File
), Item
, Size
);
214 File
.Index
:= File
.Index
+ 1;
216 -- Set last operation to read, unless we did not read a full record
217 -- (happens with the variant record case) in which case we set the
218 -- last operation as other, to force the file position to be reset
221 if File
.Bytes
= Size
then
222 File
.Last_Op
:= Op_Read
;
224 File
.Last_Op
:= Op_Other
;
228 -- The following is the required overriding for Stream.Read, which is
229 -- not used, since we do not do Stream operations on Direct_IO files.
232 (File
: in out Direct_AFCB
;
233 Item
: out Ada
.Streams
.Stream_Element_Array
;
234 Last
: out Ada
.Streams
.Stream_Element_Offset
)
244 procedure Reset
(File
: in out File_Type
; Mode
: in FCB
.File_Mode
) is
246 FIO
.Reset
(AP
(File
), Mode
);
248 File
.Last_Op
:= Op_Read
;
251 procedure Reset
(File
: in out File_Type
) is
253 FIO
.Reset
(AP
(File
));
255 File
.Last_Op
:= Op_Read
;
262 procedure Set_Index
(File
: in File_Type
; To
: in Positive_Count
) is
264 FIO
.Check_File_Open
(AP
(File
));
265 File
.Index
:= Count
(To
);
266 File
.Last_Op
:= Op_Other
;
273 procedure Set_Position
(File
: in File_Type
) is
276 (File
.Stream
, long
(File
.Bytes
) *
277 long
(File
.Index
- 1), SEEK_SET
) /= 0
287 function Size
(File
: in File_Type
) return Count
is
289 FIO
.Check_File_Open
(AP
(File
));
290 File
.Last_Op
:= Op_Other
;
292 if fseek
(File
.Stream
, 0, SEEK_END
) /= 0 then
296 return Count
(ftell
(File
.Stream
) / long
(File
.Bytes
));
306 Size
: in Interfaces
.C_Streams
.size_t
;
307 Zeroes
: System
.Storage_Elements
.Storage_Array
)
311 -- Do the actual write
313 procedure Do_Write
is
315 FIO
.Write_Buf
(AP
(File
), Item
, Size
);
317 -- If we did not write the whole record (happens with the variant
318 -- record case), then fill out the rest of the record with zeroes.
319 -- This is cleaner in any case, and is required for the last
320 -- record, since otherwise the length of the file is wrong.
322 if File
.Bytes
> Size
then
323 FIO
.Write_Buf
(AP
(File
), Zeroes
'Address, File
.Bytes
- Size
);
327 -- Start of processing for Write
330 FIO
.Check_Write_Status
(AP
(File
));
332 -- If last operation was not a write, or if in file sharing mode,
333 -- then reset the physical pointer of the file to match the index
334 -- We lock out task access over the two operations in this case.
336 if File
.Last_Op
/= Op_Write
337 or else File
.Shared_Status
= FCB
.Yes
339 Locked_Processing
: begin
349 end Locked_Processing
;
355 File
.Index
:= File
.Index
+ 1;
357 -- Set last operation to write, unless we did not read a full record
358 -- (happens with the variant record case) in which case we set the
359 -- last operation as other, to force the file position to be reset
360 -- on the next write.
362 if File
.Bytes
= Size
then
363 File
.Last_Op
:= Op_Write
;
365 File
.Last_Op
:= Op_Other
;
369 -- The following is the required overriding for Stream.Write, which is
370 -- not used, since we do not do Stream operations on Direct_IO files.
373 (File
: in out Direct_AFCB
;
374 Item
: in Ada
.Streams
.Stream_Element_Array
)
380 end System
.Direct_IO
;