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-2001, 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;
38 with Unchecked_Conversion;
39 with Unchecked_Deallocation;
41 package body Ada.Streams.Stream_IO is
43 package FIO renames System.File_IO;
44 package SSL renames System.Soft_Links;
46 subtype AP is FCB.AFCB_Ptr;
48 function To_FCB is new Unchecked_Conversion (File_Mode, FCB.File_Mode);
49 function To_SIO is new Unchecked_Conversion (FCB.File_Mode, File_Mode);
50 use type FCB.File_Mode;
51 use type FCB.Shared_Status_Type;
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
57 procedure Set_Position (File : in File_Type);
58 -- Sets file position pointer according to value of current index
64 function AFCB_Allocate (Control_Block : Stream_AFCB) return FCB.AFCB_Ptr is
65 pragma Warnings (Off, Control_Block);
68 return new Stream_AFCB;
75 -- No special processing required for closing Stream_IO file
77 procedure AFCB_Close (File : access Stream_AFCB) is
78 pragma Warnings (Off, File);
88 procedure AFCB_Free (File : access Stream_AFCB) is
89 type FCB_Ptr is access all Stream_AFCB;
90 FT : FCB_Ptr := FCB_Ptr (File);
92 procedure Free is new Unchecked_Deallocation (Stream_AFCB, FCB_Ptr);
102 procedure Close (File : in out File_Type) is
104 FIO.Close (AP (File));
112 (File : in out File_Type;
113 Mode : in File_Mode := Out_File;
114 Name : in String := "";
115 Form : in String := "")
117 File_Control_Block : Stream_AFCB;
120 FIO.Open (File_Ptr => AP (File),
121 Dummy_FCB => File_Control_Block,
122 Mode => To_FCB (Mode),
128 File.Last_Op := Op_Write;
135 procedure Delete (File : in out File_Type) is
137 FIO.Delete (AP (File));
144 function End_Of_File (File : in File_Type) return Boolean is
146 FIO.Check_Read_Status (AP (File));
147 return Count (File.Index) > Size (File);
154 procedure Flush (File : File_Type) is
156 FIO.Flush (AP (File));
163 function Form (File : in File_Type) return String is
165 return FIO.Form (AP (File));
172 function Index (File : in File_Type) return Positive_Count is
174 FIO.Check_File_Open (AP (File));
175 return Count (File.Index);
182 function Is_Open (File : in File_Type) return Boolean is
184 return FIO.Is_Open (AP (File));
191 function Mode (File : in File_Type) return File_Mode is
193 return To_SIO (FIO.Mode (AP (File)));
200 function Name (File : in File_Type) return String is
202 return FIO.Name (AP (File));
210 (File : in out File_Type;
213 Form : in String := "")
215 File_Control_Block : Stream_AFCB;
218 FIO.Open (File_Ptr => AP (File),
219 Dummy_FCB => File_Control_Block,
220 Mode => To_FCB (Mode),
227 -- Ensure that the stream index is set properly (e.g., for Append_File)
231 File.Last_Op := Op_Read;
239 (File : in File_Type;
240 Item : out Stream_Element_Array;
241 Last : out Stream_Element_Offset;
242 From : in Positive_Count)
245 Set_Index (File, From);
246 Read (File, Item, Last);
250 (File : in File_Type;
251 Item : out Stream_Element_Array;
252 Last : out Stream_Element_Offset)
257 FIO.Check_Read_Status (AP (File));
259 -- If last operation was not a read, or if in file sharing mode,
260 -- then reset the physical pointer of the file to match the index
261 -- We lock out task access over the two operations in this case.
263 if File.Last_Op /= Op_Read
264 or else File.Shared_Status = FCB.Yes
266 Locked_Processing : begin
269 FIO.Read_Buf (AP (File), Item'Address, Item'Length, Nread);
276 end Locked_Processing;
279 FIO.Read_Buf (AP (File), Item'Address, Item'Length, Nread);
282 File.Index := File.Index + Count (Nread);
283 Last := Item'First + Stream_Element_Offset (Nread) - 1;
284 File.Last_Op := Op_Read;
287 -- This version of Read is the primitive operation on the underlying
288 -- Stream type, used when a Stream_IO file is treated as a Stream
291 (File : in out Stream_AFCB;
292 Item : out Ada.Streams.Stream_Element_Array;
293 Last : out Ada.Streams.Stream_Element_Offset)
296 Read (File'Unchecked_Access, Item, Last);
303 procedure Reset (File : in out File_Type; Mode : in File_Mode) is
305 FIO.Check_File_Open (AP (File));
307 -- Reset file index to start of file for read/write cases. For
308 -- the append case, the Set_Mode call repositions the index.
311 Set_Mode (File, Mode);
314 procedure Reset (File : in out File_Type) is
316 Reset (File, To_SIO (File.Mode));
323 procedure Set_Index (File : in File_Type; To : in Positive_Count) is
325 FIO.Check_File_Open (AP (File));
326 File.Index := Count (To);
327 File.Last_Op := Op_Other;
334 procedure Set_Mode (File : in out File_Type; Mode : in File_Mode) is
336 FIO.Check_File_Open (AP (File));
338 -- If we are switching from read to write, or vice versa, and
339 -- we are not already open in update mode, then reopen in update
340 -- mode now. Note that we can use Inout_File as the mode for the
341 -- call since File_IO handles all modes for all file types.
343 if ((File.Mode = FCB.In_File) /= (Mode = In_File))
344 and then not File.Update_Mode
346 FIO.Reset (AP (File), FCB.Inout_File);
347 File.Update_Mode := True;
350 -- Set required mode and position to end of file if append mode
352 File.Mode := To_FCB (Mode);
353 FIO.Append_Set (AP (File));
355 if File.Mode = FCB.Append_File then
356 File.Index := Count (ftell (File.Stream)) + 1;
359 File.Last_Op := Op_Other;
366 procedure Set_Position (File : in File_Type) is
368 if fseek (File.Stream, long (File.Index) - 1, SEEK_SET) /= 0 then
377 function Size (File : in File_Type) return Count is
379 FIO.Check_File_Open (AP (File));
381 if File.File_Size = -1 then
382 File.Last_Op := Op_Other;
384 if fseek (File.Stream, 0, SEEK_END) /= 0 then
388 File.File_Size := Stream_Element_Offset (ftell (File.Stream));
391 return Count (File.File_Size);
398 function Stream (File : in File_Type) return Stream_Access is
400 FIO.Check_File_Open (AP (File));
401 return Stream_Access (File);
409 (File : in File_Type;
410 Item : in Stream_Element_Array;
411 To : in Positive_Count)
414 Set_Index (File, To);
418 procedure Write (File : in File_Type; Item : in Stream_Element_Array) is
420 FIO.Check_Write_Status (AP (File));
422 -- If last operation was not a write, or if in file sharing mode,
423 -- then reset the physical pointer of the file to match the index
424 -- We lock out task access over the two operations in this case.
426 if File.Last_Op /= Op_Write
427 or else File.Shared_Status = FCB.Yes
429 Locked_Processing : begin
432 FIO.Write_Buf (AP (File), Item'Address, Item'Length);
439 end Locked_Processing;
442 FIO.Write_Buf (AP (File), Item'Address, Item'Length);
445 File.Index := File.Index + Item'Length;
446 File.Last_Op := Op_Write;
447 File.File_Size := -1;
450 -- This version of Write is the primitive operation on the underlying
451 -- Stream type, used when a Stream_IO file is treated as a Stream
454 (File : in out Stream_AFCB;
455 Item : in Ada.Streams.Stream_Element_Array)
458 Write (File'Unchecked_Access, Item);
461 end Ada.Streams.Stream_IO;