173
|
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'
|
221
|
29 std::optional<bool> isUnformatted; // FORM='UNFORMATTED' if true
|
173
|
30 bool isUTF8{false}; // ENCODING='UTF-8'
|
221
|
31 bool isFixedRecordLength{false}; // RECL= on OPEN
|
|
32 std::optional<std::int64_t> recordLength; // RECL= or current record
|
173
|
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;
|
221
|
38 bool NeedAdvance(std::size_t) const;
|
173
|
39 void HandleAbsolutePosition(std::int64_t);
|
|
40 void HandleRelativePosition(std::int64_t);
|
|
41
|
221
|
42 void BeginRecord() {
|
|
43 positionInRecord = 0;
|
|
44 furthestPositionInRecord = 0;
|
|
45 leftTabLimit.reset();
|
|
46 }
|
|
47
|
173
|
48 // Positions in a record file (sequential or direct, not stream)
|
|
49 std::int64_t currentRecordNumber{1}; // 1 is first
|
|
50 std::int64_t positionInRecord{0}; // offset in current record
|
221
|
51 std::int64_t furthestPositionInRecord{0}; // max(position+bytes)
|
173
|
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_
|