1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S T R E A M S . S T R E A M _ I O --
9 -- Copyright (C) 1992-2003, Free Software Foundation, Inc. --
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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Interfaces.C_Streams; use Interfaces.C_Streams;
35 with System; use System;
37 with System.Soft_Links;
39 with Unchecked_Conversion;
40 with Unchecked_Deallocation;
42 package body Ada.Streams.Stream_IO is
44 package FIO renames System.File_IO;
45 package SSL renames System.Soft_Links;
47 subtype AP is FCB.AFCB_Ptr;
49 function To_FCB is new Unchecked_Conversion (File_Mode, FCB.File_Mode);
50 function To_SIO is new Unchecked_Conversion (FCB.File_Mode, File_Mode);
51 use type FCB.File_Mode;
52 use type FCB.Shared_Status_Type;
54 -----------------------
55 -- Local Subprograms --
56 -----------------------
58 procedure Set_Position (File : in File_Type);
59 -- Sets file position pointer according to value of current index
65 function AFCB_Allocate (Control_Block : Stream_AFCB) return FCB.AFCB_Ptr is
66 pragma Warnings (Off, Control_Block);
69 return new Stream_AFCB;
76 -- No special processing required for closing Stream_IO file
78 procedure AFCB_Close (File : access Stream_AFCB) is
79 pragma Warnings (Off, File);
89 procedure AFCB_Free (File : access Stream_AFCB) is
90 type FCB_Ptr is access all Stream_AFCB;
91 FT : FCB_Ptr := FCB_Ptr (File);
93 procedure Free is new Unchecked_Deallocation (Stream_AFCB, FCB_Ptr);
103 procedure Close (File : in out File_Type) is
105 FIO.Close (AP (File));
113 (File : in out File_Type;
114 Mode : in File_Mode := Out_File;
115 Name : in String := "";
116 Form : in String := "")
118 Dummy_File_Control_Block : Stream_AFCB;
119 pragma Warnings (Off, Dummy_File_Control_Block);
120 -- Yes, we know this is never assigned a value, only the tag
121 -- is used for dispatching purposes, so that's expected.
124 FIO.Open (File_Ptr => AP (File),
125 Dummy_FCB => Dummy_File_Control_Block,
126 Mode => To_FCB (Mode),
132 File.Last_Op := Op_Write;
139 procedure Delete (File : in out File_Type) is
141 FIO.Delete (AP (File));
148 function End_Of_File (File : in File_Type) return Boolean is
150 FIO.Check_Read_Status (AP (File));
151 return Count (File.Index) > Size (File);
158 procedure Flush (File : File_Type) is
160 FIO.Flush (AP (File));
167 function Form (File : in File_Type) return String is
169 return FIO.Form (AP (File));
176 function Index (File : in File_Type) return Positive_Count is
178 FIO.Check_File_Open (AP (File));
179 return Count (File.Index);
186 function Is_Open (File : in File_Type) return Boolean is
188 return FIO.Is_Open (AP (File));
195 function Mode (File : in File_Type) return File_Mode is
197 return To_SIO (FIO.Mode (AP (File)));
204 function Name (File : in File_Type) return String is
206 return FIO.Name (AP (File));
214 (File : in out File_Type;
217 Form : in String := "")
219 Dummy_File_Control_Block : Stream_AFCB;
220 pragma Warnings (Off, Dummy_File_Control_Block);
221 -- Yes, we know this is never assigned a value, only the tag
222 -- is used for dispatching purposes, so that's expected.
225 FIO.Open (File_Ptr => AP (File),
226 Dummy_FCB => Dummy_File_Control_Block,
227 Mode => To_FCB (Mode),
234 -- Ensure that the stream index is set properly (e.g., for Append_File)
238 -- Set last operation. The purpose here is to ensure proper handling
239 -- of the initial operation. In general, a write after a read requires
240 -- resetting and doing a seek, so we set the last operation as Read
241 -- for an In_Out file, but for an Out file we set the last operation
242 -- to Op_Write, since in this case it is not necessary to do a seek
243 -- (and furthermore there are situations (such as the case of writing
244 -- a sequential Posix FIFO file) where the lseek would cause problems.
246 if Mode = Out_File then
247 File.Last_Op := Op_Write;
249 File.Last_Op := Op_Read;
258 (File : in File_Type;
259 Item : out Stream_Element_Array;
260 Last : out Stream_Element_Offset;
261 From : in Positive_Count)
264 Set_Index (File, From);
265 Read (File, Item, Last);
269 (File : in File_Type;
270 Item : out Stream_Element_Array;
271 Last : out Stream_Element_Offset)
276 FIO.Check_Read_Status (AP (File));
278 -- If last operation was not a read, or if in file sharing mode,
279 -- then reset the physical pointer of the file to match the index
280 -- We lock out task access over the two operations in this case.
282 if File.Last_Op /= Op_Read
283 or else File.Shared_Status = FCB.Yes
285 Locked_Processing : begin
288 FIO.Read_Buf (AP (File), Item'Address, Item'Length, Nread);
295 end Locked_Processing;
298 FIO.Read_Buf (AP (File), Item'Address, Item'Length, Nread);
301 File.Index := File.Index + Count (Nread);
302 Last := Item'First + Stream_Element_Offset (Nread) - 1;
303 File.Last_Op := Op_Read;
306 -- This version of Read is the primitive operation on the underlying
307 -- Stream type, used when a Stream_IO file is treated as a Stream
310 (File : in out Stream_AFCB;
311 Item : out Ada.Streams.Stream_Element_Array;
312 Last : out Ada.Streams.Stream_Element_Offset)
315 Read (File'Unchecked_Access, Item, Last);
322 procedure Reset (File : in out File_Type; Mode : in File_Mode) is
324 FIO.Check_File_Open (AP (File));
326 -- Reset file index to start of file for read/write cases. For
327 -- the append case, the Set_Mode call repositions the index.
330 Set_Mode (File, Mode);
333 procedure Reset (File : in out File_Type) is
335 Reset (File, To_SIO (File.Mode));
342 procedure Set_Index (File : in File_Type; To : in Positive_Count) is
344 FIO.Check_File_Open (AP (File));
345 File.Index := Count (To);
346 File.Last_Op := Op_Other;
353 procedure Set_Mode (File : in out File_Type; Mode : in File_Mode) is
355 FIO.Check_File_Open (AP (File));
357 -- If we are switching from read to write, or vice versa, and
358 -- we are not already open in update mode, then reopen in update
359 -- mode now. Note that we can use Inout_File as the mode for the
360 -- call since File_IO handles all modes for all file types.
362 if ((File.Mode = FCB.In_File) /= (Mode = In_File))
363 and then not File.Update_Mode
365 FIO.Reset (AP (File), FCB.Inout_File);
366 File.Update_Mode := True;
369 -- Set required mode and position to end of file if append mode
371 File.Mode := To_FCB (Mode);
372 FIO.Append_Set (AP (File));
374 if File.Mode = FCB.Append_File then
375 File.Index := Count (ftell (File.Stream)) + 1;
378 File.Last_Op := Op_Other;
385 procedure Set_Position (File : in File_Type) is
386 use type System.CRTL.long;
388 if fseek (File.Stream,
389 System.CRTL.long (File.Index) - 1, SEEK_SET) /= 0
399 function Size (File : in File_Type) return Count is
401 FIO.Check_File_Open (AP (File));
403 if File.File_Size = -1 then
404 File.Last_Op := Op_Other;
406 if fseek (File.Stream, 0, SEEK_END) /= 0 then
410 File.File_Size := Stream_Element_Offset (ftell (File.Stream));
413 return Count (File.File_Size);
420 function Stream (File : in File_Type) return Stream_Access is
422 FIO.Check_File_Open (AP (File));
423 return Stream_Access (File);
431 (File : in File_Type;
432 Item : in Stream_Element_Array;
433 To : in Positive_Count)
436 Set_Index (File, To);
440 procedure Write (File : in File_Type; Item : in Stream_Element_Array) is
442 FIO.Check_Write_Status (AP (File));
444 -- If last operation was not a write, or if in file sharing mode,
445 -- then reset the physical pointer of the file to match the index
446 -- We lock out task access over the two operations in this case.
448 if File.Last_Op /= Op_Write
449 or else File.Shared_Status = FCB.Yes
451 Locked_Processing : begin
454 FIO.Write_Buf (AP (File), Item'Address, Item'Length);
461 end Locked_Processing;
464 FIO.Write_Buf (AP (File), Item'Address, Item'Length);
467 File.Index := File.Index + Item'Length;
468 File.Last_Op := Op_Write;
469 File.File_Size := -1;
472 -- This version of Write is the primitive operation on the underlying
473 -- Stream type, used when a Stream_IO file is treated as a Stream
476 (File : in out Stream_AFCB;
477 Item : in Ada.Streams.Stream_Element_Array)
480 Write (File'Unchecked_Access, Item);
483 end Ada.Streams.Stream_IO;