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 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; 20 } 21 22 bool ConnectionState::NeedAdvance(std::size_t width) const { 23 return positionInRecord > 0 && width > RemainingSpaceInRecord(); 24 } 25 26 bool ConnectionState::IsAtEOF() const { 27 return endfileRecordNumber && currentRecordNumber >= *endfileRecordNumber; 28 } 29 30 bool ConnectionState::IsAfterEndfile() const { 31 return endfileRecordNumber && currentRecordNumber > *endfileRecordNumber; 32 } 33 34 void ConnectionState::HandleAbsolutePosition(std::int64_t n) { 35 positionInRecord = std::max(n, std::int64_t{0}) + leftTabLimit.value_or(0); 36 } 37 38 void ConnectionState::HandleRelativePosition(std::int64_t n) { 39 positionInRecord = std::max(leftTabLimit.value_or(0), positionInRecord + n); 40 } 41 42 SavedPosition::SavedPosition(IoStatementState &io) : io_{io} { 43 ConnectionState &conn{io_.GetConnectionState()}; 44 saved_ = conn; 45 conn.pinnedFrame = true; 46 } 47 48 SavedPosition::~SavedPosition() { 49 ConnectionState &conn{io_.GetConnectionState()}; 50 while (conn.currentRecordNumber > saved_.currentRecordNumber) { 51 io_.BackspaceRecord(); 52 } 53 conn.leftTabLimit = saved_.leftTabLimit; 54 conn.furthestPositionInRecord = saved_.furthestPositionInRecord; 55 conn.positionInRecord = saved_.positionInRecord; 56 conn.pinnedFrame = saved_.pinnedFrame; 57 } 58 } // namespace Fortran::runtime::io 59