1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . S T R E A M S . S T R E A M _ I O --
9 -- Copyright (C) 1992-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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
;
36 with System
; use System
;
38 with System
.Soft_Links
;
41 with Unchecked_Conversion
;
42 with Unchecked_Deallocation
;
44 package body Ada
.Streams
.Stream_IO
is
46 package FIO
renames System
.File_IO
;
47 package SSL
renames System
.Soft_Links
;
49 subtype AP
is FCB
.AFCB_Ptr
;
51 function To_FCB
is new Unchecked_Conversion
(File_Mode
, FCB
.File_Mode
);
52 function To_SIO
is new Unchecked_Conversion
(FCB
.File_Mode
, File_Mode
);
53 use type FCB
.File_Mode
;
54 use type FCB
.Shared_Status_Type
;
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
60 procedure Set_Position
(File
: File_Type
);
61 -- Sets file position pointer according to value of current index
67 function AFCB_Allocate
(Control_Block
: Stream_AFCB
) return FCB
.AFCB_Ptr
is
68 pragma Warnings
(Off
, Control_Block
);
70 return new Stream_AFCB
;
77 -- No special processing required for closing Stream_IO file
79 procedure AFCB_Close
(File
: access Stream_AFCB
) is
80 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
: File_Mode
:= Out_File
;
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
: 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
: File_Type
) return String is
169 return FIO
.Form
(AP
(File
));
176 function Index
(File
: File_Type
) return Positive_Count
is
178 FIO
.Check_File_Open
(AP
(File
));
179 return Count
(File
.Index
);
186 function Is_Open
(File
: File_Type
) return Boolean is
188 return FIO
.Is_Open
(AP
(File
));
195 function Mode
(File
: File_Type
) return File_Mode
is
197 return To_SIO
(FIO
.Mode
(AP
(File
)));
204 function Name
(File
: File_Type
) return String is
206 return FIO
.Name
(AP
(File
));
214 (File
: in out File_Type
;
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
;
259 Item
: out Stream_Element_Array
;
260 Last
: out Stream_Element_Offset
;
261 From
: Positive_Count
)
264 Set_Index
(File
, From
);
265 Read
(File
, Item
, Last
);
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
: 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
: File_Type
; To
: 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
: 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
: 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
: 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
: File_Type
) return Stream_Access
is
422 FIO
.Check_File_Open
(AP
(File
));
423 return Stream_Access
(File
);
432 Item
: Stream_Element_Array
;
436 Set_Index
(File
, To
);
442 Item
: Stream_Element_Array
)
445 FIO
.Check_Write_Status
(AP
(File
));
447 -- If last operation was not a write, or if in file sharing mode,
448 -- then reset the physical pointer of the file to match the index
449 -- We lock out task access over the two operations in this case.
451 if File
.Last_Op
/= Op_Write
452 or else File
.Shared_Status
= FCB
.Yes
454 Locked_Processing
: begin
457 FIO
.Write_Buf
(AP
(File
), Item
'Address, Item
'Length);
464 end Locked_Processing
;
467 FIO
.Write_Buf
(AP
(File
), Item
'Address, Item
'Length);
470 File
.Index
:= File
.Index
+ Item
'Length;
471 File
.Last_Op
:= Op_Write
;
472 File
.File_Size
:= -1;
475 -- This version of Write is the primitive operation on the underlying
476 -- Stream type, used when a Stream_IO file is treated as a Stream
479 (File
: in out Stream_AFCB
;
480 Item
: Ada
.Streams
.Stream_Element_Array
)
483 Write
(File
'Unchecked_Access, Item
);
486 end Ada
.Streams
.Stream_IO
;