1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- S Y S T E M . D I R E C T _ I O --
11 -- Copyright (C) 1992-2001 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 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 with Ada.IO_Exceptions; use Ada.IO_Exceptions;
37 with Interfaces.C_Streams; use Interfaces.C_Streams;
38 with System; use System;
40 with System.Soft_Links;
41 with Unchecked_Deallocation;
43 package body System.Direct_IO is
45 package FIO renames System.File_IO;
46 package SSL renames System.Soft_Links;
48 subtype AP is FCB.AFCB_Ptr;
49 use type FCB.Shared_Status_Type;
51 -----------------------
52 -- Local Subprograms --
53 -----------------------
55 procedure Set_Position (File : in File_Type);
56 -- Sets file position pointer according to value of current index
62 function AFCB_Allocate (Control_Block : Direct_AFCB) return FCB.AFCB_Ptr is
63 pragma Warnings (Off, Control_Block);
66 return new Direct_AFCB;
73 -- No special processing required for Direct_IO close
75 procedure AFCB_Close (File : access Direct_AFCB) is
76 pragma Warnings (Off, File);
86 procedure AFCB_Free (File : access Direct_AFCB) is
88 type FCB_Ptr is access all Direct_AFCB;
90 FT : FCB_Ptr := FCB_Ptr (File);
93 Unchecked_Deallocation (Direct_AFCB, FCB_Ptr);
104 (File : in out File_Type;
105 Mode : in FCB.File_Mode := FCB.Inout_File;
106 Name : in String := "";
107 Form : in String := "")
109 File_Control_Block : Direct_AFCB;
112 FIO.Open (File_Ptr => AP (File),
113 Dummy_FCB => File_Control_Block,
126 function End_Of_File (File : in File_Type) return Boolean is
128 FIO.Check_Read_Status (AP (File));
129 return Count (File.Index) > Size (File);
136 function Index (File : in File_Type) return Positive_Count is
138 FIO.Check_File_Open (AP (File));
139 return Count (File.Index);
147 (File : in out File_Type;
148 Mode : in FCB.File_Mode;
150 Form : in String := "")
152 File_Control_Block : Direct_AFCB;
155 FIO.Open (File_Ptr => AP (File),
156 Dummy_FCB => File_Control_Block,
170 (File : in File_Type;
172 Size : in Interfaces.C_Streams.size_t;
173 From : in Positive_Count)
176 Set_Index (File, From);
177 Read (File, Item, Size);
181 (File : in File_Type;
183 Size : in Interfaces.C_Streams.size_t)
186 FIO.Check_Read_Status (AP (File));
188 -- If last operation was not a read, or if in file sharing mode,
189 -- then reset the physical pointer of the file to match the index
190 -- We lock out task access over the two operations in this case.
192 if File.Last_Op /= Op_Read
193 or else File.Shared_Status = FCB.Yes
195 if End_Of_File (File) then
199 Locked_Processing : begin
202 FIO.Read_Buf (AP (File), Item, Size);
209 end Locked_Processing;
212 FIO.Read_Buf (AP (File), Item, Size);
215 File.Index := File.Index + 1;
217 -- Set last operation to read, unless we did not read a full record
218 -- (happens with the variant record case) in which case we set the
219 -- last operation as other, to force the file position to be reset
222 if File.Bytes = Size then
223 File.Last_Op := Op_Read;
225 File.Last_Op := Op_Other;
229 -- The following is the required overriding for Stream.Read, which is
230 -- not used, since we do not do Stream operations on Direct_IO files.
233 (File : in out Direct_AFCB;
234 Item : out Ada.Streams.Stream_Element_Array;
235 Last : out Ada.Streams.Stream_Element_Offset)
245 procedure Reset (File : in out File_Type; Mode : in FCB.File_Mode) is
247 FIO.Reset (AP (File), Mode);
249 File.Last_Op := Op_Read;
252 procedure Reset (File : in out File_Type) is
254 FIO.Reset (AP (File));
256 File.Last_Op := Op_Read;
263 procedure Set_Index (File : in File_Type; To : in Positive_Count) is
265 FIO.Check_File_Open (AP (File));
266 File.Index := Count (To);
267 File.Last_Op := Op_Other;
274 procedure Set_Position (File : in File_Type) is
277 (File.Stream, long (File.Bytes) *
278 long (File.Index - 1), SEEK_SET) /= 0
288 function Size (File : in File_Type) return Count is
290 FIO.Check_File_Open (AP (File));
291 File.Last_Op := Op_Other;
293 if fseek (File.Stream, 0, SEEK_END) /= 0 then
297 return Count (ftell (File.Stream) / long (File.Bytes));
307 Size : in Interfaces.C_Streams.size_t;
308 Zeroes : System.Storage_Elements.Storage_Array)
312 -- Do the actual write
314 procedure Do_Write is
316 FIO.Write_Buf (AP (File), Item, Size);
318 -- If we did not write the whole record (happens with the variant
319 -- record case), then fill out the rest of the record with zeroes.
320 -- This is cleaner in any case, and is required for the last
321 -- record, since otherwise the length of the file is wrong.
323 if File.Bytes > Size then
324 FIO.Write_Buf (AP (File), Zeroes'Address, File.Bytes - Size);
328 -- Start of processing for Write
331 FIO.Check_Write_Status (AP (File));
333 -- If last operation was not a write, or if in file sharing mode,
334 -- then reset the physical pointer of the file to match the index
335 -- We lock out task access over the two operations in this case.
337 if File.Last_Op /= Op_Write
338 or else File.Shared_Status = FCB.Yes
340 Locked_Processing : begin
350 end Locked_Processing;
356 File.Index := File.Index + 1;
358 -- Set last operation to write, unless we did not read a full record
359 -- (happens with the variant record case) in which case we set the
360 -- last operation as other, to force the file position to be reset
361 -- on the next write.
363 if File.Bytes = Size then
364 File.Last_Op := Op_Write;
366 File.Last_Op := Op_Other;
370 -- The following is the required overriding for Stream.Write, which is
371 -- not used, since we do not do Stream operations on Direct_IO files.
374 (File : in out Direct_AFCB;
375 Item : in Ada.Streams.Stream_Element_Array)
381 end System.Direct_IO;