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
{
15 RT_OFFLOAD_API_GROUP_BEGIN
17 RT_API_ATTRS
std::size_t ConnectionState::RemainingSpaceInRecord() const {
18 auto recl
{recordLength
.value_or(openRecl
.value_or(
19 executionEnvironment
.listDirectedOutputLineLengthLimit
))};
20 return positionInRecord
>= recl
? 0 : recl
- positionInRecord
;
23 RT_API_ATTRS
bool ConnectionState::NeedAdvance(std::size_t width
) const {
24 return positionInRecord
> 0 && width
> RemainingSpaceInRecord();
27 RT_API_ATTRS
bool ConnectionState::IsAtEOF() const {
28 return endfileRecordNumber
&& currentRecordNumber
>= *endfileRecordNumber
;
31 RT_API_ATTRS
bool ConnectionState::IsAfterEndfile() const {
32 return endfileRecordNumber
&& currentRecordNumber
> *endfileRecordNumber
;
35 RT_API_ATTRS
void ConnectionState::HandleAbsolutePosition(std::int64_t n
) {
36 positionInRecord
= std::max(n
, std::int64_t{0}) + leftTabLimit
.value_or(0);
39 RT_API_ATTRS
void ConnectionState::HandleRelativePosition(std::int64_t n
) {
40 positionInRecord
= std::max(leftTabLimit
.value_or(0), positionInRecord
+ n
);
43 SavedPosition::SavedPosition(IoStatementState
&io
) : io_
{io
} {
44 ConnectionState
&conn
{io_
.GetConnectionState()};
46 conn
.pinnedFrame
= true;
49 SavedPosition::~SavedPosition() {
51 ConnectionState
&conn
{io_
.GetConnectionState()};
52 while (conn
.currentRecordNumber
> saved_
.currentRecordNumber
) {
53 io_
.BackspaceRecord();
55 conn
.leftTabLimit
= saved_
.leftTabLimit
;
56 conn
.furthestPositionInRecord
= saved_
.furthestPositionInRecord
;
57 conn
.positionInRecord
= saved_
.positionInRecord
;
58 conn
.pinnedFrame
= saved_
.pinnedFrame
;
62 RT_OFFLOAD_API_GROUP_END
63 } // namespace Fortran::runtime::io