1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- S Y S T E M . D I R E C T _ 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 -- 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
64 return new Direct_AFCB;
71 -- No special processing required for Direct_IO close
73 procedure AFCB_Close (File : access Direct_AFCB) is
82 procedure AFCB_Free (File : access Direct_AFCB) is
84 type FCB_Ptr is access all Direct_AFCB;
86 FT : FCB_Ptr := FCB_Ptr (File);
89 Unchecked_Deallocation (Direct_AFCB, FCB_Ptr);
100 (File : in out File_Type;
101 Mode : in FCB.File_Mode := FCB.Inout_File;
102 Name : in String := "";
103 Form : in String := "")
105 File_Control_Block : Direct_AFCB;
108 FIO.Open (File_Ptr => AP (File),
109 Dummy_FCB => File_Control_Block,
122 function End_Of_File (File : in File_Type) return Boolean is
124 FIO.Check_Read_Status (AP (File));
125 return Count (File.Index) > Size (File);
132 function Index (File : in File_Type) return Positive_Count is
134 FIO.Check_File_Open (AP (File));
135 return Count (File.Index);
143 (File : in out File_Type;
144 Mode : in FCB.File_Mode;
146 Form : in String := "")
148 File_Control_Block : Direct_AFCB;
151 FIO.Open (File_Ptr => AP (File),
152 Dummy_FCB => File_Control_Block,
166 (File : in File_Type;
168 Size : in Interfaces.C_Streams.size_t;
169 From : in Positive_Count)
172 Set_Index (File, From);
173 Read (File, Item, Size);
177 (File : in File_Type;
179 Size : in Interfaces.C_Streams.size_t)
182 FIO.Check_Read_Status (AP (File));
184 -- If last operation was not a read, or if in file sharing mode,
185 -- then reset the physical pointer of the file to match the index
186 -- We lock out task access over the two operations in this case.
188 if File.Last_Op /= Op_Read
189 or else File.Shared_Status = FCB.Yes
191 if End_Of_File (File) then
195 Locked_Processing : begin
198 FIO.Read_Buf (AP (File), Item, Size);
205 end Locked_Processing;
208 FIO.Read_Buf (AP (File), Item, Size);
211 File.Index := File.Index + 1;
213 -- Set last operation to read, unless we did not read a full record
214 -- (happens with the variant record case) in which case we set the
215 -- last operation as other, to force the file position to be reset
218 if File.Bytes = Size then
219 File.Last_Op := Op_Read;
221 File.Last_Op := Op_Other;
225 -- The following is the required overriding for Stream.Read, which is
226 -- not used, since we do not do Stream operations on Direct_IO files.
229 (File : in out Direct_AFCB;
230 Item : out Ada.Streams.Stream_Element_Array;
231 Last : out Ada.Streams.Stream_Element_Offset)
241 procedure Reset (File : in out File_Type; Mode : in FCB.File_Mode) is
243 FIO.Reset (AP (File), Mode);
245 File.Last_Op := Op_Read;
248 procedure Reset (File : in out File_Type) is
250 FIO.Reset (AP (File));
252 File.Last_Op := Op_Read;
259 procedure Set_Index (File : in File_Type; To : in Positive_Count) is
261 FIO.Check_File_Open (AP (File));
262 File.Index := Count (To);
263 File.Last_Op := Op_Other;
270 procedure Set_Position (File : in File_Type) is
273 (File.Stream, long (File.Bytes) *
274 long (File.Index - 1), SEEK_SET) /= 0
284 function Size (File : in File_Type) return Count is
286 FIO.Check_File_Open (AP (File));
287 File.Last_Op := Op_Other;
289 if fseek (File.Stream, 0, SEEK_END) /= 0 then
293 return Count (ftell (File.Stream) / long (File.Bytes));
303 Size : in Interfaces.C_Streams.size_t;
304 Zeroes : System.Storage_Elements.Storage_Array)
308 -- Do the actual write
310 procedure Do_Write is
312 FIO.Write_Buf (AP (File), Item, Size);
314 -- If we did not write the whole record (happens with the variant
315 -- record case), then fill out the rest of the record with zeroes.
316 -- This is cleaner in any case, and is required for the last
317 -- record, since otherwise the length of the file is wrong.
319 if File.Bytes > Size then
320 FIO.Write_Buf (AP (File), Zeroes'Address, File.Bytes - Size);
324 -- Start of processing for Write
327 FIO.Check_Write_Status (AP (File));
329 -- If last operation was not a write, or if in file sharing mode,
330 -- then reset the physical pointer of the file to match the index
331 -- We lock out task access over the two operations in this case.
333 if File.Last_Op /= Op_Write
334 or else File.Shared_Status = FCB.Yes
336 Locked_Processing : begin
346 end Locked_Processing;
352 File.Index := File.Index + 1;
354 -- Set last operation to write, unless we did not read a full record
355 -- (happens with the variant record case) in which case we set the
356 -- last operation as other, to force the file position to be reset
357 -- on the next write.
359 if File.Bytes = Size then
360 File.Last_Op := Op_Write;
362 File.Last_Op := Op_Other;
366 -- The following is the required overriding for Stream.Write, which is
367 -- not used, since we do not do Stream operations on Direct_IO files.
370 (File : in out Direct_AFCB;
371 Item : in Ada.Streams.Stream_Element_Array)
377 end System.Direct_IO;