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(u: openRecl.value_or( |
18 | u&: 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(a: n, b: std::int64_t{0}) + leftTabLimit.value_or(u: 0); |
36 | } |
37 | |
38 | void ConnectionState::HandleRelativePosition(std::int64_t n) { |
39 | positionInRecord = std::max(a: leftTabLimit.value_or(u: 0), b: 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 | if (!cancelled_) { |
50 | ConnectionState &conn{io_.GetConnectionState()}; |
51 | while (conn.currentRecordNumber > saved_.currentRecordNumber) { |
52 | io_.BackspaceRecord(); |
53 | } |
54 | conn.leftTabLimit = saved_.leftTabLimit; |
55 | conn.furthestPositionInRecord = saved_.furthestPositionInRecord; |
56 | conn.positionInRecord = saved_.positionInRecord; |
57 | conn.pinnedFrame = saved_.pinnedFrame; |
58 | } |
59 | } |
60 | } // namespace Fortran::runtime::io |
61 |