1 //===-- runtime/connection.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "connection.h"
10 #include "environment.h"
14 namespace Fortran::runtime::io
{
16 std::size_t ConnectionState::RemainingSpaceInRecord() const {
17 auto recl
{recordLength
.value_or(openRecl
.value_or(
18 executionEnvironment
.listDirectedOutputLineLengthLimit
))};
19 return positionInRecord
>= recl
? 0 : recl
- positionInRecord
;
22 bool ConnectionState::NeedAdvance(std::size_t width
) const {
23 return positionInRecord
> 0 && width
> RemainingSpaceInRecord();
26 bool ConnectionState::IsAtEOF() const {
27 return endfileRecordNumber
&& currentRecordNumber
>= *endfileRecordNumber
;
30 bool ConnectionState::IsAfterEndfile() const {
31 return endfileRecordNumber
&& currentRecordNumber
> *endfileRecordNumber
;
34 void ConnectionState::HandleAbsolutePosition(std::int64_t n
) {
35 positionInRecord
= std::max(n
, std::int64_t{0}) + leftTabLimit
.value_or(0);
38 void ConnectionState::HandleRelativePosition(std::int64_t n
) {
39 positionInRecord
= std::max(leftTabLimit
.value_or(0), positionInRecord
+ n
);
42 SavedPosition::SavedPosition(IoStatementState
&io
) : io_
{io
} {
43 ConnectionState
&conn
{io_
.GetConnectionState()};
45 conn
.pinnedFrame
= true;
48 SavedPosition::~SavedPosition() {
49 ConnectionState
&conn
{io_
.GetConnectionState()};
50 while (conn
.currentRecordNumber
> saved_
.currentRecordNumber
) {
51 io_
.BackspaceRecord();
53 conn
.leftTabLimit
= saved_
.leftTabLimit
;
54 conn
.furthestPositionInRecord
= saved_
.furthestPositionInRecord
;
55 conn
.positionInRecord
= saved_
.positionInRecord
;
56 conn
.pinnedFrame
= saved_
.pinnedFrame
;
58 } // namespace Fortran::runtime::io