1 //===-- runtime/connection.h ------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Fortran I/O connection state (abstracted over internal & external units)
11 #ifndef FORTRAN_RUNTIME_IO_CONNECTION_H_
12 #define FORTRAN_RUNTIME_IO_CONNECTION_H_
18 namespace Fortran::runtime::io
{
20 enum class Direction
{ Output
, Input
};
21 enum class Access
{ Sequential
, Direct
, Stream
};
23 inline bool IsRecordFile(Access a
) { return a
!= Access::Stream
; }
25 // These characteristics of a connection are immutable after being
26 // established in an OPEN statement.
27 struct ConnectionAttributes
{
28 Access access
{Access::Sequential
}; // ACCESS='SEQUENTIAL', 'DIRECT', 'STREAM'
29 bool isUnformatted
{false}; // FORM='UNFORMATTED'
30 bool isUTF8
{false}; // ENCODING='UTF-8'
31 bool isFixedRecordLength
{false}; // RECL= on OPEN
32 std::optional
<std::int64_t> recordLength
; // RECL= or current record
35 struct ConnectionState
: public ConnectionAttributes
{
36 bool IsAtEOF() const; // true when read has hit EOF or endfile record
37 std::size_t RemainingSpaceInRecord() const;
38 void HandleAbsolutePosition(std::int64_t);
39 void HandleRelativePosition(std::int64_t);
43 furthestPositionInRecord
= 0;
47 // Positions in a record file (sequential or direct, not stream)
48 std::int64_t currentRecordNumber
{1}; // 1 is first
49 std::int64_t positionInRecord
{0}; // offset in current record
50 std::int64_t furthestPositionInRecord
{0}; // max(position+bytes)
51 bool nonAdvancing
{false}; // ADVANCE='NO'
53 // Set at end of non-advancing I/O data transfer
54 std::optional
<std::int64_t> leftTabLimit
; // offset in current record
56 // currentRecordNumber value captured after ENDFILE/REWIND/BACKSPACE statement
57 // or an end-of-file READ condition on a sequential access file
58 std::optional
<std::int64_t> endfileRecordNumber
;
60 // Mutable modes set at OPEN() that can be overridden in READ/WRITE & FORMAT
61 MutableModes modes
; // BLANK=, DECIMAL=, SIGN=, ROUND=, PAD=, DELIM=, kP
63 } // namespace Fortran::runtime::io
64 #endif // FORTRAN_RUNTIME_IO_CONNECTION_H_