2021-09-02 16:14:01 +08:00
|
|
|
//===-- runtime/io-stmt.cpp -----------------------------------------------===//
|
2020-01-17 05:51:25 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "io-stmt.h"
|
2020-02-05 08:55:45 +08:00
|
|
|
#include "connection.h"
|
|
|
|
#include "format.h"
|
|
|
|
#include "tools.h"
|
2020-01-24 08:59:27 +08:00
|
|
|
#include "unit.h"
|
2021-09-02 07:00:53 +08:00
|
|
|
#include "flang/Runtime/memory.h"
|
2020-01-17 05:51:25 +08:00
|
|
|
#include <algorithm>
|
2020-07-04 08:57:28 +08:00
|
|
|
#include <cstdio>
|
2020-01-17 05:51:25 +08:00
|
|
|
#include <cstring>
|
2020-02-05 08:55:45 +08:00
|
|
|
#include <limits>
|
2020-01-17 05:51:25 +08:00
|
|
|
|
|
|
|
namespace Fortran::runtime::io {
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
int IoStatementBase::EndIoStatement() { return GetIoStat(); }
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2021-06-26 01:40:08 +08:00
|
|
|
bool IoStatementBase::Emit(const char *, std::size_t, std::size_t) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-17 02:25:28 +08:00
|
|
|
bool IoStatementBase::Emit(const char *, std::size_t) { return false; }
|
2021-06-26 01:40:08 +08:00
|
|
|
|
2021-11-17 02:25:28 +08:00
|
|
|
bool IoStatementBase::Emit(const char16_t *, std::size_t) { return false; }
|
2021-06-26 01:40:08 +08:00
|
|
|
|
2021-11-17 02:25:28 +08:00
|
|
|
bool IoStatementBase::Emit(const char32_t *, std::size_t) { return false; }
|
2021-06-26 01:40:08 +08:00
|
|
|
|
2021-11-03 04:01:38 +08:00
|
|
|
std::size_t IoStatementBase::GetNextInputBytes(const char *&p) {
|
|
|
|
p = nullptr;
|
|
|
|
return 0;
|
2021-06-26 01:40:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IoStatementBase::AdvanceRecord(int) { return false; }
|
|
|
|
|
|
|
|
void IoStatementBase::BackspaceRecord() {}
|
|
|
|
|
|
|
|
bool IoStatementBase::Receive(char *, std::size_t, std::size_t) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
std::optional<DataEdit> IoStatementBase::GetNextDataEdit(
|
|
|
|
IoStatementState &, int) {
|
|
|
|
return std::nullopt;
|
2020-01-17 05:51:25 +08:00
|
|
|
}
|
|
|
|
|
2021-06-26 01:40:08 +08:00
|
|
|
ExternalFileUnit *IoStatementBase::GetExternalFileUnit() const {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IoStatementBase::BeginReadingRecord() { return true; }
|
|
|
|
|
|
|
|
void IoStatementBase::FinishReadingRecord() {}
|
|
|
|
|
|
|
|
void IoStatementBase::HandleAbsolutePosition(std::int64_t) {}
|
|
|
|
|
|
|
|
void IoStatementBase::HandleRelativePosition(std::int64_t) {}
|
|
|
|
|
2020-08-04 02:35:29 +08:00
|
|
|
bool IoStatementBase::Inquire(InquiryKeywordHash, char *, std::size_t) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-17 02:25:28 +08:00
|
|
|
bool IoStatementBase::Inquire(InquiryKeywordHash, bool &) { return false; }
|
2020-08-04 02:35:29 +08:00
|
|
|
|
|
|
|
bool IoStatementBase::Inquire(InquiryKeywordHash, std::int64_t, bool &) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IoStatementBase::Inquire(InquiryKeywordHash, std::int64_t &) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IoStatementBase::BadInquiryKeywordHashCrash(InquiryKeywordHash inquiry) {
|
|
|
|
char buffer[16];
|
|
|
|
const char *decode{InquiryKeywordHashDecode(buffer, sizeof buffer, inquiry)};
|
|
|
|
Crash("bad InquiryKeywordHash 0x%x (%s)", inquiry,
|
|
|
|
decode ? decode : "(cannot decode)");
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
InternalIoStatementState<DIR, CHAR>::InternalIoStatementState(
|
2020-02-05 08:55:45 +08:00
|
|
|
Buffer scalar, std::size_t length, const char *sourceFile, int sourceLine)
|
2020-03-29 12:00:16 +08:00
|
|
|
: IoStatementBase{sourceFile, sourceLine}, unit_{scalar, length} {}
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
InternalIoStatementState<DIR, CHAR>::InternalIoStatementState(
|
2020-02-05 08:55:45 +08:00
|
|
|
const Descriptor &d, const char *sourceFile, int sourceLine)
|
2020-03-29 12:00:16 +08:00
|
|
|
: IoStatementBase{sourceFile, sourceLine}, unit_{d, *this} {}
|
2020-01-17 05:51:25 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
bool InternalIoStatementState<DIR, CHAR>::Emit(
|
2021-06-26 01:40:08 +08:00
|
|
|
const CharType *data, std::size_t chars) {
|
2020-02-14 06:41:56 +08:00
|
|
|
if constexpr (DIR == Direction::Input) {
|
|
|
|
Crash("InternalIoStatementState<Direction::Input>::Emit() called");
|
2020-01-24 08:59:27 +08:00
|
|
|
return false;
|
2020-01-17 05:51:25 +08:00
|
|
|
}
|
2021-06-26 01:40:08 +08:00
|
|
|
return unit_.Emit(data, chars * sizeof(CharType), *this);
|
2020-01-17 05:51:25 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2021-11-03 04:01:38 +08:00
|
|
|
std::size_t InternalIoStatementState<DIR, CHAR>::GetNextInputBytes(
|
|
|
|
const char *&p) {
|
|
|
|
return unit_.GetNextInputBytes(p, *this);
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
bool InternalIoStatementState<DIR, CHAR>::AdvanceRecord(int n) {
|
2020-02-05 08:55:45 +08:00
|
|
|
while (n-- > 0) {
|
|
|
|
if (!unit_.AdvanceRecord(*this)) {
|
|
|
|
return false;
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
return true;
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
void InternalIoStatementState<DIR, CHAR>::BackspaceRecord() {
|
|
|
|
unit_.BackspaceRecord(*this);
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
int InternalIoStatementState<DIR, CHAR>::EndIoStatement() {
|
|
|
|
if constexpr (DIR == Direction::Output) {
|
2020-03-29 12:00:16 +08:00
|
|
|
unit_.EndIoStatement(); // fill
|
2020-01-17 05:51:25 +08:00
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
auto result{IoStatementBase::EndIoStatement()};
|
2020-01-17 05:51:25 +08:00
|
|
|
if (free_) {
|
|
|
|
FreeMemory(this);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
void InternalIoStatementState<DIR, CHAR>::HandleAbsolutePosition(
|
|
|
|
std::int64_t n) {
|
|
|
|
return unit_.HandleAbsolutePosition(n);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
void InternalIoStatementState<DIR, CHAR>::HandleRelativePosition(
|
2020-02-05 08:55:45 +08:00
|
|
|
std::int64_t n) {
|
2020-02-14 06:41:56 +08:00
|
|
|
return unit_.HandleRelativePosition(n);
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
InternalFormattedIoStatementState<DIR, CHAR>::InternalFormattedIoStatementState(
|
|
|
|
Buffer buffer, std::size_t length, const CHAR *format,
|
|
|
|
std::size_t formatLength, const char *sourceFile, int sourceLine)
|
2020-03-29 12:00:16 +08:00
|
|
|
: InternalIoStatementState<DIR, CHAR>{buffer, length, sourceFile,
|
|
|
|
sourceLine},
|
|
|
|
ioStatementState_{*this}, format_{*this, format, formatLength} {}
|
2020-02-14 06:41:56 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
InternalFormattedIoStatementState<DIR, CHAR>::InternalFormattedIoStatementState(
|
|
|
|
const Descriptor &d, const CHAR *format, std::size_t formatLength,
|
|
|
|
const char *sourceFile, int sourceLine)
|
2020-03-29 12:00:16 +08:00
|
|
|
: InternalIoStatementState<DIR, CHAR>{d, sourceFile, sourceLine},
|
|
|
|
ioStatementState_{*this}, format_{*this, format, formatLength} {}
|
2020-02-14 06:41:56 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
int InternalFormattedIoStatementState<DIR, CHAR>::EndIoStatement() {
|
|
|
|
if constexpr (DIR == Direction::Output) {
|
2020-03-29 12:00:16 +08:00
|
|
|
format_.Finish(*this); // ignore any remaining input positioning actions
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
return InternalIoStatementState<DIR, CHAR>::EndIoStatement();
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
InternalListIoStatementState<DIR, CHAR>::InternalListIoStatementState(
|
2020-02-05 08:55:45 +08:00
|
|
|
Buffer buffer, std::size_t length, const char *sourceFile, int sourceLine)
|
2020-03-29 12:00:16 +08:00
|
|
|
: InternalIoStatementState<DIR, CharType>{buffer, length, sourceFile,
|
|
|
|
sourceLine},
|
|
|
|
ioStatementState_{*this} {}
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
InternalListIoStatementState<DIR, CHAR>::InternalListIoStatementState(
|
2020-02-05 08:55:45 +08:00
|
|
|
const Descriptor &d, const char *sourceFile, int sourceLine)
|
2020-03-29 12:00:16 +08:00
|
|
|
: InternalIoStatementState<DIR, CharType>{d, sourceFile, sourceLine},
|
|
|
|
ioStatementState_{*this} {}
|
2020-02-05 08:55:45 +08:00
|
|
|
|
|
|
|
ExternalIoStatementBase::ExternalIoStatementBase(
|
|
|
|
ExternalFileUnit &unit, const char *sourceFile, int sourceLine)
|
2020-03-29 12:00:16 +08:00
|
|
|
: IoStatementBase{sourceFile, sourceLine}, unit_{unit} {}
|
2020-02-05 08:55:45 +08:00
|
|
|
|
|
|
|
MutableModes &ExternalIoStatementBase::mutableModes() { return unit_.modes; }
|
|
|
|
|
|
|
|
ConnectionState &ExternalIoStatementBase::GetConnectionState() { return unit_; }
|
|
|
|
|
|
|
|
int ExternalIoStatementBase::EndIoStatement() {
|
2021-06-29 02:41:04 +08:00
|
|
|
if (mutableModes().nonAdvancing) {
|
2020-02-05 08:55:45 +08:00
|
|
|
unit_.leftTabLimit = unit_.furthestPositionInRecord;
|
|
|
|
} else {
|
|
|
|
unit_.leftTabLimit.reset();
|
|
|
|
}
|
|
|
|
auto result{IoStatementBase::EndIoStatement()};
|
2020-03-29 12:00:16 +08:00
|
|
|
unit_.EndIoStatement(); // annihilates *this in unit_.u_
|
2020-02-05 08:55:45 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-04 02:35:29 +08:00
|
|
|
void OpenStatementState::set_path(const char *path, std::size_t length) {
|
|
|
|
pathLength_ = TrimTrailingSpaces(path, length);
|
|
|
|
path_ = SaveDefaultCharacter(path, pathLength_, *this);
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int OpenStatementState::EndIoStatement() {
|
2020-08-04 02:35:29 +08:00
|
|
|
if (path_.get() || wasExtant_ ||
|
|
|
|
(status_ && *status_ == OpenStatus::Scratch)) {
|
2021-04-13 08:05:05 +08:00
|
|
|
unit().OpenUnit(status_, action_, position_, std::move(path_), pathLength_,
|
|
|
|
convert_, *this);
|
2020-08-04 02:35:29 +08:00
|
|
|
} else {
|
2021-04-13 08:05:05 +08:00
|
|
|
unit().OpenAnonymousUnit(status_, action_, position_, convert_, *this);
|
2020-08-04 02:35:29 +08:00
|
|
|
}
|
|
|
|
if (access_) {
|
|
|
|
if (*access_ != unit().access) {
|
|
|
|
if (wasExtant_) {
|
|
|
|
SignalError("ACCESS= may not be changed on an open unit");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unit().access = *access_;
|
|
|
|
}
|
2021-05-06 02:33:00 +08:00
|
|
|
if (!unit().isUnformatted) {
|
|
|
|
unit().isUnformatted = isUnformatted_;
|
2020-08-04 02:35:29 +08:00
|
|
|
}
|
2021-05-06 02:33:00 +08:00
|
|
|
if (isUnformatted_ && *isUnformatted_ != *unit().isUnformatted) {
|
2020-08-04 02:35:29 +08:00
|
|
|
if (wasExtant_) {
|
|
|
|
SignalError("FORM= may not be changed on an open unit");
|
|
|
|
}
|
|
|
|
unit().isUnformatted = *isUnformatted_;
|
|
|
|
}
|
2021-09-01 14:39:39 +08:00
|
|
|
if (!unit().isUnformatted) {
|
|
|
|
// Set default format (C.7.4 point 2).
|
|
|
|
unit().isUnformatted = unit().access != Access::Sequential;
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
return ExternalIoStatementBase::EndIoStatement();
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CloseStatementState::EndIoStatement() {
|
2020-02-14 06:41:56 +08:00
|
|
|
int result{ExternalIoStatementBase::EndIoStatement()};
|
2020-02-05 08:55:45 +08:00
|
|
|
unit().CloseUnit(status_, *this);
|
2020-02-14 06:41:56 +08:00
|
|
|
unit().DestroyClosed();
|
|
|
|
return result;
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
|
|
|
|
2020-08-04 02:35:29 +08:00
|
|
|
int NoUnitIoStatementState::EndIoStatement() {
|
2020-02-05 08:55:45 +08:00
|
|
|
auto result{IoStatementBase::EndIoStatement()};
|
|
|
|
FreeMemory(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-29 02:41:04 +08:00
|
|
|
template <Direction DIR>
|
|
|
|
ExternalIoStatementState<DIR>::ExternalIoStatementState(
|
|
|
|
ExternalFileUnit &unit, const char *sourceFile, int sourceLine)
|
|
|
|
: ExternalIoStatementBase{unit, sourceFile, sourceLine}, mutableModes_{
|
2021-11-16 21:52:35 +08:00
|
|
|
unit.modes} {
|
|
|
|
if constexpr (DIR == Direction::Output) {
|
|
|
|
// If the last statement was a non advancing IO input statement, the unit
|
|
|
|
// furthestPositionInRecord was not advanced, but the positionInRecord may
|
|
|
|
// have been advanced. Advance furthestPositionInRecord here to avoid
|
|
|
|
// overwriting the part of the record that has been read with blanks.
|
|
|
|
unit.furthestPositionInRecord =
|
|
|
|
std::max(unit.furthestPositionInRecord, unit.positionInRecord);
|
|
|
|
}
|
|
|
|
}
|
2021-06-29 02:41:04 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR> int ExternalIoStatementState<DIR>::EndIoStatement() {
|
2020-08-04 02:31:13 +08:00
|
|
|
if constexpr (DIR == Direction::Input) {
|
2021-04-14 07:07:58 +08:00
|
|
|
BeginReadingRecord(); // in case there were no I/O items
|
2021-09-10 19:07:13 +08:00
|
|
|
if (!mutableModes().nonAdvancing || GetIoStat() == IostatEor) {
|
2020-10-01 03:53:00 +08:00
|
|
|
FinishReadingRecord();
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-29 02:41:04 +08:00
|
|
|
if (!mutableModes().nonAdvancing) {
|
2020-10-01 03:53:00 +08:00
|
|
|
unit().AdvanceRecord(*this);
|
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
unit().FlushIfTerminal(*this);
|
|
|
|
}
|
|
|
|
return ExternalIoStatementBase::EndIoStatement();
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR>
|
2020-07-22 08:37:35 +08:00
|
|
|
bool ExternalIoStatementState<DIR>::Emit(
|
|
|
|
const char *data, std::size_t bytes, std::size_t elementBytes) {
|
2020-02-14 06:41:56 +08:00
|
|
|
if constexpr (DIR == Direction::Input) {
|
|
|
|
Crash("ExternalIoStatementState::Emit(char) called for input statement");
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
2020-07-22 08:37:35 +08:00
|
|
|
return unit().Emit(data, bytes, elementBytes, *this);
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
|
|
|
|
2021-06-26 01:40:08 +08:00
|
|
|
template <Direction DIR>
|
|
|
|
bool ExternalIoStatementState<DIR>::Emit(const char *data, std::size_t bytes) {
|
|
|
|
if constexpr (DIR == Direction::Input) {
|
|
|
|
Crash("ExternalIoStatementState::Emit(char) called for input statement");
|
|
|
|
}
|
|
|
|
return unit().Emit(data, bytes, 0, *this);
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR>
|
2020-02-14 06:41:56 +08:00
|
|
|
bool ExternalIoStatementState<DIR>::Emit(
|
2020-02-05 08:55:45 +08:00
|
|
|
const char16_t *data, std::size_t chars) {
|
2020-02-14 06:41:56 +08:00
|
|
|
if constexpr (DIR == Direction::Input) {
|
|
|
|
Crash(
|
|
|
|
"ExternalIoStatementState::Emit(char16_t) called for input statement");
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
|
|
|
// TODO: UTF-8 encoding
|
2020-07-22 08:37:35 +08:00
|
|
|
return unit().Emit(reinterpret_cast<const char *>(data), chars * sizeof *data,
|
2021-06-26 01:40:08 +08:00
|
|
|
sizeof *data, *this);
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR>
|
2020-02-14 06:41:56 +08:00
|
|
|
bool ExternalIoStatementState<DIR>::Emit(
|
2020-02-05 08:55:45 +08:00
|
|
|
const char32_t *data, std::size_t chars) {
|
2020-02-14 06:41:56 +08:00
|
|
|
if constexpr (DIR == Direction::Input) {
|
|
|
|
Crash(
|
|
|
|
"ExternalIoStatementState::Emit(char32_t) called for input statement");
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
|
|
|
// TODO: UTF-8 encoding
|
2020-07-22 08:37:35 +08:00
|
|
|
return unit().Emit(reinterpret_cast<const char *>(data), chars * sizeof *data,
|
2021-06-26 01:40:08 +08:00
|
|
|
sizeof *data, *this);
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR>
|
2021-11-03 04:01:38 +08:00
|
|
|
std::size_t ExternalIoStatementState<DIR>::GetNextInputBytes(const char *&p) {
|
|
|
|
return unit().GetNextInputBytes(p, *this);
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR>
|
2020-02-14 06:41:56 +08:00
|
|
|
bool ExternalIoStatementState<DIR>::AdvanceRecord(int n) {
|
2020-01-24 08:59:27 +08:00
|
|
|
while (n-- > 0) {
|
2020-02-05 08:55:45 +08:00
|
|
|
if (!unit().AdvanceRecord(*this)) {
|
2020-01-24 08:59:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR> void ExternalIoStatementState<DIR>::BackspaceRecord() {
|
2020-02-14 06:41:56 +08:00
|
|
|
unit().BackspaceRecord(*this);
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR>
|
2020-02-14 06:41:56 +08:00
|
|
|
void ExternalIoStatementState<DIR>::HandleAbsolutePosition(std::int64_t n) {
|
|
|
|
return unit().HandleAbsolutePosition(n);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR>
|
2020-02-14 06:41:56 +08:00
|
|
|
void ExternalIoStatementState<DIR>::HandleRelativePosition(std::int64_t n) {
|
|
|
|
return unit().HandleRelativePosition(n);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
|
2020-08-04 02:31:13 +08:00
|
|
|
template <Direction DIR>
|
2021-04-14 07:07:58 +08:00
|
|
|
bool ExternalIoStatementState<DIR>::BeginReadingRecord() {
|
2020-08-04 02:31:13 +08:00
|
|
|
if constexpr (DIR == Direction::Input) {
|
2021-04-14 07:07:58 +08:00
|
|
|
return unit().BeginReadingRecord(*this);
|
2020-10-01 03:53:00 +08:00
|
|
|
} else {
|
|
|
|
Crash("ExternalIoStatementState<Direction::Output>::BeginReadingRecord() "
|
|
|
|
"called");
|
2021-04-14 07:07:58 +08:00
|
|
|
return false;
|
2020-10-01 03:53:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
|
|
|
void ExternalIoStatementState<DIR>::FinishReadingRecord() {
|
|
|
|
if constexpr (DIR == Direction::Input) {
|
|
|
|
unit().FinishReadingRecord(*this);
|
|
|
|
} else {
|
|
|
|
Crash("ExternalIoStatementState<Direction::Output>::FinishReadingRecord() "
|
|
|
|
"called");
|
2020-08-04 02:31:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
ExternalFormattedIoStatementState<DIR, CHAR>::ExternalFormattedIoStatementState(
|
|
|
|
ExternalFileUnit &unit, const CHAR *format, std::size_t formatLength,
|
|
|
|
const char *sourceFile, int sourceLine)
|
2020-03-29 12:00:16 +08:00
|
|
|
: ExternalIoStatementState<DIR>{unit, sourceFile, sourceLine},
|
2021-06-29 02:41:04 +08:00
|
|
|
format_{*this, format, formatLength} {}
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
int ExternalFormattedIoStatementState<DIR, CHAR>::EndIoStatement() {
|
2021-10-23 03:22:42 +08:00
|
|
|
if constexpr (DIR == Direction::Input) {
|
|
|
|
this->BeginReadingRecord(); // in case there were no I/O items
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
format_.Finish(*this);
|
|
|
|
return ExternalIoStatementState<DIR>::EndIoStatement();
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
std::optional<DataEdit> IoStatementState::GetNextDataEdit(int n) {
|
|
|
|
return std::visit(
|
|
|
|
[&](auto &x) { return x.get().GetNextDataEdit(*this, n); }, u_);
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 08:37:35 +08:00
|
|
|
bool IoStatementState::Emit(
|
|
|
|
const char *data, std::size_t n, std::size_t elementBytes) {
|
|
|
|
return std::visit(
|
|
|
|
[=](auto &x) { return x.get().Emit(data, n, elementBytes); }, u_);
|
2020-02-05 08:55:45 +08:00
|
|
|
}
|
|
|
|
|
2021-06-26 01:40:08 +08:00
|
|
|
bool IoStatementState::Emit(const char *data, std::size_t n) {
|
|
|
|
return std::visit([=](auto &x) { return x.get().Emit(data, n); }, u_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IoStatementState::Emit(const char16_t *data, std::size_t chars) {
|
|
|
|
return std::visit([=](auto &x) { return x.get().Emit(data, chars); }, u_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IoStatementState::Emit(const char32_t *data, std::size_t chars) {
|
|
|
|
return std::visit([=](auto &x) { return x.get().Emit(data, chars); }, u_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IoStatementState::Receive(
|
|
|
|
char *data, std::size_t n, std::size_t elementBytes) {
|
|
|
|
return std::visit(
|
|
|
|
[=](auto &x) { return x.get().Receive(data, n, elementBytes); }, u_);
|
|
|
|
}
|
|
|
|
|
2021-11-03 04:01:38 +08:00
|
|
|
std::size_t IoStatementState::GetNextInputBytes(const char *&p) {
|
|
|
|
return std::visit([&](auto &x) { return x.get().GetNextInputBytes(p); }, u_);
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
bool IoStatementState::AdvanceRecord(int n) {
|
|
|
|
return std::visit([=](auto &x) { return x.get().AdvanceRecord(n); }, u_);
|
|
|
|
}
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
void IoStatementState::BackspaceRecord() {
|
|
|
|
std::visit([](auto &x) { x.get().BackspaceRecord(); }, u_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IoStatementState::HandleRelativePosition(std::int64_t n) {
|
2020-07-04 08:57:28 +08:00
|
|
|
std::visit([=](auto &x) { x.get().HandleRelativePosition(n); }, u_);
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
|
2021-06-26 01:40:08 +08:00
|
|
|
void IoStatementState::HandleAbsolutePosition(std::int64_t n) {
|
|
|
|
std::visit([=](auto &x) { x.get().HandleAbsolutePosition(n); }, u_);
|
|
|
|
}
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
int IoStatementState::EndIoStatement() {
|
|
|
|
return std::visit([](auto &x) { return x.get().EndIoStatement(); }, u_);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionState &IoStatementState::GetConnectionState() {
|
|
|
|
return std::visit(
|
|
|
|
[](auto &x) -> ConnectionState & { return x.get().GetConnectionState(); },
|
|
|
|
u_);
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableModes &IoStatementState::mutableModes() {
|
|
|
|
return std::visit(
|
|
|
|
[](auto &x) -> MutableModes & { return x.get().mutableModes(); }, u_);
|
|
|
|
}
|
|
|
|
|
2021-04-14 07:07:58 +08:00
|
|
|
bool IoStatementState::BeginReadingRecord() {
|
|
|
|
return std::visit([](auto &x) { return x.get().BeginReadingRecord(); }, u_);
|
2020-08-04 02:31:13 +08:00
|
|
|
}
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
IoErrorHandler &IoStatementState::GetIoErrorHandler() const {
|
|
|
|
return std::visit(
|
|
|
|
[](auto &x) -> IoErrorHandler & {
|
|
|
|
return static_cast<IoErrorHandler &>(x.get());
|
|
|
|
},
|
|
|
|
u_);
|
|
|
|
}
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
ExternalFileUnit *IoStatementState::GetExternalFileUnit() const {
|
|
|
|
return std::visit([](auto &x) { return x.get().GetExternalFileUnit(); }, u_);
|
|
|
|
}
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
bool IoStatementState::EmitRepeated(char ch, std::size_t n) {
|
|
|
|
return std::visit(
|
|
|
|
[=](auto &x) {
|
|
|
|
for (std::size_t j{0}; j < n; ++j) {
|
|
|
|
if (!x.get().Emit(&ch, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
u_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IoStatementState::EmitField(
|
|
|
|
const char *p, std::size_t length, std::size_t width) {
|
|
|
|
if (width <= 0) {
|
|
|
|
width = static_cast<int>(length);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
if (length > static_cast<std::size_t>(width)) {
|
|
|
|
return EmitRepeated('*', width);
|
|
|
|
} else {
|
|
|
|
return EmitRepeated(' ', static_cast<int>(width - length)) &&
|
|
|
|
Emit(p, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 02:35:29 +08:00
|
|
|
bool IoStatementState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, char *out, std::size_t chars) {
|
|
|
|
return std::visit(
|
|
|
|
[&](auto &x) { return x.get().Inquire(inquiry, out, chars); }, u_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IoStatementState::Inquire(InquiryKeywordHash inquiry, bool &out) {
|
|
|
|
return std::visit([&](auto &x) { return x.get().Inquire(inquiry, out); }, u_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IoStatementState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, std::int64_t id, bool &out) {
|
|
|
|
return std::visit(
|
|
|
|
[&](auto &x) { return x.get().Inquire(inquiry, id, out); }, u_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IoStatementState::Inquire(InquiryKeywordHash inquiry, std::int64_t &n) {
|
|
|
|
return std::visit([&](auto &x) { return x.get().Inquire(inquiry, n); }, u_);
|
|
|
|
}
|
|
|
|
|
2021-09-21 01:52:39 +08:00
|
|
|
void IoStatementState::GotChar(int n) {
|
|
|
|
if (auto *formattedIn{
|
|
|
|
get_if<FormattedIoStatementState<Direction::Input>>()}) {
|
|
|
|
formattedIn->GotChar(n);
|
|
|
|
} else {
|
|
|
|
GetIoErrorHandler().Crash("IoStatementState::GotChar() called for "
|
|
|
|
"statement that is not formatted input");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t
|
|
|
|
FormattedIoStatementState<Direction::Input>::GetEditDescriptorChars() const {
|
|
|
|
return chars_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormattedIoStatementState<Direction::Input>::GotChar(int n) {
|
|
|
|
chars_ += n;
|
|
|
|
}
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
bool ListDirectedStatementState<Direction::Output>::EmitLeadingSpaceOrAdvance(
|
2020-02-05 08:55:45 +08:00
|
|
|
IoStatementState &io, std::size_t length, bool isCharacter) {
|
|
|
|
if (length == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const ConnectionState &connection{io.GetConnectionState()};
|
|
|
|
int space{connection.positionInRecord == 0 ||
|
2021-05-06 02:37:49 +08:00
|
|
|
!(isCharacter && lastWasUndelimitedCharacter())};
|
|
|
|
set_lastWasUndelimitedCharacter(false);
|
|
|
|
if (connection.NeedAdvance(space + length)) {
|
2020-02-05 08:55:45 +08:00
|
|
|
return io.AdvanceRecord();
|
|
|
|
}
|
|
|
|
if (space) {
|
|
|
|
return io.Emit(" ", 1);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
std::optional<DataEdit>
|
|
|
|
ListDirectedStatementState<Direction::Output>::GetNextDataEdit(
|
|
|
|
IoStatementState &io, int maxRepeat) {
|
|
|
|
DataEdit edit;
|
|
|
|
edit.descriptor = DataEdit::ListDirected;
|
|
|
|
edit.repeat = maxRepeat;
|
|
|
|
edit.modes = io.mutableModes();
|
|
|
|
return edit;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<DataEdit>
|
|
|
|
ListDirectedStatementState<Direction::Input>::GetNextDataEdit(
|
|
|
|
IoStatementState &io, int maxRepeat) {
|
|
|
|
// N.B. list-directed transfers cannot be nonadvancing (C1221)
|
|
|
|
ConnectionState &connection{io.GetConnectionState()};
|
|
|
|
DataEdit edit;
|
|
|
|
edit.descriptor = DataEdit::ListDirected;
|
2020-03-29 12:00:16 +08:00
|
|
|
edit.repeat = 1; // may be overridden below
|
2020-02-14 06:41:56 +08:00
|
|
|
edit.modes = connection.modes;
|
2020-03-29 12:00:16 +08:00
|
|
|
if (hitSlash_) { // everything after '/' is nullified
|
2020-02-14 06:41:56 +08:00
|
|
|
edit.descriptor = DataEdit::ListDirectedNullValue;
|
|
|
|
return edit;
|
|
|
|
}
|
2020-07-15 03:31:16 +08:00
|
|
|
char32_t comma{','};
|
|
|
|
if (io.mutableModes().editingFlags & decimalComma) {
|
|
|
|
comma = ';';
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
if (remaining_ > 0 && !realPart_) { // "r*c" repetition in progress
|
2021-11-04 06:33:29 +08:00
|
|
|
RUNTIME_CHECK(io.GetIoErrorHandler(), repeatPosition_.has_value());
|
|
|
|
repeatPosition_.reset(); // restores the saved position
|
2020-02-14 06:41:56 +08:00
|
|
|
if (!imaginaryPart_) {
|
|
|
|
edit.repeat = std::min<int>(remaining_, maxRepeat);
|
2021-08-11 04:13:50 +08:00
|
|
|
auto ch{io.GetCurrentChar()};
|
2020-07-18 07:40:49 +08:00
|
|
|
if (!ch || *ch == ' ' || *ch == '\t' || *ch == comma) {
|
|
|
|
// "r*" repeated null
|
2020-07-15 03:31:16 +08:00
|
|
|
edit.descriptor = DataEdit::ListDirectedNullValue;
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
remaining_ -= edit.repeat;
|
2021-11-04 06:33:29 +08:00
|
|
|
if (remaining_ > 0) {
|
|
|
|
repeatPosition_.emplace(io);
|
2021-08-17 04:15:01 +08:00
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
return edit;
|
|
|
|
}
|
|
|
|
// Skip separators, handle a "r*c" repeat count; see 13.10.2 in Fortran 2018
|
|
|
|
if (imaginaryPart_) {
|
|
|
|
imaginaryPart_ = false;
|
|
|
|
} else if (realPart_) {
|
|
|
|
realPart_ = false;
|
|
|
|
imaginaryPart_ = true;
|
2020-07-23 08:01:22 +08:00
|
|
|
edit.descriptor = DataEdit::ListDirectedImaginaryPart;
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
2021-08-11 04:13:50 +08:00
|
|
|
auto ch{io.GetNextNonBlank()};
|
|
|
|
if (ch && *ch == comma && eatComma_) {
|
|
|
|
// Consume comma & whitespace after previous item.
|
|
|
|
// This includes the comma between real and imaginary components
|
|
|
|
// in list-directed/NAMELIST complex input.
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
ch = io.GetNextNonBlank();
|
|
|
|
}
|
|
|
|
eatComma_ = true;
|
2020-02-14 06:41:56 +08:00
|
|
|
if (!ch) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
if (*ch == '/') {
|
|
|
|
hitSlash_ = true;
|
|
|
|
edit.descriptor = DataEdit::ListDirectedNullValue;
|
|
|
|
return edit;
|
|
|
|
}
|
2021-08-11 04:13:50 +08:00
|
|
|
if (*ch == comma) { // separator: null value
|
|
|
|
edit.descriptor = DataEdit::ListDirectedNullValue;
|
|
|
|
return edit;
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
if (imaginaryPart_) { // can't repeat components
|
2020-02-14 06:41:56 +08:00
|
|
|
return edit;
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
if (*ch >= '0' && *ch <= '9') { // look for "r*" repetition count
|
2020-02-14 06:41:56 +08:00
|
|
|
auto start{connection.positionInRecord};
|
|
|
|
int r{0};
|
|
|
|
do {
|
|
|
|
static auto constexpr clamp{(std::numeric_limits<int>::max() - '9') / 10};
|
|
|
|
if (r >= clamp) {
|
|
|
|
r = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
r = 10 * r + (*ch - '0');
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
ch = io.GetCurrentChar();
|
|
|
|
} while (ch && *ch >= '0' && *ch <= '9');
|
2020-03-29 12:00:16 +08:00
|
|
|
if (r > 0 && ch && *ch == '*') { // subtle: r must be nonzero
|
2020-02-14 06:41:56 +08:00
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
ch = io.GetCurrentChar();
|
2020-07-15 03:31:16 +08:00
|
|
|
if (ch && *ch == '/') { // r*/
|
|
|
|
hitSlash_ = true;
|
2020-02-14 06:41:56 +08:00
|
|
|
edit.descriptor = DataEdit::ListDirectedNullValue;
|
|
|
|
return edit;
|
|
|
|
}
|
2020-07-18 07:40:49 +08:00
|
|
|
if (!ch || *ch == ' ' || *ch == '\t' || *ch == comma) { // "r*" null
|
2020-07-15 03:31:16 +08:00
|
|
|
edit.descriptor = DataEdit::ListDirectedNullValue;
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
edit.repeat = std::min<int>(r, maxRepeat);
|
|
|
|
remaining_ = r - edit.repeat;
|
2021-08-17 04:15:01 +08:00
|
|
|
if (remaining_ > 0) {
|
2021-11-04 06:33:29 +08:00
|
|
|
repeatPosition_.emplace(io);
|
2021-08-17 04:15:01 +08:00
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
} else { // not a repetition count, just an integer value; rewind
|
2020-02-14 06:41:56 +08:00
|
|
|
connection.positionInRecord = start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!imaginaryPart_ && ch && *ch == '(') {
|
|
|
|
realPart_ = true;
|
|
|
|
io.HandleRelativePosition(1);
|
2020-07-23 08:01:22 +08:00
|
|
|
edit.descriptor = DataEdit::ListDirectedRealPart;
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
return edit;
|
|
|
|
}
|
|
|
|
|
2020-07-04 08:57:28 +08:00
|
|
|
template <Direction DIR>
|
2021-06-26 01:40:08 +08:00
|
|
|
bool ExternalUnformattedIoStatementState<DIR>::Receive(
|
2020-07-22 08:37:35 +08:00
|
|
|
char *data, std::size_t bytes, std::size_t elementBytes) {
|
2020-07-04 08:57:28 +08:00
|
|
|
if constexpr (DIR == Direction::Output) {
|
2021-06-26 01:40:08 +08:00
|
|
|
this->Crash("ExternalUnformattedIoStatementState::Receive() called for "
|
|
|
|
"output statement");
|
2020-07-04 08:57:28 +08:00
|
|
|
}
|
2020-07-22 08:37:35 +08:00
|
|
|
return this->unit().Receive(data, bytes, elementBytes, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
2021-06-26 01:40:08 +08:00
|
|
|
ChildIoStatementState<DIR>::ChildIoStatementState(
|
|
|
|
ChildIo &child, const char *sourceFile, int sourceLine)
|
|
|
|
: IoStatementBase{sourceFile, sourceLine}, child_{child} {}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
|
|
|
MutableModes &ChildIoStatementState<DIR>::mutableModes() {
|
|
|
|
return child_.parent().mutableModes();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
|
|
|
ConnectionState &ChildIoStatementState<DIR>::GetConnectionState() {
|
|
|
|
return child_.parent().GetConnectionState();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
|
|
|
ExternalFileUnit *ChildIoStatementState<DIR>::GetExternalFileUnit() const {
|
|
|
|
return child_.parent().GetExternalFileUnit();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR> int ChildIoStatementState<DIR>::EndIoStatement() {
|
|
|
|
auto result{IoStatementBase::EndIoStatement()};
|
|
|
|
child_.EndIoStatement(); // annihilates *this in child_.u_
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
|
|
|
bool ChildIoStatementState<DIR>::Emit(
|
2020-07-22 08:37:35 +08:00
|
|
|
const char *data, std::size_t bytes, std::size_t elementBytes) {
|
2021-06-26 01:40:08 +08:00
|
|
|
return child_.parent().Emit(data, bytes, elementBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
|
|
|
bool ChildIoStatementState<DIR>::Emit(const char *data, std::size_t bytes) {
|
|
|
|
return child_.parent().Emit(data, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
|
|
|
bool ChildIoStatementState<DIR>::Emit(const char16_t *data, std::size_t chars) {
|
|
|
|
return child_.parent().Emit(data, chars);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
|
|
|
bool ChildIoStatementState<DIR>::Emit(const char32_t *data, std::size_t chars) {
|
|
|
|
return child_.parent().Emit(data, chars);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
2021-11-03 04:01:38 +08:00
|
|
|
std::size_t ChildIoStatementState<DIR>::GetNextInputBytes(const char *&p) {
|
|
|
|
return child_.parent().GetNextInputBytes(p);
|
2021-06-26 01:40:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
|
|
|
void ChildIoStatementState<DIR>::HandleAbsolutePosition(std::int64_t n) {
|
|
|
|
return child_.parent().HandleAbsolutePosition(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
|
|
|
void ChildIoStatementState<DIR>::HandleRelativePosition(std::int64_t n) {
|
|
|
|
return child_.parent().HandleRelativePosition(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR, typename CHAR>
|
|
|
|
ChildFormattedIoStatementState<DIR, CHAR>::ChildFormattedIoStatementState(
|
|
|
|
ChildIo &child, const CHAR *format, std::size_t formatLength,
|
|
|
|
const char *sourceFile, int sourceLine)
|
|
|
|
: ChildIoStatementState<DIR>{child, sourceFile, sourceLine},
|
|
|
|
mutableModes_{child.parent().mutableModes()}, format_{*this, format,
|
|
|
|
formatLength} {}
|
|
|
|
|
|
|
|
template <Direction DIR, typename CHAR>
|
|
|
|
int ChildFormattedIoStatementState<DIR, CHAR>::EndIoStatement() {
|
|
|
|
format_.Finish(*this);
|
|
|
|
return ChildIoStatementState<DIR>::EndIoStatement();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR, typename CHAR>
|
|
|
|
bool ChildFormattedIoStatementState<DIR, CHAR>::AdvanceRecord(int) {
|
|
|
|
return false; // no can do in a child I/O
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Direction DIR>
|
|
|
|
bool ChildUnformattedIoStatementState<DIR>::Receive(
|
|
|
|
char *data, std::size_t bytes, std::size_t elementBytes) {
|
|
|
|
return this->child().parent().Receive(data, bytes, elementBytes);
|
2020-07-04 08:57:28 +08:00
|
|
|
}
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
template class InternalIoStatementState<Direction::Output>;
|
|
|
|
template class InternalIoStatementState<Direction::Input>;
|
|
|
|
template class InternalFormattedIoStatementState<Direction::Output>;
|
|
|
|
template class InternalFormattedIoStatementState<Direction::Input>;
|
|
|
|
template class InternalListIoStatementState<Direction::Output>;
|
|
|
|
template class InternalListIoStatementState<Direction::Input>;
|
|
|
|
template class ExternalIoStatementState<Direction::Output>;
|
|
|
|
template class ExternalIoStatementState<Direction::Input>;
|
|
|
|
template class ExternalFormattedIoStatementState<Direction::Output>;
|
|
|
|
template class ExternalFormattedIoStatementState<Direction::Input>;
|
|
|
|
template class ExternalListIoStatementState<Direction::Output>;
|
|
|
|
template class ExternalListIoStatementState<Direction::Input>;
|
2021-06-26 01:40:08 +08:00
|
|
|
template class ExternalUnformattedIoStatementState<Direction::Output>;
|
|
|
|
template class ExternalUnformattedIoStatementState<Direction::Input>;
|
|
|
|
template class ChildIoStatementState<Direction::Output>;
|
|
|
|
template class ChildIoStatementState<Direction::Input>;
|
|
|
|
template class ChildFormattedIoStatementState<Direction::Output>;
|
|
|
|
template class ChildFormattedIoStatementState<Direction::Input>;
|
|
|
|
template class ChildListIoStatementState<Direction::Output>;
|
|
|
|
template class ChildListIoStatementState<Direction::Input>;
|
|
|
|
template class ChildUnformattedIoStatementState<Direction::Output>;
|
|
|
|
template class ChildUnformattedIoStatementState<Direction::Input>;
|
2020-07-04 08:57:28 +08:00
|
|
|
|
|
|
|
int ExternalMiscIoStatementState::EndIoStatement() {
|
|
|
|
ExternalFileUnit &ext{unit()};
|
|
|
|
switch (which_) {
|
|
|
|
case Flush:
|
2021-06-29 02:41:04 +08:00
|
|
|
ext.FlushOutput(*this);
|
2020-07-04 08:57:28 +08:00
|
|
|
std::fflush(nullptr); // flushes C stdio output streams (12.9(2))
|
|
|
|
break;
|
|
|
|
case Backspace:
|
|
|
|
ext.BackspaceRecord(*this);
|
|
|
|
break;
|
|
|
|
case Endfile:
|
|
|
|
ext.Endfile(*this);
|
|
|
|
break;
|
|
|
|
case Rewind:
|
|
|
|
ext.Rewind(*this);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ExternalIoStatementBase::EndIoStatement();
|
|
|
|
}
|
|
|
|
|
2020-08-04 02:35:29 +08:00
|
|
|
InquireUnitState::InquireUnitState(
|
|
|
|
ExternalFileUnit &unit, const char *sourceFile, int sourceLine)
|
|
|
|
: ExternalIoStatementBase{unit, sourceFile, sourceLine} {}
|
|
|
|
|
|
|
|
bool InquireUnitState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, char *result, std::size_t length) {
|
2021-06-26 01:40:08 +08:00
|
|
|
if (unit().createdForInternalChildIo()) {
|
|
|
|
SignalError(IostatInquireInternalUnit,
|
|
|
|
"INQUIRE of unit created for defined derived type I/O of an internal "
|
|
|
|
"unit");
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-04 02:35:29 +08:00
|
|
|
const char *str{nullptr};
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("ACCESS"):
|
|
|
|
switch (unit().access) {
|
|
|
|
case Access::Sequential:
|
|
|
|
str = "SEQUENTIAL";
|
|
|
|
break;
|
|
|
|
case Access::Direct:
|
|
|
|
str = "DIRECT";
|
|
|
|
break;
|
|
|
|
case Access::Stream:
|
|
|
|
str = "STREAM";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("ACTION"):
|
|
|
|
str = unit().mayWrite() ? unit().mayRead() ? "READWRITE" : "WRITE" : "READ";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("ASYNCHRONOUS"):
|
|
|
|
str = unit().mayAsynchronous() ? "YES" : "NO";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("BLANK"):
|
2021-05-06 02:33:00 +08:00
|
|
|
str = unit().isUnformatted.value_or(true) ? "UNDEFINED"
|
2020-08-04 02:35:29 +08:00
|
|
|
: unit().modes.editingFlags & blankZero ? "ZERO"
|
|
|
|
: "NULL";
|
|
|
|
break;
|
2020-09-03 01:37:48 +08:00
|
|
|
case HashInquiryKeyword("CARRIAGECONTROL"):
|
|
|
|
str = "LIST";
|
|
|
|
break;
|
2020-08-04 02:35:29 +08:00
|
|
|
case HashInquiryKeyword("CONVERT"):
|
|
|
|
str = unit().swapEndianness() ? "SWAP" : "NATIVE";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("DECIMAL"):
|
2021-05-06 02:33:00 +08:00
|
|
|
str = unit().isUnformatted.value_or(true) ? "UNDEFINED"
|
2020-08-04 02:35:29 +08:00
|
|
|
: unit().modes.editingFlags & decimalComma ? "COMMA"
|
|
|
|
: "POINT";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("DELIM"):
|
2021-05-06 02:33:00 +08:00
|
|
|
if (unit().isUnformatted.value_or(true)) {
|
2020-08-04 02:35:29 +08:00
|
|
|
str = "UNDEFINED";
|
|
|
|
} else {
|
|
|
|
switch (unit().modes.delim) {
|
|
|
|
case '\'':
|
|
|
|
str = "APOSTROPHE";
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
str = "QUOTE";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
str = "NONE";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("DIRECT"):
|
2020-10-02 00:44:09 +08:00
|
|
|
str = unit().access == Access::Direct ||
|
|
|
|
(unit().mayPosition() && unit().isFixedRecordLength)
|
|
|
|
? "YES"
|
|
|
|
: "NO";
|
2020-08-04 02:35:29 +08:00
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("ENCODING"):
|
2021-05-06 02:33:00 +08:00
|
|
|
str = unit().isUnformatted.value_or(true) ? "UNDEFINED"
|
|
|
|
: unit().isUTF8 ? "UTF-8"
|
|
|
|
: "ASCII";
|
2020-08-04 02:35:29 +08:00
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("FORM"):
|
2021-09-01 14:39:39 +08:00
|
|
|
str = !unit().isUnformatted ? "UNDEFINED"
|
2021-05-06 02:33:00 +08:00
|
|
|
: *unit().isUnformatted ? "UNFORMATTED"
|
|
|
|
: "FORMATTED";
|
2020-08-04 02:35:29 +08:00
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("FORMATTED"):
|
2021-05-06 02:33:00 +08:00
|
|
|
str = !unit().isUnformatted ? "UNKNOWN"
|
|
|
|
: *unit().isUnformatted ? "NO"
|
|
|
|
: "YES";
|
2020-08-04 02:35:29 +08:00
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("NAME"):
|
|
|
|
str = unit().path();
|
|
|
|
if (!str) {
|
|
|
|
return true; // result is undefined
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("PAD"):
|
2021-05-06 02:33:00 +08:00
|
|
|
str = unit().isUnformatted.value_or(true) ? "UNDEFINED"
|
|
|
|
: unit().modes.pad ? "YES"
|
|
|
|
: "NO";
|
2020-08-04 02:35:29 +08:00
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("POSITION"):
|
|
|
|
if (unit().access == Access::Direct) {
|
|
|
|
str = "UNDEFINED";
|
|
|
|
} else {
|
|
|
|
auto size{unit().knownSize()};
|
|
|
|
auto pos{unit().position()};
|
|
|
|
if (pos == size.value_or(pos + 1)) {
|
|
|
|
str = "APPEND";
|
2021-11-16 08:04:02 +08:00
|
|
|
} else if (pos == 0 && unit().mayPosition()) {
|
2020-08-04 02:35:29 +08:00
|
|
|
str = "REWIND";
|
|
|
|
} else {
|
|
|
|
str = "ASIS"; // processor-dependent & no common behavior
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("READ"):
|
|
|
|
str = unit().mayRead() ? "YES" : "NO";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("READWRITE"):
|
|
|
|
str = unit().mayRead() && unit().mayWrite() ? "YES" : "NO";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("ROUND"):
|
2021-05-06 02:33:00 +08:00
|
|
|
if (unit().isUnformatted.value_or(true)) {
|
2020-08-04 02:35:29 +08:00
|
|
|
str = "UNDEFINED";
|
|
|
|
} else {
|
|
|
|
switch (unit().modes.round) {
|
|
|
|
case decimal::FortranRounding::RoundNearest:
|
|
|
|
str = "NEAREST";
|
|
|
|
break;
|
|
|
|
case decimal::FortranRounding::RoundUp:
|
|
|
|
str = "UP";
|
|
|
|
break;
|
|
|
|
case decimal::FortranRounding::RoundDown:
|
|
|
|
str = "DOWN";
|
|
|
|
break;
|
|
|
|
case decimal::FortranRounding::RoundToZero:
|
|
|
|
str = "ZERO";
|
|
|
|
break;
|
|
|
|
case decimal::FortranRounding::RoundCompatible:
|
|
|
|
str = "COMPATIBLE";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("SEQUENTIAL"):
|
2020-10-02 00:44:09 +08:00
|
|
|
// "NO" for Direct, since Sequential would not work if
|
|
|
|
// the unit were reopened without RECL=.
|
|
|
|
str = unit().access == Access::Sequential ? "YES" : "NO";
|
2020-08-04 02:35:29 +08:00
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("SIGN"):
|
2021-05-06 02:33:00 +08:00
|
|
|
str = unit().isUnformatted.value_or(true) ? "UNDEFINED"
|
2020-08-04 02:35:29 +08:00
|
|
|
: unit().modes.editingFlags & signPlus ? "PLUS"
|
|
|
|
: "SUPPRESS";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("STREAM"):
|
2020-10-02 00:44:09 +08:00
|
|
|
str = unit().access == Access::Stream ? "YES" : "NO";
|
2020-08-04 02:35:29 +08:00
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("WRITE"):
|
|
|
|
str = unit().mayWrite() ? "YES" : "NO";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("UNFORMATTED"):
|
2021-05-06 02:33:00 +08:00
|
|
|
str = !unit().isUnformatted ? "UNKNOWN"
|
|
|
|
: *unit().isUnformatted ? "YES"
|
|
|
|
: "NO";
|
2020-08-04 02:35:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (str) {
|
|
|
|
ToFortranDefaultCharacter(result, length, str);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireUnitState::Inquire(InquiryKeywordHash inquiry, bool &result) {
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("EXIST"):
|
|
|
|
result = true;
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("NAMED"):
|
|
|
|
result = unit().path() != nullptr;
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("OPENED"):
|
|
|
|
result = true;
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("PENDING"):
|
|
|
|
result = false; // asynchronous I/O is not implemented
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireUnitState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, std::int64_t, bool &result) {
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("PENDING"):
|
|
|
|
result = false; // asynchronous I/O is not implemented
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireUnitState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, std::int64_t &result) {
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("NEXTREC"):
|
|
|
|
if (unit().access == Access::Direct) {
|
|
|
|
result = unit().currentRecordNumber;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("NUMBER"):
|
|
|
|
result = unit().unitNumber();
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("POS"):
|
|
|
|
result = unit().position();
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("RECL"):
|
|
|
|
if (unit().access == Access::Stream) {
|
|
|
|
result = -2;
|
|
|
|
} else if (unit().isFixedRecordLength && unit().recordLength) {
|
|
|
|
result = *unit().recordLength;
|
|
|
|
} else {
|
|
|
|
result = std::numeric_limits<std::uint32_t>::max();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("SIZE"):
|
|
|
|
if (auto size{unit().knownSize()}) {
|
|
|
|
result = *size;
|
|
|
|
} else {
|
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InquireNoUnitState::InquireNoUnitState(const char *sourceFile, int sourceLine)
|
|
|
|
: NoUnitIoStatementState{sourceFile, sourceLine, *this} {}
|
|
|
|
|
|
|
|
bool InquireNoUnitState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, char *result, std::size_t length) {
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("ACCESS"):
|
|
|
|
case HashInquiryKeyword("ACTION"):
|
|
|
|
case HashInquiryKeyword("ASYNCHRONOUS"):
|
|
|
|
case HashInquiryKeyword("BLANK"):
|
2020-09-03 01:37:48 +08:00
|
|
|
case HashInquiryKeyword("CARRIAGECONTROL"):
|
2020-08-04 02:35:29 +08:00
|
|
|
case HashInquiryKeyword("CONVERT"):
|
|
|
|
case HashInquiryKeyword("DECIMAL"):
|
|
|
|
case HashInquiryKeyword("DELIM"):
|
|
|
|
case HashInquiryKeyword("FORM"):
|
|
|
|
case HashInquiryKeyword("NAME"):
|
|
|
|
case HashInquiryKeyword("PAD"):
|
|
|
|
case HashInquiryKeyword("POSITION"):
|
|
|
|
case HashInquiryKeyword("ROUND"):
|
|
|
|
case HashInquiryKeyword("SIGN"):
|
|
|
|
ToFortranDefaultCharacter(result, length, "UNDEFINED");
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("DIRECT"):
|
|
|
|
case HashInquiryKeyword("ENCODING"):
|
|
|
|
case HashInquiryKeyword("FORMATTED"):
|
|
|
|
case HashInquiryKeyword("READ"):
|
|
|
|
case HashInquiryKeyword("READWRITE"):
|
|
|
|
case HashInquiryKeyword("SEQUENTIAL"):
|
|
|
|
case HashInquiryKeyword("STREAM"):
|
|
|
|
case HashInquiryKeyword("WRITE"):
|
|
|
|
case HashInquiryKeyword("UNFORMATTED"):
|
|
|
|
ToFortranDefaultCharacter(result, length, "UNKNONN");
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireNoUnitState::Inquire(InquiryKeywordHash inquiry, bool &result) {
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("EXIST"):
|
|
|
|
result = true;
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("NAMED"):
|
|
|
|
case HashInquiryKeyword("OPENED"):
|
|
|
|
case HashInquiryKeyword("PENDING"):
|
|
|
|
result = false;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireNoUnitState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, std::int64_t, bool &result) {
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("PENDING"):
|
|
|
|
result = false;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireNoUnitState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, std::int64_t &result) {
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("NEXTREC"):
|
|
|
|
case HashInquiryKeyword("NUMBER"):
|
|
|
|
case HashInquiryKeyword("POS"):
|
|
|
|
case HashInquiryKeyword("RECL"):
|
|
|
|
case HashInquiryKeyword("SIZE"):
|
|
|
|
result = -1;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InquireUnconnectedFileState::InquireUnconnectedFileState(
|
|
|
|
OwningPtr<char> &&path, const char *sourceFile, int sourceLine)
|
|
|
|
: NoUnitIoStatementState{sourceFile, sourceLine, *this}, path_{std::move(
|
|
|
|
path)} {}
|
|
|
|
|
|
|
|
bool InquireUnconnectedFileState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, char *result, std::size_t length) {
|
|
|
|
const char *str{nullptr};
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("ACCESS"):
|
|
|
|
case HashInquiryKeyword("ACTION"):
|
|
|
|
case HashInquiryKeyword("ASYNCHRONOUS"):
|
|
|
|
case HashInquiryKeyword("BLANK"):
|
2020-09-03 01:37:48 +08:00
|
|
|
case HashInquiryKeyword("CARRIAGECONTROL"):
|
2020-08-04 02:35:29 +08:00
|
|
|
case HashInquiryKeyword("CONVERT"):
|
|
|
|
case HashInquiryKeyword("DECIMAL"):
|
|
|
|
case HashInquiryKeyword("DELIM"):
|
|
|
|
case HashInquiryKeyword("FORM"):
|
|
|
|
case HashInquiryKeyword("PAD"):
|
|
|
|
case HashInquiryKeyword("POSITION"):
|
|
|
|
case HashInquiryKeyword("ROUND"):
|
|
|
|
case HashInquiryKeyword("SIGN"):
|
|
|
|
str = "UNDEFINED";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("DIRECT"):
|
|
|
|
case HashInquiryKeyword("ENCODING"):
|
2020-10-02 00:44:09 +08:00
|
|
|
case HashInquiryKeyword("FORMATTED"):
|
|
|
|
case HashInquiryKeyword("SEQUENTIAL"):
|
|
|
|
case HashInquiryKeyword("STREAM"):
|
|
|
|
case HashInquiryKeyword("UNFORMATTED"):
|
2020-08-04 02:35:29 +08:00
|
|
|
str = "UNKNONN";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("READ"):
|
|
|
|
str = MayRead(path_.get()) ? "YES" : "NO";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("READWRITE"):
|
|
|
|
str = MayReadAndWrite(path_.get()) ? "YES" : "NO";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("WRITE"):
|
|
|
|
str = MayWrite(path_.get()) ? "YES" : "NO";
|
|
|
|
break;
|
|
|
|
case HashInquiryKeyword("NAME"):
|
|
|
|
str = path_.get();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (str) {
|
|
|
|
ToFortranDefaultCharacter(result, length, str);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireUnconnectedFileState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, bool &result) {
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("EXIST"):
|
|
|
|
result = IsExtant(path_.get());
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("NAMED"):
|
|
|
|
result = true;
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("OPENED"):
|
|
|
|
result = false;
|
|
|
|
return true;
|
|
|
|
case HashInquiryKeyword("PENDING"):
|
|
|
|
result = false;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireUnconnectedFileState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, std::int64_t, bool &result) {
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("PENDING"):
|
|
|
|
result = false;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireUnconnectedFileState::Inquire(
|
|
|
|
InquiryKeywordHash inquiry, std::int64_t &result) {
|
|
|
|
switch (inquiry) {
|
|
|
|
case HashInquiryKeyword("NEXTREC"):
|
|
|
|
case HashInquiryKeyword("NUMBER"):
|
|
|
|
case HashInquiryKeyword("POS"):
|
|
|
|
case HashInquiryKeyword("RECL"):
|
|
|
|
case HashInquiryKeyword("SIZE"):
|
|
|
|
result = -1;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
BadInquiryKeywordHashCrash(inquiry);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InquireIOLengthState::InquireIOLengthState(
|
|
|
|
const char *sourceFile, int sourceLine)
|
|
|
|
: NoUnitIoStatementState{sourceFile, sourceLine, *this} {}
|
|
|
|
|
2021-11-17 02:25:28 +08:00
|
|
|
bool InquireIOLengthState::Emit(const char *, std::size_t n, std::size_t) {
|
|
|
|
bytes_ += n;
|
2021-09-21 01:52:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireIOLengthState::Emit(const char *p, std::size_t n) {
|
|
|
|
bytes_ += sizeof *p * n;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireIOLengthState::Emit(const char16_t *p, std::size_t n) {
|
|
|
|
bytes_ += sizeof *p * n;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InquireIOLengthState::Emit(const char32_t *p, std::size_t n) {
|
|
|
|
bytes_ += sizeof *p * n;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime::io
|