| 1 |
//===-- runtime/connection.cpp --------------------------------------------===// |
| 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 |
#include "connection.h" |
| 10 |
#include "environment.h" |
| 11 |
#include "io-stmt.h" |
| 12 |
#include <algorithm> |
| 13 |
|
| 14 |
namespace Fortran::runtime::io { |
| 15 |
RT_OFFLOAD_API_GROUP_BEGIN |
| 16 |
|
| 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; |
| 21 |
} |
| 22 |
|
| 23 |
RT_API_ATTRS bool ConnectionState::NeedAdvance(std::size_t width) const { |
| 24 |
return positionInRecord > 0 && width > RemainingSpaceInRecord(); |
| 25 |
} |
| 26 |
|
| 27 |
RT_API_ATTRS bool ConnectionState::IsAtEOF() const { |
| 28 |
return endfileRecordNumber && currentRecordNumber >= *endfileRecordNumber; |
| 29 |
} |
| 30 |
|
| 31 |
RT_API_ATTRS bool ConnectionState::IsAfterEndfile() const { |
| 32 |
return endfileRecordNumber && currentRecordNumber > *endfileRecordNumber; |
| 33 |
} |
| 34 |
|
| 35 |
RT_API_ATTRS void ConnectionState::HandleAbsolutePosition(std::int64_t n) { |
| 36 |
positionInRecord = std::max(n, std::int64_t{0}) + leftTabLimit.value_or(0); |
| 37 |
} |
| 38 |
|
| 39 |
RT_API_ATTRS void ConnectionState::HandleRelativePosition(std::int64_t n) { |
| 40 |
positionInRecord = std::max(leftTabLimit.value_or(0), positionInRecord + n); |
| 41 |
} |
| 42 |
|
| 43 |
SavedPosition::SavedPosition(IoStatementState &io) : io_{io} { |
| 44 |
ConnectionState &conn{io_.GetConnectionState()}; |
| 45 |
saved_ = conn; |
| 46 |
conn.pinnedFrame = true; |
| 47 |
} |
| 48 |
|
| 49 |
SavedPosition::~SavedPosition() { |
| 50 |
if (!cancelled_) { |
| 51 |
ConnectionState &conn{io_.GetConnectionState()}; |
| 52 |
while (conn.currentRecordNumber > saved_.currentRecordNumber) { |
| 53 |
io_.BackspaceRecord(); |
| 54 |
} |
| 55 |
conn.leftTabLimit = saved_.leftTabLimit; |
| 56 |
conn.furthestPositionInRecord = saved_.furthestPositionInRecord; |
| 57 |
conn.positionInRecord = saved_.positionInRecord; |
| 58 |
conn.pinnedFrame = saved_.pinnedFrame; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
RT_OFFLOAD_API_GROUP_END |
| 63 |
} // namespace Fortran::runtime::io |