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 
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   std::optional<std::int64_t> recordLength; // RECL= when fixed-length
30   bool isUnformatted{false}; // FORM='UNFORMATTED'
31   bool isUTF8{false}; // ENCODING='UTF-8'
32 };
33 
34 struct ConnectionState : public ConnectionAttributes {
35   bool IsAtEOF() const; // true when read has hit EOF or endfile record
36   std::size_t RemainingSpaceInRecord() const;
37   void HandleAbsolutePosition(std::int64_t);
38   void HandleRelativePosition(std::int64_t);
39 
40   // Positions in a record file (sequential or direct, not stream)
41   std::int64_t currentRecordNumber{1}; // 1 is first
42   std::int64_t positionInRecord{0}; // offset in current record
43   std::int64_t furthestPositionInRecord{0}; // max(positionInRecord)
44   bool nonAdvancing{false}; // ADVANCE='NO'
45 
46   // Set at end of non-advancing I/O data transfer
47   std::optional<std::int64_t> leftTabLimit; // offset in current record
48 
49   // currentRecordNumber value captured after ENDFILE/REWIND/BACKSPACE statement
50   // or an end-of-file READ condition on a sequential access file
51   std::optional<std::int64_t> endfileRecordNumber;
52 
53   // Mutable modes set at OPEN() that can be overridden in READ/WRITE & FORMAT
54   MutableModes modes; // BLANK=, DECIMAL=, SIGN=, ROUND=, PAD=, DELIM=, kP
55 };
56 } // namespace Fortran::runtime::io
57 #endif // FORTRAN_RUNTIME_IO_CONNECTION_H_
58