1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S T R E A M S . S T R E A M _ I O --
11 -- Copyright (C) 1992-2000, 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 Interfaces
.C_Streams
; use Interfaces
.C_Streams
;
37 with System
; use System
;
39 with System
.Soft_Links
;
40 with Unchecked_Conversion
;
41 with Unchecked_Deallocation
;
43 package body Ada
.Streams
.Stream_IO
is
45 package FIO
renames System
.File_IO
;
46 package SSL
renames System
.Soft_Links
;
48 subtype AP
is FCB
.AFCB_Ptr
;
50 function To_FCB
is new Unchecked_Conversion
(File_Mode
, FCB
.File_Mode
);
51 function To_SIO
is new Unchecked_Conversion
(FCB
.File_Mode
, File_Mode
);
52 use type FCB
.File_Mode
;
53 use type FCB
.Shared_Status_Type
;
55 -----------------------
56 -- Local Subprograms --
57 -----------------------
59 procedure Set_Position
(File
: in File_Type
);
60 -- Sets file position pointer according to value of current index
66 function AFCB_Allocate
(Control_Block
: Stream_AFCB
) return FCB
.AFCB_Ptr
is
68 return new Stream_AFCB
;
75 -- No special processing required for closing Stream_IO file
77 procedure AFCB_Close
(File
: access Stream_AFCB
) is
86 procedure AFCB_Free
(File
: access Stream_AFCB
) is
87 type FCB_Ptr
is access all Stream_AFCB
;
88 FT
: FCB_Ptr
:= FCB_Ptr
(File
);
90 procedure Free
is new Unchecked_Deallocation
(Stream_AFCB
, FCB_Ptr
);
100 procedure Close
(File
: in out File_Type
) is
102 FIO
.Close
(AP
(File
));
110 (File
: in out File_Type
;
111 Mode
: in File_Mode
:= Out_File
;
112 Name
: in String := "";
113 Form
: in String := "")
115 File_Control_Block
: Stream_AFCB
;
118 FIO
.Open
(File_Ptr
=> AP
(File
),
119 Dummy_FCB
=> File_Control_Block
,
120 Mode
=> To_FCB
(Mode
),
126 File
.Last_Op
:= Op_Write
;
133 procedure Delete
(File
: in out File_Type
) is
135 FIO
.Delete
(AP
(File
));
142 function End_Of_File
(File
: in File_Type
) return Boolean is
144 FIO
.Check_Read_Status
(AP
(File
));
145 return Count
(File
.Index
) > Size
(File
);
152 procedure Flush
(File
: in out File_Type
) is
154 FIO
.Flush
(AP
(File
));
161 function Form
(File
: in File_Type
) return String is
163 return FIO
.Form
(AP
(File
));
170 function Index
(File
: in File_Type
) return Positive_Count
is
172 FIO
.Check_File_Open
(AP
(File
));
173 return Count
(File
.Index
);
180 function Is_Open
(File
: in File_Type
) return Boolean is
182 return FIO
.Is_Open
(AP
(File
));
189 function Mode
(File
: in File_Type
) return File_Mode
is
191 return To_SIO
(FIO
.Mode
(AP
(File
)));
198 function Name
(File
: in File_Type
) return String is
200 return FIO
.Name
(AP
(File
));
208 (File
: in out File_Type
;
211 Form
: in String := "")
213 File_Control_Block
: Stream_AFCB
;
216 FIO
.Open
(File_Ptr
=> AP
(File
),
217 Dummy_FCB
=> File_Control_Block
,
218 Mode
=> To_FCB
(Mode
),
225 -- Ensure that the stream index is set properly (e.g., for Append_File)
229 File
.Last_Op
:= Op_Read
;
237 (File
: in File_Type
;
238 Item
: out Stream_Element_Array
;
239 Last
: out Stream_Element_Offset
;
240 From
: in Positive_Count
)
243 Set_Index
(File
, From
);
244 Read
(File
, Item
, Last
);
248 (File
: in File_Type
;
249 Item
: out Stream_Element_Array
;
250 Last
: out Stream_Element_Offset
)
255 FIO
.Check_Read_Status
(AP
(File
));
257 -- If last operation was not a read, or if in file sharing mode,
258 -- then reset the physical pointer of the file to match the index
259 -- We lock out task access over the two operations in this case.
261 if File
.Last_Op
/= Op_Read
262 or else File
.Shared_Status
= FCB
.Yes
264 if End_Of_File
(File
) then
268 Locked_Processing
: begin
271 FIO
.Read_Buf
(AP
(File
), Item
'Address, Item
'Length, Nread
);
278 end Locked_Processing
;
281 FIO
.Read_Buf
(AP
(File
), Item
'Address, Item
'Length, Nread
);
284 File
.Index
:= File
.Index
+ Count
(Nread
);
285 Last
:= Item
'First + Stream_Element_Offset
(Nread
) - 1;
286 File
.Last_Op
:= Op_Read
;
289 -- This version of Read is the primitive operation on the underlying
290 -- Stream type, used when a Stream_IO file is treated as a Stream
293 (File
: in out Stream_AFCB
;
294 Item
: out Ada
.Streams
.Stream_Element_Array
;
295 Last
: out Ada
.Streams
.Stream_Element_Offset
)
298 Read
(File
'Unchecked_Access, Item
, Last
);
305 procedure Reset
(File
: in out File_Type
; Mode
: in File_Mode
) is
307 FIO
.Check_File_Open
(AP
(File
));
309 -- Reset file index to start of file for read/write cases. For
310 -- the append case, the Set_Mode call repositions the index.
313 Set_Mode
(File
, Mode
);
316 procedure Reset
(File
: in out File_Type
) is
318 Reset
(File
, To_SIO
(File
.Mode
));
325 procedure Set_Index
(File
: in File_Type
; To
: in Positive_Count
) is
327 FIO
.Check_File_Open
(AP
(File
));
328 File
.Index
:= Count
(To
);
329 File
.Last_Op
:= Op_Other
;
336 procedure Set_Mode
(File
: in out File_Type
; Mode
: in File_Mode
) is
338 FIO
.Check_File_Open
(AP
(File
));
340 -- If we are switching from read to write, or vice versa, and
341 -- we are not already open in update mode, then reopen in update
342 -- mode now. Note that we can use Inout_File as the mode for the
343 -- call since File_IO handles all modes for all file types.
345 if ((File
.Mode
= FCB
.In_File
) /= (Mode
= In_File
))
346 and then not File
.Update_Mode
348 FIO
.Reset
(AP
(File
), FCB
.Inout_File
);
349 File
.Update_Mode
:= True;
352 -- Set required mode and position to end of file if append mode
354 File
.Mode
:= To_FCB
(Mode
);
355 FIO
.Append_Set
(AP
(File
));
357 if File
.Mode
= FCB
.Append_File
then
358 File
.Index
:= Count
(ftell
(File
.Stream
)) + 1;
361 File
.Last_Op
:= Op_Other
;
368 procedure Set_Position
(File
: in File_Type
) is
370 if fseek
(File
.Stream
, long
(File
.Index
) - 1, SEEK_SET
) /= 0 then
379 function Size
(File
: in File_Type
) return Count
is
381 FIO
.Check_File_Open
(AP
(File
));
383 if File
.File_Size
= -1 then
384 File
.Last_Op
:= Op_Other
;
386 if fseek
(File
.Stream
, 0, SEEK_END
) /= 0 then
390 File
.File_Size
:= Stream_Element_Offset
(ftell
(File
.Stream
));
393 return Count
(File
.File_Size
);
400 function Stream
(File
: in File_Type
) return Stream_Access
is
402 FIO
.Check_File_Open
(AP
(File
));
403 return Stream_Access
(File
);
411 (File
: in File_Type
;
412 Item
: in Stream_Element_Array
;
413 To
: in Positive_Count
)
416 Set_Index
(File
, To
);
420 procedure Write
(File
: in File_Type
; Item
: in Stream_Element_Array
) is
422 FIO
.Check_Write_Status
(AP
(File
));
424 -- If last operation was not a write, or if in file sharing mode,
425 -- then reset the physical pointer of the file to match the index
426 -- We lock out task access over the two operations in this case.
428 if File
.Last_Op
/= Op_Write
429 or else File
.Shared_Status
= FCB
.Yes
431 Locked_Processing
: begin
434 FIO
.Write_Buf
(AP
(File
), Item
'Address, Item
'Length);
441 end Locked_Processing
;
444 FIO
.Write_Buf
(AP
(File
), Item
'Address, Item
'Length);
447 File
.Index
:= File
.Index
+ Item
'Length;
448 File
.Last_Op
:= Op_Write
;
449 File
.File_Size
:= -1;
452 -- This version of Write is the primitive operation on the underlying
453 -- Stream type, used when a Stream_IO file is treated as a Stream
456 (File
: in out Stream_AFCB
;
457 Item
: in Ada
.Streams
.Stream_Element_Array
)
460 Write
(File
'Unchecked_Access, Item
);
463 end Ada
.Streams
.Stream_IO
;