2020-02-05 08:55:45 +08:00
|
|
|
//===-- runtime/connection.h ------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
// Fortran I/O connection state (abstracted over internal & external units)
|
2020-02-05 08:55:45 +08:00
|
|
|
|
|
|
|
#ifndef FORTRAN_RUNTIME_IO_CONNECTION_H_
|
|
|
|
#define FORTRAN_RUNTIME_IO_CONNECTION_H_
|
|
|
|
|
|
|
|
#include "format.h"
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Fortran::runtime::io {
|
|
|
|
|
2021-11-04 06:33:29 +08:00
|
|
|
class IoStatementState;
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
enum class Direction { Output, Input };
|
2020-02-05 08:55:45 +08:00
|
|
|
enum class Access { Sequential, Direct, Stream };
|
|
|
|
|
|
|
|
// These characteristics of a connection are immutable after being
|
2021-12-03 08:36:09 +08:00
|
|
|
// established in an OPEN statement.
|
2020-02-05 08:55:45 +08:00
|
|
|
struct ConnectionAttributes {
|
2020-03-29 12:00:16 +08:00
|
|
|
Access access{Access::Sequential}; // ACCESS='SEQUENTIAL', 'DIRECT', 'STREAM'
|
2021-05-06 02:33:00 +08:00
|
|
|
std::optional<bool> isUnformatted; // FORM='UNFORMATTED' if true
|
2020-03-29 12:00:16 +08:00
|
|
|
bool isUTF8{false}; // ENCODING='UTF-8'
|
2021-12-03 08:36:09 +08:00
|
|
|
std::optional<std::int64_t> openRecl; // RECL= on OPEN
|
2022-01-29 07:34:28 +08:00
|
|
|
|
|
|
|
bool IsRecordFile() const {
|
|
|
|
// Formatted stream files are viewed as having records, at least on input
|
|
|
|
return access != Access::Stream || !isUnformatted.value_or(true);
|
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ConnectionState : public ConnectionAttributes {
|
2020-03-29 12:00:16 +08:00
|
|
|
bool IsAtEOF() const; // true when read has hit EOF or endfile record
|
2022-01-08 02:29:23 +08:00
|
|
|
bool IsAfterEndfile() const; // true after ENDFILE until repositioned
|
2020-02-05 08:55:45 +08:00
|
|
|
std::size_t RemainingSpaceInRecord() const;
|
2021-05-06 02:37:49 +08:00
|
|
|
bool NeedAdvance(std::size_t) const;
|
2020-02-14 06:41:56 +08:00
|
|
|
void HandleAbsolutePosition(std::int64_t);
|
|
|
|
void HandleRelativePosition(std::int64_t);
|
|
|
|
|
2020-07-03 05:11:14 +08:00
|
|
|
void BeginRecord() {
|
|
|
|
positionInRecord = 0;
|
|
|
|
furthestPositionInRecord = 0;
|
|
|
|
leftTabLimit.reset();
|
|
|
|
}
|
|
|
|
|
2021-12-03 08:36:09 +08:00
|
|
|
std::optional<std::int64_t> EffectiveRecordLength() const {
|
|
|
|
// When an input record is longer than an explicit RECL= from OPEN
|
|
|
|
// it is effectively truncated on input.
|
|
|
|
return openRecl && recordLength && *openRecl < *recordLength ? openRecl
|
|
|
|
: recordLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::int64_t> recordLength;
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
// Positions in a record file (sequential or direct, not stream)
|
2020-03-29 12:00:16 +08:00
|
|
|
std::int64_t currentRecordNumber{1}; // 1 is first
|
|
|
|
std::int64_t positionInRecord{0}; // offset in current record
|
2020-06-19 03:19:49 +08:00
|
|
|
std::int64_t furthestPositionInRecord{0}; // max(position+bytes)
|
2020-02-14 06:41:56 +08:00
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
// Set at end of non-advancing I/O data transfer
|
2020-03-29 12:00:16 +08:00
|
|
|
std::optional<std::int64_t> leftTabLimit; // offset in current record
|
2020-02-14 06:41:56 +08:00
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
// currentRecordNumber value captured after ENDFILE/REWIND/BACKSPACE statement
|
2020-02-14 06:41:56 +08:00
|
|
|
// or an end-of-file READ condition on a sequential access file
|
2020-02-05 08:55:45 +08:00
|
|
|
std::optional<std::int64_t> endfileRecordNumber;
|
2020-02-14 06:41:56 +08:00
|
|
|
|
2021-11-04 06:33:29 +08:00
|
|
|
// Mutable modes set at OPEN() that can be overridden in READ/WRITE & FORMAT
|
|
|
|
MutableModes modes; // BLANK=, DECIMAL=, SIGN=, ROUND=, PAD=, DELIM=, kP
|
|
|
|
|
2021-08-17 04:15:01 +08:00
|
|
|
// Set when processing repeated items during list-directed & NAMELIST input
|
|
|
|
// in order to keep a span of records in frame on a non-positionable file,
|
|
|
|
// so that backspacing to the beginning of the repeated item doesn't require
|
|
|
|
// repositioning the external storage medium when that's impossible.
|
2021-11-04 06:33:29 +08:00
|
|
|
bool pinnedFrame{false};
|
2020-02-05 08:55:45 +08:00
|
|
|
};
|
2021-10-21 04:56:47 +08:00
|
|
|
|
|
|
|
// Utility class for capturing and restoring a position in an input stream.
|
|
|
|
class SavedPosition {
|
|
|
|
public:
|
2021-11-04 06:33:29 +08:00
|
|
|
explicit SavedPosition(IoStatementState &);
|
|
|
|
~SavedPosition();
|
2021-10-21 04:56:47 +08:00
|
|
|
|
|
|
|
private:
|
2021-11-04 06:33:29 +08:00
|
|
|
IoStatementState &io_;
|
|
|
|
ConnectionState saved_;
|
2021-10-21 04:56:47 +08:00
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime::io
|
|
|
|
#endif // FORTRAN_RUNTIME_IO_CONNECTION_H_
|