OSDN Git Service

2003-12-11 Ed Falis <falis@gnat.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-direio.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUNTIME COMPONENTS                          --
4 --                                                                          --
5 --                     S Y S T E M . D I R E C T _ I O                      --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2002 Free Software Foundation, Inc.          --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with Ada.IO_Exceptions;         use Ada.IO_Exceptions;
35 with Interfaces.C_Streams;      use Interfaces.C_Streams;
36 with System;                    use System;
37 with System.File_IO;
38 with System.Soft_Links;
39 with Unchecked_Deallocation;
40
41 package body System.Direct_IO is
42
43    package FIO renames System.File_IO;
44    package SSL renames System.Soft_Links;
45
46    subtype AP is FCB.AFCB_Ptr;
47    use type FCB.Shared_Status_Type;
48
49    -----------------------
50    -- Local Subprograms --
51    -----------------------
52
53    procedure Set_Position (File : in File_Type);
54    --  Sets file position pointer according to value of current index
55
56    -------------------
57    -- AFCB_Allocate --
58    -------------------
59
60    function AFCB_Allocate (Control_Block : Direct_AFCB) return FCB.AFCB_Ptr is
61       pragma Unreferenced (Control_Block);
62
63    begin
64       return new Direct_AFCB;
65    end AFCB_Allocate;
66
67    ----------------
68    -- AFCB_Close --
69    ----------------
70
71    --  No special processing required for Direct_IO close
72
73    procedure AFCB_Close (File : access Direct_AFCB) is
74       pragma Unreferenced (File);
75
76    begin
77       null;
78    end AFCB_Close;
79
80    ---------------
81    -- AFCB_Free --
82    ---------------
83
84    procedure AFCB_Free (File : access Direct_AFCB) is
85
86       type FCB_Ptr is access all Direct_AFCB;
87
88       FT : FCB_Ptr := FCB_Ptr (File);
89
90       procedure Free is new
91         Unchecked_Deallocation (Direct_AFCB, FCB_Ptr);
92
93    begin
94       Free (FT);
95    end AFCB_Free;
96
97    ------------
98    -- Create --
99    ------------
100
101    procedure Create
102      (File : in out File_Type;
103       Mode : in FCB.File_Mode := FCB.Inout_File;
104       Name : in String := "";
105       Form : in String := "")
106    is
107       Dummy_File_Control_Block : Direct_AFCB;
108       pragma Warnings (Off, Dummy_File_Control_Block);
109       --  Yes, we know this is never assigned a value, only the tag
110       --  is used for dispatching purposes, so that's expected.
111
112    begin
113       FIO.Open (File_Ptr  => AP (File),
114                 Dummy_FCB => Dummy_File_Control_Block,
115                 Mode      => Mode,
116                 Name      => Name,
117                 Form      => Form,
118                 Amethod   => 'D',
119                 Creat     => True,
120                 Text      => False);
121    end Create;
122
123    -----------------
124    -- End_Of_File --
125    -----------------
126
127    function End_Of_File (File : in File_Type) return Boolean is
128    begin
129       FIO.Check_Read_Status (AP (File));
130       return Count (File.Index) > Size (File);
131    end End_Of_File;
132
133    -----------
134    -- Index --
135    -----------
136
137    function Index (File : in File_Type) return Positive_Count is
138    begin
139       FIO.Check_File_Open (AP (File));
140       return Count (File.Index);
141    end Index;
142
143    ----------
144    -- Open --
145    ----------
146
147    procedure Open
148      (File : in out File_Type;
149       Mode : in FCB.File_Mode;
150       Name : in String;
151       Form : in String := "")
152    is
153       Dummy_File_Control_Block : Direct_AFCB;
154       pragma Warnings (Off, Dummy_File_Control_Block);
155       --  Yes, we know this is never assigned a value, only the tag
156       --  is used for dispatching purposes, so that's expected.
157
158    begin
159       FIO.Open (File_Ptr  => AP (File),
160                 Dummy_FCB => Dummy_File_Control_Block,
161                 Mode      => Mode,
162                 Name      => Name,
163                 Form      => Form,
164                 Amethod   => 'D',
165                 Creat     => False,
166                 Text      => False);
167    end Open;
168
169    ----------
170    -- Read --
171    ----------
172
173    procedure Read
174      (File : in File_Type;
175       Item : Address;
176       Size : in Interfaces.C_Streams.size_t;
177       From : in Positive_Count)
178    is
179    begin
180       Set_Index (File, From);
181       Read (File, Item, Size);
182    end Read;
183
184    procedure Read
185      (File : in File_Type;
186       Item : Address;
187       Size : in Interfaces.C_Streams.size_t)
188    is
189    begin
190       FIO.Check_Read_Status (AP (File));
191
192       --  If last operation was not a read, or if in file sharing mode,
193       --  then reset the physical pointer of the file to match the index
194       --  We lock out task access over the two operations in this case.
195
196       if File.Last_Op /= Op_Read
197         or else File.Shared_Status = FCB.Yes
198       then
199          if End_Of_File (File) then
200             raise End_Error;
201          end if;
202
203          Locked_Processing : begin
204             SSL.Lock_Task.all;
205             Set_Position (File);
206             FIO.Read_Buf (AP (File), Item, Size);
207             SSL.Unlock_Task.all;
208
209          exception
210             when others =>
211                SSL.Unlock_Task.all;
212                raise;
213          end Locked_Processing;
214
215       else
216          FIO.Read_Buf (AP (File), Item, Size);
217       end if;
218
219       File.Index := File.Index + 1;
220
221       --  Set last operation to read, unless we did not read a full record
222       --  (happens with the variant record case) in which case we set the
223       --  last operation as other, to force the file position to be reset
224       --  on the next read.
225
226       if File.Bytes = Size then
227          File.Last_Op := Op_Read;
228       else
229          File.Last_Op := Op_Other;
230       end if;
231    end Read;
232
233    --  The following is the required overriding for Stream.Read, which is
234    --  not used, since we do not do Stream operations on Direct_IO files.
235
236    procedure Read
237      (File : in out Direct_AFCB;
238       Item : out Ada.Streams.Stream_Element_Array;
239       Last : out Ada.Streams.Stream_Element_Offset)
240    is
241    begin
242       raise Program_Error;
243    end Read;
244
245    -----------
246    -- Reset --
247    -----------
248
249    procedure Reset (File : in out File_Type; Mode : in FCB.File_Mode) is
250    begin
251       FIO.Reset (AP (File), Mode);
252       File.Index := 1;
253       File.Last_Op := Op_Read;
254    end Reset;
255
256    procedure Reset (File : in out File_Type) is
257    begin
258       FIO.Reset (AP (File));
259       File.Index := 1;
260       File.Last_Op := Op_Read;
261    end Reset;
262
263    ---------------
264    -- Set_Index --
265    ---------------
266
267    procedure Set_Index (File : in File_Type; To : in Positive_Count) is
268    begin
269       FIO.Check_File_Open (AP (File));
270       File.Index := Count (To);
271       File.Last_Op := Op_Other;
272    end Set_Index;
273
274    ------------------
275    -- Set_Position --
276    ------------------
277
278    procedure Set_Position (File : in File_Type) is
279    begin
280       if fseek
281            (File.Stream, long (File.Bytes) *
282               long (File.Index - 1), SEEK_SET) /= 0
283       then
284          raise Use_Error;
285       end if;
286    end Set_Position;
287
288    ----------
289    -- Size --
290    ----------
291
292    function Size (File : in File_Type) return Count is
293    begin
294       FIO.Check_File_Open (AP (File));
295       File.Last_Op := Op_Other;
296
297       if fseek (File.Stream, 0, SEEK_END) /= 0 then
298          raise Device_Error;
299       end if;
300
301       return Count (ftell (File.Stream) / long (File.Bytes));
302    end Size;
303
304    -----------
305    -- Write --
306    -----------
307
308    procedure Write
309      (File   : File_Type;
310       Item   : Address;
311       Size   : in Interfaces.C_Streams.size_t;
312       Zeroes : System.Storage_Elements.Storage_Array)
313
314    is
315       procedure Do_Write;
316       --  Do the actual write
317
318       procedure Do_Write is
319       begin
320          FIO.Write_Buf (AP (File), Item, Size);
321
322          --  If we did not write the whole record (happens with the variant
323          --  record case), then fill out the rest of the record with zeroes.
324          --  This is cleaner in any case, and is required for the last
325          --  record, since otherwise the length of the file is wrong.
326
327          if File.Bytes > Size then
328             FIO.Write_Buf (AP (File), Zeroes'Address, File.Bytes - Size);
329          end if;
330       end Do_Write;
331
332    --  Start of processing for Write
333
334    begin
335       FIO.Check_Write_Status (AP (File));
336
337       --  If last operation was not a write, or if in file sharing mode,
338       --  then reset the physical pointer of the file to match the index
339       --  We lock out task access over the two operations in this case.
340
341       if File.Last_Op /= Op_Write
342         or else File.Shared_Status = FCB.Yes
343       then
344          Locked_Processing : begin
345             SSL.Lock_Task.all;
346             Set_Position (File);
347             Do_Write;
348             SSL.Unlock_Task.all;
349
350          exception
351             when others =>
352                SSL.Unlock_Task.all;
353                raise;
354          end Locked_Processing;
355
356       else
357          Do_Write;
358       end if;
359
360       File.Index := File.Index + 1;
361
362       --  Set last operation to write, unless we did not read a full record
363       --  (happens with the variant record case) in which case we set the
364       --  last operation as other, to force the file position to be reset
365       --  on the next write.
366
367       if File.Bytes = Size then
368          File.Last_Op := Op_Write;
369       else
370          File.Last_Op := Op_Other;
371       end if;
372    end Write;
373
374    --  The following is the required overriding for Stream.Write, which is
375    --  not used, since we do not do Stream operations on Direct_IO files.
376
377    procedure Write
378      (File : in out Direct_AFCB;
379       Item : in Ada.Streams.Stream_Element_Array)
380    is
381    begin
382       raise Program_Error;
383    end Write;
384
385 end System.Direct_IO;