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 <algorithm>
12 
13 namespace Fortran::runtime::io {
14 
15 std::size_t ConnectionState::RemainingSpaceInRecord() const {
16   auto recl{recordLength.value_or(
17       executionEnvironment.listDirectedOutputLineLengthLimit)};
18   return positionInRecord >= recl ? 0 : recl - positionInRecord;
19 }
20 
21 bool ConnectionState::NeedAdvance(std::size_t width) const {
22   return positionInRecord > 0 && width > RemainingSpaceInRecord();
23 }
24 
25 bool ConnectionState::IsAtEOF() const {
26   return endfileRecordNumber && currentRecordNumber >= *endfileRecordNumber;
27 }
28 
29 void ConnectionState::HandleAbsolutePosition(std::int64_t n) {
30   positionInRecord = std::max(n, std::int64_t{0}) + leftTabLimit.value_or(0);
31 }
32 
33 void ConnectionState::HandleRelativePosition(std::int64_t n) {
34   positionInRecord = std::max(leftTabLimit.value_or(0), positionInRecord + n);
35 }
36 } // namespace Fortran::runtime::io
37