1 //===-- runtime/connection.h ------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // Fortran I/O connection state (abstracted over internal & external units)
10 
11 #ifndef FORTRAN_RUNTIME_IO_CONNECTION_H_
12 #define FORTRAN_RUNTIME_IO_CONNECTION_H_
13 
14 #include "format.h"
15 #include <cinttypes>
16 #include <optional>
17 
18 namespace Fortran::runtime::io {
19 
20 enum class Direction { Output, Input };
21 enum class Access { Sequential, Direct, Stream };
22 
IsRecordFile(Access a)23 inline bool IsRecordFile(Access a) { return a != Access::Stream; }
24 
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
33 };
34 
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);
40 
BeginRecordConnectionState41   void BeginRecord() {
42     positionInRecord = 0;
43     furthestPositionInRecord = 0;
44     leftTabLimit.reset();
45   }
46 
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'
52 
53   // Set at end of non-advancing I/O data transfer
54   std::optional<std::int64_t> leftTabLimit; // offset in current record
55 
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;
59 
60   // Mutable modes set at OPEN() that can be overridden in READ/WRITE & FORMAT
61   MutableModes modes; // BLANK=, DECIMAL=, SIGN=, ROUND=, PAD=, DELIM=, kP
62 };
63 } // namespace Fortran::runtime::io
64 #endif // FORTRAN_RUNTIME_IO_CONNECTION_H_
65