1 //===-- runtime/io-stmt.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 "io-stmt.h" 10 #include "connection.h" 11 #include "format.h" 12 #include "tools.h" 13 #include "unit.h" 14 #include "flang/Runtime/memory.h" 15 #include <algorithm> 16 #include <cstdio> 17 #include <cstring> 18 #include <limits> 19 20 namespace Fortran::runtime::io { 21 22 int IoStatementBase::EndIoStatement() { return GetIoStat(); } 23 24 bool IoStatementBase::Emit(const char *, std::size_t, std::size_t) { 25 return false; 26 } 27 28 bool IoStatementBase::Emit(const char *, std::size_t) { return false; } 29 30 bool IoStatementBase::Emit(const char16_t *, std::size_t) { return false; } 31 32 bool IoStatementBase::Emit(const char32_t *, std::size_t) { return false; } 33 34 std::size_t IoStatementBase::GetNextInputBytes(const char *&p) { 35 p = nullptr; 36 return 0; 37 } 38 39 bool IoStatementBase::AdvanceRecord(int) { return false; } 40 41 void IoStatementBase::BackspaceRecord() {} 42 43 bool IoStatementBase::Receive(char *, std::size_t, std::size_t) { 44 return false; 45 } 46 47 std::optional<DataEdit> IoStatementBase::GetNextDataEdit( 48 IoStatementState &, int) { 49 return std::nullopt; 50 } 51 52 ExternalFileUnit *IoStatementBase::GetExternalFileUnit() const { 53 return nullptr; 54 } 55 56 bool IoStatementBase::BeginReadingRecord() { return true; } 57 58 void IoStatementBase::FinishReadingRecord() {} 59 60 void IoStatementBase::HandleAbsolutePosition(std::int64_t) {} 61 62 void IoStatementBase::HandleRelativePosition(std::int64_t) {} 63 64 bool IoStatementBase::Inquire(InquiryKeywordHash, char *, std::size_t) { 65 return false; 66 } 67 68 bool IoStatementBase::Inquire(InquiryKeywordHash, bool &) { return false; } 69 70 bool IoStatementBase::Inquire(InquiryKeywordHash, std::int64_t, bool &) { 71 return false; 72 } 73 74 bool IoStatementBase::Inquire(InquiryKeywordHash, std::int64_t &) { 75 return false; 76 } 77 78 void IoStatementBase::BadInquiryKeywordHashCrash(InquiryKeywordHash inquiry) { 79 char buffer[16]; 80 const char *decode{InquiryKeywordHashDecode(buffer, sizeof buffer, inquiry)}; 81 Crash("bad InquiryKeywordHash 0x%x (%s)", inquiry, 82 decode ? decode : "(cannot decode)"); 83 } 84 85 template <Direction DIR, typename CHAR> 86 InternalIoStatementState<DIR, CHAR>::InternalIoStatementState( 87 Buffer scalar, std::size_t length, const char *sourceFile, int sourceLine) 88 : IoStatementBase{sourceFile, sourceLine}, unit_{scalar, length} {} 89 90 template <Direction DIR, typename CHAR> 91 InternalIoStatementState<DIR, CHAR>::InternalIoStatementState( 92 const Descriptor &d, const char *sourceFile, int sourceLine) 93 : IoStatementBase{sourceFile, sourceLine}, unit_{d, *this} {} 94 95 template <Direction DIR, typename CHAR> 96 bool InternalIoStatementState<DIR, CHAR>::Emit( 97 const CharType *data, std::size_t chars) { 98 if constexpr (DIR == Direction::Input) { 99 Crash("InternalIoStatementState<Direction::Input>::Emit() called"); 100 return false; 101 } 102 return unit_.Emit(data, chars * sizeof(CharType), *this); 103 } 104 105 template <Direction DIR, typename CHAR> 106 std::size_t InternalIoStatementState<DIR, CHAR>::GetNextInputBytes( 107 const char *&p) { 108 return unit_.GetNextInputBytes(p, *this); 109 } 110 111 template <Direction DIR, typename CHAR> 112 bool InternalIoStatementState<DIR, CHAR>::AdvanceRecord(int n) { 113 while (n-- > 0) { 114 if (!unit_.AdvanceRecord(*this)) { 115 return false; 116 } 117 } 118 return true; 119 } 120 121 template <Direction DIR, typename CHAR> 122 void InternalIoStatementState<DIR, CHAR>::BackspaceRecord() { 123 unit_.BackspaceRecord(*this); 124 } 125 126 template <Direction DIR, typename CHAR> 127 int InternalIoStatementState<DIR, CHAR>::EndIoStatement() { 128 if constexpr (DIR == Direction::Output) { 129 unit_.EndIoStatement(); // fill 130 } 131 auto result{IoStatementBase::EndIoStatement()}; 132 if (free_) { 133 FreeMemory(this); 134 } 135 return result; 136 } 137 138 template <Direction DIR, typename CHAR> 139 void InternalIoStatementState<DIR, CHAR>::HandleAbsolutePosition( 140 std::int64_t n) { 141 return unit_.HandleAbsolutePosition(n); 142 } 143 144 template <Direction DIR, typename CHAR> 145 void InternalIoStatementState<DIR, CHAR>::HandleRelativePosition( 146 std::int64_t n) { 147 return unit_.HandleRelativePosition(n); 148 } 149 150 template <Direction DIR, typename CHAR> 151 InternalFormattedIoStatementState<DIR, CHAR>::InternalFormattedIoStatementState( 152 Buffer buffer, std::size_t length, const CHAR *format, 153 std::size_t formatLength, const char *sourceFile, int sourceLine) 154 : InternalIoStatementState<DIR, CHAR>{buffer, length, sourceFile, 155 sourceLine}, 156 ioStatementState_{*this}, format_{*this, format, formatLength} {} 157 158 template <Direction DIR, typename CHAR> 159 InternalFormattedIoStatementState<DIR, CHAR>::InternalFormattedIoStatementState( 160 const Descriptor &d, const CHAR *format, std::size_t formatLength, 161 const char *sourceFile, int sourceLine) 162 : InternalIoStatementState<DIR, CHAR>{d, sourceFile, sourceLine}, 163 ioStatementState_{*this}, format_{*this, format, formatLength} {} 164 165 template <Direction DIR, typename CHAR> 166 int InternalFormattedIoStatementState<DIR, CHAR>::EndIoStatement() { 167 if constexpr (DIR == Direction::Output) { 168 format_.Finish(*this); // ignore any remaining input positioning actions 169 } 170 return InternalIoStatementState<DIR, CHAR>::EndIoStatement(); 171 } 172 173 template <Direction DIR, typename CHAR> 174 InternalListIoStatementState<DIR, CHAR>::InternalListIoStatementState( 175 Buffer buffer, std::size_t length, const char *sourceFile, int sourceLine) 176 : InternalIoStatementState<DIR, CharType>{buffer, length, sourceFile, 177 sourceLine}, 178 ioStatementState_{*this} {} 179 180 template <Direction DIR, typename CHAR> 181 InternalListIoStatementState<DIR, CHAR>::InternalListIoStatementState( 182 const Descriptor &d, const char *sourceFile, int sourceLine) 183 : InternalIoStatementState<DIR, CharType>{d, sourceFile, sourceLine}, 184 ioStatementState_{*this} {} 185 186 ExternalIoStatementBase::ExternalIoStatementBase( 187 ExternalFileUnit &unit, const char *sourceFile, int sourceLine) 188 : IoStatementBase{sourceFile, sourceLine}, unit_{unit} {} 189 190 MutableModes &ExternalIoStatementBase::mutableModes() { return unit_.modes; } 191 192 ConnectionState &ExternalIoStatementBase::GetConnectionState() { return unit_; } 193 194 int ExternalIoStatementBase::EndIoStatement() { 195 if (mutableModes().nonAdvancing) { 196 unit_.leftTabLimit = unit_.furthestPositionInRecord; 197 } else { 198 unit_.leftTabLimit.reset(); 199 } 200 auto result{IoStatementBase::EndIoStatement()}; 201 unit_.EndIoStatement(); // annihilates *this in unit_.u_ 202 return result; 203 } 204 205 void OpenStatementState::set_path(const char *path, std::size_t length) { 206 pathLength_ = TrimTrailingSpaces(path, length); 207 path_ = SaveDefaultCharacter(path, pathLength_, *this); 208 } 209 210 int OpenStatementState::EndIoStatement() { 211 if (position_) { 212 if (access_ && *access_ == Access::Direct) { 213 SignalError("POSITION= may not be set with ACCESS='DIRECT'"); 214 position_.reset(); 215 } 216 } 217 if (path_.get() || wasExtant_ || 218 (status_ && *status_ == OpenStatus::Scratch)) { 219 unit().OpenUnit(status_, action_, position_.value_or(Position::AsIs), 220 std::move(path_), pathLength_, convert_, *this); 221 } else { 222 unit().OpenAnonymousUnit( 223 status_, action_, position_.value_or(Position::AsIs), convert_, *this); 224 } 225 if (access_) { 226 if (*access_ != unit().access) { 227 if (wasExtant_) { 228 SignalError("ACCESS= may not be changed on an open unit"); 229 access_.reset(); 230 } 231 } 232 if (access_) { 233 unit().access = *access_; 234 } 235 } 236 if (!unit().isUnformatted) { 237 unit().isUnformatted = isUnformatted_; 238 } 239 if (isUnformatted_ && *isUnformatted_ != *unit().isUnformatted) { 240 if (wasExtant_) { 241 SignalError("FORM= may not be changed on an open unit"); 242 } 243 unit().isUnformatted = *isUnformatted_; 244 } 245 if (!unit().isUnformatted) { 246 // Set default format (C.7.4 point 2). 247 unit().isUnformatted = unit().access != Access::Sequential; 248 } 249 return ExternalIoStatementBase::EndIoStatement(); 250 } 251 252 int CloseStatementState::EndIoStatement() { 253 int result{ExternalIoStatementBase::EndIoStatement()}; 254 unit().CloseUnit(status_, *this); 255 unit().DestroyClosed(); 256 return result; 257 } 258 259 int NoUnitIoStatementState::EndIoStatement() { 260 auto result{IoStatementBase::EndIoStatement()}; 261 FreeMemory(this); 262 return result; 263 } 264 265 template <Direction DIR> 266 ExternalIoStatementState<DIR>::ExternalIoStatementState( 267 ExternalFileUnit &unit, const char *sourceFile, int sourceLine) 268 : ExternalIoStatementBase{unit, sourceFile, sourceLine}, mutableModes_{ 269 unit.modes} { 270 if constexpr (DIR == Direction::Output) { 271 // If the last statement was a non-advancing IO input statement, the unit 272 // furthestPositionInRecord was not advanced, but the positionInRecord may 273 // have been advanced. Advance furthestPositionInRecord here to avoid 274 // overwriting the part of the record that has been read with blanks. 275 unit.furthestPositionInRecord = 276 std::max(unit.furthestPositionInRecord, unit.positionInRecord); 277 } 278 } 279 280 template <Direction DIR> int ExternalIoStatementState<DIR>::EndIoStatement() { 281 if constexpr (DIR == Direction::Input) { 282 BeginReadingRecord(); // in case there were no I/O items 283 if (!mutableModes().nonAdvancing || GetIoStat() == IostatEor) { 284 FinishReadingRecord(); 285 } 286 } else { 287 if (!mutableModes().nonAdvancing) { 288 unit().AdvanceRecord(*this); 289 } 290 unit().FlushIfTerminal(*this); 291 } 292 return ExternalIoStatementBase::EndIoStatement(); 293 } 294 295 template <Direction DIR> 296 bool ExternalIoStatementState<DIR>::Emit( 297 const char *data, std::size_t bytes, std::size_t elementBytes) { 298 if constexpr (DIR == Direction::Input) { 299 Crash("ExternalIoStatementState::Emit(char) called for input statement"); 300 } 301 return unit().Emit(data, bytes, elementBytes, *this); 302 } 303 304 template <Direction DIR> 305 bool ExternalIoStatementState<DIR>::Emit(const char *data, std::size_t bytes) { 306 if constexpr (DIR == Direction::Input) { 307 Crash("ExternalIoStatementState::Emit(char) called for input statement"); 308 } 309 return unit().Emit(data, bytes, 0, *this); 310 } 311 312 template <Direction DIR> 313 bool ExternalIoStatementState<DIR>::Emit( 314 const char16_t *data, std::size_t chars) { 315 if constexpr (DIR == Direction::Input) { 316 Crash( 317 "ExternalIoStatementState::Emit(char16_t) called for input statement"); 318 } 319 // TODO: UTF-8 encoding 320 return unit().Emit(reinterpret_cast<const char *>(data), chars * sizeof *data, 321 sizeof *data, *this); 322 } 323 324 template <Direction DIR> 325 bool ExternalIoStatementState<DIR>::Emit( 326 const char32_t *data, std::size_t chars) { 327 if constexpr (DIR == Direction::Input) { 328 Crash( 329 "ExternalIoStatementState::Emit(char32_t) called for input statement"); 330 } 331 // TODO: UTF-8 encoding 332 return unit().Emit(reinterpret_cast<const char *>(data), chars * sizeof *data, 333 sizeof *data, *this); 334 } 335 336 template <Direction DIR> 337 std::size_t ExternalIoStatementState<DIR>::GetNextInputBytes(const char *&p) { 338 return unit().GetNextInputBytes(p, *this); 339 } 340 341 template <Direction DIR> 342 bool ExternalIoStatementState<DIR>::AdvanceRecord(int n) { 343 while (n-- > 0) { 344 if (!unit().AdvanceRecord(*this)) { 345 return false; 346 } 347 } 348 return true; 349 } 350 351 template <Direction DIR> void ExternalIoStatementState<DIR>::BackspaceRecord() { 352 unit().BackspaceRecord(*this); 353 } 354 355 template <Direction DIR> 356 void ExternalIoStatementState<DIR>::HandleAbsolutePosition(std::int64_t n) { 357 return unit().HandleAbsolutePosition(n); 358 } 359 360 template <Direction DIR> 361 void ExternalIoStatementState<DIR>::HandleRelativePosition(std::int64_t n) { 362 return unit().HandleRelativePosition(n); 363 } 364 365 template <Direction DIR> 366 bool ExternalIoStatementState<DIR>::BeginReadingRecord() { 367 if constexpr (DIR == Direction::Input) { 368 return unit().BeginReadingRecord(*this); 369 } else { 370 Crash("ExternalIoStatementState<Direction::Output>::BeginReadingRecord() " 371 "called"); 372 return false; 373 } 374 } 375 376 template <Direction DIR> 377 void ExternalIoStatementState<DIR>::FinishReadingRecord() { 378 if constexpr (DIR == Direction::Input) { 379 unit().FinishReadingRecord(*this); 380 } else { 381 Crash("ExternalIoStatementState<Direction::Output>::FinishReadingRecord() " 382 "called"); 383 } 384 } 385 386 template <Direction DIR, typename CHAR> 387 ExternalFormattedIoStatementState<DIR, CHAR>::ExternalFormattedIoStatementState( 388 ExternalFileUnit &unit, const CHAR *format, std::size_t formatLength, 389 const char *sourceFile, int sourceLine) 390 : ExternalIoStatementState<DIR>{unit, sourceFile, sourceLine}, 391 format_{*this, format, formatLength} {} 392 393 template <Direction DIR, typename CHAR> 394 int ExternalFormattedIoStatementState<DIR, CHAR>::EndIoStatement() { 395 if constexpr (DIR == Direction::Input) { 396 this->BeginReadingRecord(); // in case there were no I/O items 397 } 398 format_.Finish(*this); 399 return ExternalIoStatementState<DIR>::EndIoStatement(); 400 } 401 402 std::optional<DataEdit> IoStatementState::GetNextDataEdit(int n) { 403 return std::visit( 404 [&](auto &x) { return x.get().GetNextDataEdit(*this, n); }, u_); 405 } 406 407 bool IoStatementState::Emit( 408 const char *data, std::size_t n, std::size_t elementBytes) { 409 return std::visit( 410 [=](auto &x) { return x.get().Emit(data, n, elementBytes); }, u_); 411 } 412 413 bool IoStatementState::Emit(const char *data, std::size_t n) { 414 return std::visit([=](auto &x) { return x.get().Emit(data, n); }, u_); 415 } 416 417 bool IoStatementState::Emit(const char16_t *data, std::size_t chars) { 418 return std::visit([=](auto &x) { return x.get().Emit(data, chars); }, u_); 419 } 420 421 bool IoStatementState::Emit(const char32_t *data, std::size_t chars) { 422 return std::visit([=](auto &x) { return x.get().Emit(data, chars); }, u_); 423 } 424 425 bool IoStatementState::Receive( 426 char *data, std::size_t n, std::size_t elementBytes) { 427 return std::visit( 428 [=](auto &x) { return x.get().Receive(data, n, elementBytes); }, u_); 429 } 430 431 std::size_t IoStatementState::GetNextInputBytes(const char *&p) { 432 return std::visit([&](auto &x) { return x.get().GetNextInputBytes(p); }, u_); 433 } 434 435 bool IoStatementState::AdvanceRecord(int n) { 436 return std::visit([=](auto &x) { return x.get().AdvanceRecord(n); }, u_); 437 } 438 439 void IoStatementState::BackspaceRecord() { 440 std::visit([](auto &x) { x.get().BackspaceRecord(); }, u_); 441 } 442 443 void IoStatementState::HandleRelativePosition(std::int64_t n) { 444 std::visit([=](auto &x) { x.get().HandleRelativePosition(n); }, u_); 445 } 446 447 void IoStatementState::HandleAbsolutePosition(std::int64_t n) { 448 std::visit([=](auto &x) { x.get().HandleAbsolutePosition(n); }, u_); 449 } 450 451 int IoStatementState::EndIoStatement() { 452 return std::visit([](auto &x) { return x.get().EndIoStatement(); }, u_); 453 } 454 455 ConnectionState &IoStatementState::GetConnectionState() { 456 return std::visit( 457 [](auto &x) -> ConnectionState & { return x.get().GetConnectionState(); }, 458 u_); 459 } 460 461 MutableModes &IoStatementState::mutableModes() { 462 return std::visit( 463 [](auto &x) -> MutableModes & { return x.get().mutableModes(); }, u_); 464 } 465 466 bool IoStatementState::BeginReadingRecord() { 467 return std::visit([](auto &x) { return x.get().BeginReadingRecord(); }, u_); 468 } 469 470 IoErrorHandler &IoStatementState::GetIoErrorHandler() const { 471 return std::visit( 472 [](auto &x) -> IoErrorHandler & { 473 return static_cast<IoErrorHandler &>(x.get()); 474 }, 475 u_); 476 } 477 478 ExternalFileUnit *IoStatementState::GetExternalFileUnit() const { 479 return std::visit([](auto &x) { return x.get().GetExternalFileUnit(); }, u_); 480 } 481 482 bool IoStatementState::EmitRepeated(char ch, std::size_t n) { 483 return std::visit( 484 [=](auto &x) { 485 for (std::size_t j{0}; j < n; ++j) { 486 if (!x.get().Emit(&ch, 1)) { 487 return false; 488 } 489 } 490 return true; 491 }, 492 u_); 493 } 494 495 bool IoStatementState::EmitField( 496 const char *p, std::size_t length, std::size_t width) { 497 if (width <= 0) { 498 width = static_cast<int>(length); 499 } 500 if (length > static_cast<std::size_t>(width)) { 501 return EmitRepeated('*', width); 502 } else { 503 return EmitRepeated(' ', static_cast<int>(width - length)) && 504 Emit(p, length); 505 } 506 } 507 508 std::optional<char32_t> IoStatementState::NextInField( 509 std::optional<int> &remaining, const DataEdit &edit) { 510 if (!remaining) { // Stream, list-directed, or NAMELIST 511 if (auto next{GetCurrentChar()}) { 512 if (edit.IsListDirected()) { 513 // list-directed or NAMELIST: check for separators 514 switch (*next) { 515 case ' ': 516 case '\t': 517 case ';': 518 case '/': 519 case '(': 520 case ')': 521 case '\'': 522 case '"': 523 case '*': 524 case '\n': // for stream access 525 return std::nullopt; 526 case ',': 527 if (edit.modes.editingFlags & decimalComma) { 528 break; 529 } else { 530 return std::nullopt; 531 } 532 default: 533 break; 534 } 535 } 536 HandleRelativePosition(1); 537 GotChar(); 538 return next; 539 } 540 } else if (*remaining > 0) { 541 if (auto next{GetCurrentChar()}) { 542 --*remaining; 543 HandleRelativePosition(1); 544 GotChar(); 545 return next; 546 } 547 const ConnectionState &connection{GetConnectionState()}; 548 if (!connection.IsAtEOF()) { 549 if (auto length{connection.EffectiveRecordLength()}) { 550 if (connection.positionInRecord >= *length) { 551 IoErrorHandler &handler{GetIoErrorHandler()}; 552 if (mutableModes().nonAdvancing) { 553 handler.SignalEor(); 554 } else if (connection.openRecl && !connection.modes.pad) { 555 handler.SignalError(IostatRecordReadOverrun); 556 } 557 if (connection.modes.pad) { // PAD='YES' 558 --*remaining; 559 return std::optional<char32_t>{' '}; 560 } 561 } 562 } 563 } 564 } 565 return std::nullopt; 566 } 567 568 bool IoStatementState::Inquire( 569 InquiryKeywordHash inquiry, char *out, std::size_t chars) { 570 return std::visit( 571 [&](auto &x) { return x.get().Inquire(inquiry, out, chars); }, u_); 572 } 573 574 bool IoStatementState::Inquire(InquiryKeywordHash inquiry, bool &out) { 575 return std::visit([&](auto &x) { return x.get().Inquire(inquiry, out); }, u_); 576 } 577 578 bool IoStatementState::Inquire( 579 InquiryKeywordHash inquiry, std::int64_t id, bool &out) { 580 return std::visit( 581 [&](auto &x) { return x.get().Inquire(inquiry, id, out); }, u_); 582 } 583 584 bool IoStatementState::Inquire(InquiryKeywordHash inquiry, std::int64_t &n) { 585 return std::visit([&](auto &x) { return x.get().Inquire(inquiry, n); }, u_); 586 } 587 588 void IoStatementState::GotChar(int n) { 589 if (auto *formattedIn{ 590 get_if<FormattedIoStatementState<Direction::Input>>()}) { 591 formattedIn->GotChar(n); 592 } else { 593 GetIoErrorHandler().Crash("IoStatementState::GotChar() called for " 594 "statement that is not formatted input"); 595 } 596 } 597 598 std::size_t 599 FormattedIoStatementState<Direction::Input>::GetEditDescriptorChars() const { 600 return chars_; 601 } 602 603 void FormattedIoStatementState<Direction::Input>::GotChar(int n) { 604 chars_ += n; 605 } 606 607 bool ListDirectedStatementState<Direction::Output>::EmitLeadingSpaceOrAdvance( 608 IoStatementState &io, std::size_t length, bool isCharacter) { 609 if (length == 0) { 610 return true; 611 } 612 const ConnectionState &connection{io.GetConnectionState()}; 613 int space{connection.positionInRecord == 0 || 614 !(isCharacter && lastWasUndelimitedCharacter())}; 615 set_lastWasUndelimitedCharacter(false); 616 if (connection.NeedAdvance(space + length)) { 617 return io.AdvanceRecord(); 618 } 619 if (space) { 620 return io.Emit(" ", 1); 621 } 622 return true; 623 } 624 625 std::optional<DataEdit> 626 ListDirectedStatementState<Direction::Output>::GetNextDataEdit( 627 IoStatementState &io, int maxRepeat) { 628 DataEdit edit; 629 edit.descriptor = DataEdit::ListDirected; 630 edit.repeat = maxRepeat; 631 edit.modes = io.mutableModes(); 632 return edit; 633 } 634 635 std::optional<DataEdit> 636 ListDirectedStatementState<Direction::Input>::GetNextDataEdit( 637 IoStatementState &io, int maxRepeat) { 638 // N.B. list-directed transfers cannot be nonadvancing (C1221) 639 ConnectionState &connection{io.GetConnectionState()}; 640 DataEdit edit; 641 edit.descriptor = DataEdit::ListDirected; 642 edit.repeat = 1; // may be overridden below 643 edit.modes = io.mutableModes(); 644 if (hitSlash_) { // everything after '/' is nullified 645 edit.descriptor = DataEdit::ListDirectedNullValue; 646 return edit; 647 } 648 char32_t comma{','}; 649 if (edit.modes.editingFlags & decimalComma) { 650 comma = ';'; 651 } 652 if (remaining_ > 0 && !realPart_) { // "r*c" repetition in progress 653 RUNTIME_CHECK(io.GetIoErrorHandler(), repeatPosition_.has_value()); 654 repeatPosition_.reset(); // restores the saved position 655 if (!imaginaryPart_) { 656 edit.repeat = std::min<int>(remaining_, maxRepeat); 657 auto ch{io.GetCurrentChar()}; 658 if (!ch || *ch == ' ' || *ch == '\t' || *ch == comma) { 659 // "r*" repeated null 660 edit.descriptor = DataEdit::ListDirectedNullValue; 661 } 662 } 663 remaining_ -= edit.repeat; 664 if (remaining_ > 0) { 665 repeatPosition_.emplace(io); 666 } 667 return edit; 668 } 669 // Skip separators, handle a "r*c" repeat count; see 13.10.2 in Fortran 2018 670 if (imaginaryPart_) { 671 imaginaryPart_ = false; 672 } else if (realPart_) { 673 realPart_ = false; 674 imaginaryPart_ = true; 675 edit.descriptor = DataEdit::ListDirectedImaginaryPart; 676 } 677 auto ch{io.GetNextNonBlank()}; 678 if (ch && *ch == comma && eatComma_) { 679 // Consume comma & whitespace after previous item. 680 // This includes the comma between real and imaginary components 681 // in list-directed/NAMELIST complex input. 682 // (When DECIMAL='COMMA', the comma is actually a semicolon.) 683 io.HandleRelativePosition(1); 684 ch = io.GetNextNonBlank(); 685 } 686 eatComma_ = true; 687 if (!ch) { 688 return std::nullopt; 689 } 690 if (*ch == '/') { 691 hitSlash_ = true; 692 edit.descriptor = DataEdit::ListDirectedNullValue; 693 return edit; 694 } 695 if (*ch == comma) { // separator: null value 696 edit.descriptor = DataEdit::ListDirectedNullValue; 697 return edit; 698 } 699 if (imaginaryPart_) { // can't repeat components 700 return edit; 701 } 702 if (*ch >= '0' && *ch <= '9') { // look for "r*" repetition count 703 auto start{connection.positionInRecord}; 704 int r{0}; 705 do { 706 static auto constexpr clamp{(std::numeric_limits<int>::max() - '9') / 10}; 707 if (r >= clamp) { 708 r = 0; 709 break; 710 } 711 r = 10 * r + (*ch - '0'); 712 io.HandleRelativePosition(1); 713 ch = io.GetCurrentChar(); 714 } while (ch && *ch >= '0' && *ch <= '9'); 715 if (r > 0 && ch && *ch == '*') { // subtle: r must be nonzero 716 io.HandleRelativePosition(1); 717 ch = io.GetCurrentChar(); 718 if (ch && *ch == '/') { // r*/ 719 hitSlash_ = true; 720 edit.descriptor = DataEdit::ListDirectedNullValue; 721 return edit; 722 } 723 if (!ch || *ch == ' ' || *ch == '\t' || *ch == comma) { // "r*" null 724 edit.descriptor = DataEdit::ListDirectedNullValue; 725 } 726 edit.repeat = std::min<int>(r, maxRepeat); 727 remaining_ = r - edit.repeat; 728 if (remaining_ > 0) { 729 repeatPosition_.emplace(io); 730 } 731 } else { // not a repetition count, just an integer value; rewind 732 connection.positionInRecord = start; 733 } 734 } 735 if (!imaginaryPart_ && ch && *ch == '(') { 736 realPart_ = true; 737 io.HandleRelativePosition(1); 738 edit.descriptor = DataEdit::ListDirectedRealPart; 739 } 740 return edit; 741 } 742 743 template <Direction DIR> 744 bool ExternalUnformattedIoStatementState<DIR>::Receive( 745 char *data, std::size_t bytes, std::size_t elementBytes) { 746 if constexpr (DIR == Direction::Output) { 747 this->Crash("ExternalUnformattedIoStatementState::Receive() called for " 748 "output statement"); 749 } 750 return this->unit().Receive(data, bytes, elementBytes, *this); 751 } 752 753 template <Direction DIR> 754 ChildIoStatementState<DIR>::ChildIoStatementState( 755 ChildIo &child, const char *sourceFile, int sourceLine) 756 : IoStatementBase{sourceFile, sourceLine}, child_{child} {} 757 758 template <Direction DIR> 759 MutableModes &ChildIoStatementState<DIR>::mutableModes() { 760 return child_.parent().mutableModes(); 761 } 762 763 template <Direction DIR> 764 ConnectionState &ChildIoStatementState<DIR>::GetConnectionState() { 765 return child_.parent().GetConnectionState(); 766 } 767 768 template <Direction DIR> 769 ExternalFileUnit *ChildIoStatementState<DIR>::GetExternalFileUnit() const { 770 return child_.parent().GetExternalFileUnit(); 771 } 772 773 template <Direction DIR> int ChildIoStatementState<DIR>::EndIoStatement() { 774 auto result{IoStatementBase::EndIoStatement()}; 775 child_.EndIoStatement(); // annihilates *this in child_.u_ 776 return result; 777 } 778 779 template <Direction DIR> 780 bool ChildIoStatementState<DIR>::Emit( 781 const char *data, std::size_t bytes, std::size_t elementBytes) { 782 return child_.parent().Emit(data, bytes, elementBytes); 783 } 784 785 template <Direction DIR> 786 bool ChildIoStatementState<DIR>::Emit(const char *data, std::size_t bytes) { 787 return child_.parent().Emit(data, bytes); 788 } 789 790 template <Direction DIR> 791 bool ChildIoStatementState<DIR>::Emit(const char16_t *data, std::size_t chars) { 792 return child_.parent().Emit(data, chars); 793 } 794 795 template <Direction DIR> 796 bool ChildIoStatementState<DIR>::Emit(const char32_t *data, std::size_t chars) { 797 return child_.parent().Emit(data, chars); 798 } 799 800 template <Direction DIR> 801 std::size_t ChildIoStatementState<DIR>::GetNextInputBytes(const char *&p) { 802 return child_.parent().GetNextInputBytes(p); 803 } 804 805 template <Direction DIR> 806 void ChildIoStatementState<DIR>::HandleAbsolutePosition(std::int64_t n) { 807 return child_.parent().HandleAbsolutePosition(n); 808 } 809 810 template <Direction DIR> 811 void ChildIoStatementState<DIR>::HandleRelativePosition(std::int64_t n) { 812 return child_.parent().HandleRelativePosition(n); 813 } 814 815 template <Direction DIR, typename CHAR> 816 ChildFormattedIoStatementState<DIR, CHAR>::ChildFormattedIoStatementState( 817 ChildIo &child, const CHAR *format, std::size_t formatLength, 818 const char *sourceFile, int sourceLine) 819 : ChildIoStatementState<DIR>{child, sourceFile, sourceLine}, 820 mutableModes_{child.parent().mutableModes()}, format_{*this, format, 821 formatLength} {} 822 823 template <Direction DIR, typename CHAR> 824 int ChildFormattedIoStatementState<DIR, CHAR>::EndIoStatement() { 825 format_.Finish(*this); 826 return ChildIoStatementState<DIR>::EndIoStatement(); 827 } 828 829 template <Direction DIR, typename CHAR> 830 bool ChildFormattedIoStatementState<DIR, CHAR>::AdvanceRecord(int) { 831 return false; // no can do in a child I/O 832 } 833 834 template <Direction DIR> 835 bool ChildUnformattedIoStatementState<DIR>::Receive( 836 char *data, std::size_t bytes, std::size_t elementBytes) { 837 return this->child().parent().Receive(data, bytes, elementBytes); 838 } 839 840 template class InternalIoStatementState<Direction::Output>; 841 template class InternalIoStatementState<Direction::Input>; 842 template class InternalFormattedIoStatementState<Direction::Output>; 843 template class InternalFormattedIoStatementState<Direction::Input>; 844 template class InternalListIoStatementState<Direction::Output>; 845 template class InternalListIoStatementState<Direction::Input>; 846 template class ExternalIoStatementState<Direction::Output>; 847 template class ExternalIoStatementState<Direction::Input>; 848 template class ExternalFormattedIoStatementState<Direction::Output>; 849 template class ExternalFormattedIoStatementState<Direction::Input>; 850 template class ExternalListIoStatementState<Direction::Output>; 851 template class ExternalListIoStatementState<Direction::Input>; 852 template class ExternalUnformattedIoStatementState<Direction::Output>; 853 template class ExternalUnformattedIoStatementState<Direction::Input>; 854 template class ChildIoStatementState<Direction::Output>; 855 template class ChildIoStatementState<Direction::Input>; 856 template class ChildFormattedIoStatementState<Direction::Output>; 857 template class ChildFormattedIoStatementState<Direction::Input>; 858 template class ChildListIoStatementState<Direction::Output>; 859 template class ChildListIoStatementState<Direction::Input>; 860 template class ChildUnformattedIoStatementState<Direction::Output>; 861 template class ChildUnformattedIoStatementState<Direction::Input>; 862 863 int ExternalMiscIoStatementState::EndIoStatement() { 864 ExternalFileUnit &ext{unit()}; 865 switch (which_) { 866 case Flush: 867 ext.FlushOutput(*this); 868 std::fflush(nullptr); // flushes C stdio output streams (12.9(2)) 869 break; 870 case Backspace: 871 ext.BackspaceRecord(*this); 872 break; 873 case Endfile: 874 ext.Endfile(*this); 875 break; 876 case Rewind: 877 ext.Rewind(*this); 878 break; 879 } 880 return ExternalIoStatementBase::EndIoStatement(); 881 } 882 883 InquireUnitState::InquireUnitState( 884 ExternalFileUnit &unit, const char *sourceFile, int sourceLine) 885 : ExternalIoStatementBase{unit, sourceFile, sourceLine} {} 886 887 bool InquireUnitState::Inquire( 888 InquiryKeywordHash inquiry, char *result, std::size_t length) { 889 if (unit().createdForInternalChildIo()) { 890 SignalError(IostatInquireInternalUnit, 891 "INQUIRE of unit created for defined derived type I/O of an internal " 892 "unit"); 893 return false; 894 } 895 const char *str{nullptr}; 896 switch (inquiry) { 897 case HashInquiryKeyword("ACCESS"): 898 if (!unit().IsConnected()) { 899 str = "UNDEFINED"; 900 } else { 901 switch (unit().access) { 902 case Access::Sequential: 903 str = "SEQUENTIAL"; 904 break; 905 case Access::Direct: 906 str = "DIRECT"; 907 break; 908 case Access::Stream: 909 str = "STREAM"; 910 break; 911 } 912 } 913 break; 914 case HashInquiryKeyword("ACTION"): 915 str = !unit().IsConnected() ? "UNDEFINED" 916 : unit().mayWrite() ? unit().mayRead() ? "READWRITE" : "WRITE" 917 : "READ"; 918 break; 919 case HashInquiryKeyword("ASYNCHRONOUS"): 920 str = !unit().IsConnected() ? "UNDEFINED" 921 : unit().mayAsynchronous() ? "YES" 922 : "NO"; 923 break; 924 case HashInquiryKeyword("BLANK"): 925 str = !unit().IsConnected() || unit().isUnformatted.value_or(true) 926 ? "UNDEFINED" 927 : unit().modes.editingFlags & blankZero ? "ZERO" 928 : "NULL"; 929 break; 930 case HashInquiryKeyword("CARRIAGECONTROL"): 931 str = "LIST"; 932 break; 933 case HashInquiryKeyword("CONVERT"): 934 str = unit().swapEndianness() ? "SWAP" : "NATIVE"; 935 break; 936 case HashInquiryKeyword("DECIMAL"): 937 str = !unit().IsConnected() || unit().isUnformatted.value_or(true) 938 ? "UNDEFINED" 939 : unit().modes.editingFlags & decimalComma ? "COMMA" 940 : "POINT"; 941 break; 942 case HashInquiryKeyword("DELIM"): 943 if (!unit().IsConnected() || unit().isUnformatted.value_or(true)) { 944 str = "UNDEFINED"; 945 } else { 946 switch (unit().modes.delim) { 947 case '\'': 948 str = "APOSTROPHE"; 949 break; 950 case '"': 951 str = "QUOTE"; 952 break; 953 default: 954 str = "NONE"; 955 break; 956 } 957 } 958 break; 959 case HashInquiryKeyword("DIRECT"): 960 str = !unit().IsConnected() ? "UNKNOWN" 961 : unit().access == Access::Direct || 962 (unit().mayPosition() && unit().openRecl) 963 ? "YES" 964 : "NO"; 965 break; 966 case HashInquiryKeyword("ENCODING"): 967 str = !unit().IsConnected() ? "UNKNOWN" 968 : unit().isUnformatted.value_or(true) ? "UNDEFINED" 969 : unit().isUTF8 ? "UTF-8" 970 : "ASCII"; 971 break; 972 case HashInquiryKeyword("FORM"): 973 str = !unit().IsConnected() || !unit().isUnformatted ? "UNDEFINED" 974 : *unit().isUnformatted ? "UNFORMATTED" 975 : "FORMATTED"; 976 break; 977 case HashInquiryKeyword("FORMATTED"): 978 str = !unit().IsConnected() ? "UNDEFINED" 979 : !unit().isUnformatted ? "UNKNOWN" 980 : *unit().isUnformatted ? "NO" 981 : "YES"; 982 break; 983 case HashInquiryKeyword("NAME"): 984 str = unit().path(); 985 if (!str) { 986 return true; // result is undefined 987 } 988 break; 989 case HashInquiryKeyword("PAD"): 990 str = !unit().IsConnected() || unit().isUnformatted.value_or(true) 991 ? "UNDEFINED" 992 : unit().modes.pad ? "YES" 993 : "NO"; 994 break; 995 case HashInquiryKeyword("POSITION"): 996 if (!unit().IsConnected() || unit().access == Access::Direct) { 997 str = "UNDEFINED"; 998 } else { 999 switch (unit().InquirePosition()) { 1000 case Position::Rewind: 1001 str = "REWIND"; 1002 break; 1003 case Position::Append: 1004 str = "APPEND"; 1005 break; 1006 case Position::AsIs: 1007 str = "ASIS"; 1008 break; 1009 } 1010 } 1011 break; 1012 case HashInquiryKeyword("READ"): 1013 str = !unit().IsConnected() ? "UNDEFINED" : unit().mayRead() ? "YES" : "NO"; 1014 break; 1015 case HashInquiryKeyword("READWRITE"): 1016 str = !unit().IsConnected() ? "UNDEFINED" 1017 : unit().mayRead() && unit().mayWrite() ? "YES" 1018 : "NO"; 1019 break; 1020 case HashInquiryKeyword("ROUND"): 1021 if (!unit().IsConnected() || unit().isUnformatted.value_or(true)) { 1022 str = "UNDEFINED"; 1023 } else { 1024 switch (unit().modes.round) { 1025 case decimal::FortranRounding::RoundNearest: 1026 str = "NEAREST"; 1027 break; 1028 case decimal::FortranRounding::RoundUp: 1029 str = "UP"; 1030 break; 1031 case decimal::FortranRounding::RoundDown: 1032 str = "DOWN"; 1033 break; 1034 case decimal::FortranRounding::RoundToZero: 1035 str = "ZERO"; 1036 break; 1037 case decimal::FortranRounding::RoundCompatible: 1038 str = "COMPATIBLE"; 1039 break; 1040 } 1041 } 1042 break; 1043 case HashInquiryKeyword("SEQUENTIAL"): 1044 // "NO" for Direct, since Sequential would not work if 1045 // the unit were reopened without RECL=. 1046 str = !unit().IsConnected() ? "UNKNOWN" 1047 : unit().access == Access::Sequential ? "YES" 1048 : "NO"; 1049 break; 1050 case HashInquiryKeyword("SIGN"): 1051 str = !unit().IsConnected() || unit().isUnformatted.value_or(true) 1052 ? "UNDEFINED" 1053 : unit().modes.editingFlags & signPlus ? "PLUS" 1054 : "SUPPRESS"; 1055 break; 1056 case HashInquiryKeyword("STREAM"): 1057 str = !unit().IsConnected() ? "UNKNOWN" 1058 : unit().access == Access::Stream ? "YES" 1059 : "NO"; 1060 break; 1061 case HashInquiryKeyword("UNFORMATTED"): 1062 str = !unit().IsConnected() || !unit().isUnformatted ? "UNKNOWN" 1063 : *unit().isUnformatted ? "YES" 1064 : "NO"; 1065 break; 1066 case HashInquiryKeyword("WRITE"): 1067 str = !unit().IsConnected() ? "UNKNOWN" : unit().mayWrite() ? "YES" : "NO"; 1068 break; 1069 } 1070 if (str) { 1071 ToFortranDefaultCharacter(result, length, str); 1072 return true; 1073 } else { 1074 BadInquiryKeywordHashCrash(inquiry); 1075 return false; 1076 } 1077 } 1078 1079 bool InquireUnitState::Inquire(InquiryKeywordHash inquiry, bool &result) { 1080 switch (inquiry) { 1081 case HashInquiryKeyword("EXIST"): 1082 result = true; 1083 return true; 1084 case HashInquiryKeyword("NAMED"): 1085 result = unit().path() != nullptr; 1086 return true; 1087 case HashInquiryKeyword("OPENED"): 1088 result = unit().IsConnected(); 1089 return true; 1090 case HashInquiryKeyword("PENDING"): 1091 result = false; // asynchronous I/O is not implemented 1092 return true; 1093 default: 1094 BadInquiryKeywordHashCrash(inquiry); 1095 return false; 1096 } 1097 } 1098 1099 bool InquireUnitState::Inquire( 1100 InquiryKeywordHash inquiry, std::int64_t, bool &result) { 1101 switch (inquiry) { 1102 case HashInquiryKeyword("PENDING"): 1103 result = false; // asynchronous I/O is not implemented 1104 return true; 1105 default: 1106 BadInquiryKeywordHashCrash(inquiry); 1107 return false; 1108 } 1109 } 1110 1111 bool InquireUnitState::Inquire( 1112 InquiryKeywordHash inquiry, std::int64_t &result) { 1113 switch (inquiry) { 1114 case HashInquiryKeyword("NEXTREC"): 1115 if (unit().access == Access::Direct) { 1116 result = unit().currentRecordNumber; 1117 } 1118 return true; 1119 case HashInquiryKeyword("NUMBER"): 1120 result = unit().IsConnected() ? unit().unitNumber() : -1; 1121 return true; 1122 case HashInquiryKeyword("POS"): 1123 result = unit().InquirePos(); 1124 return true; 1125 case HashInquiryKeyword("RECL"): 1126 if (!unit().IsConnected()) { 1127 result = -1; 1128 } else if (unit().access == Access::Stream) { 1129 result = -2; 1130 } else if (unit().openRecl) { 1131 result = *unit().openRecl; 1132 } else { 1133 result = std::numeric_limits<std::int32_t>::max(); 1134 } 1135 return true; 1136 case HashInquiryKeyword("SIZE"): 1137 result = -1; 1138 if (unit().IsConnected()) { 1139 if (auto size{unit().knownSize()}) { 1140 result = *size; 1141 } 1142 } 1143 return true; 1144 default: 1145 BadInquiryKeywordHashCrash(inquiry); 1146 return false; 1147 } 1148 } 1149 1150 InquireNoUnitState::InquireNoUnitState(const char *sourceFile, int sourceLine) 1151 : NoUnitIoStatementState{sourceFile, sourceLine, *this} {} 1152 1153 bool InquireNoUnitState::Inquire( 1154 InquiryKeywordHash inquiry, char *result, std::size_t length) { 1155 switch (inquiry) { 1156 case HashInquiryKeyword("ACCESS"): 1157 case HashInquiryKeyword("ACTION"): 1158 case HashInquiryKeyword("ASYNCHRONOUS"): 1159 case HashInquiryKeyword("BLANK"): 1160 case HashInquiryKeyword("CARRIAGECONTROL"): 1161 case HashInquiryKeyword("CONVERT"): 1162 case HashInquiryKeyword("DECIMAL"): 1163 case HashInquiryKeyword("DELIM"): 1164 case HashInquiryKeyword("FORM"): 1165 case HashInquiryKeyword("NAME"): 1166 case HashInquiryKeyword("PAD"): 1167 case HashInquiryKeyword("POSITION"): 1168 case HashInquiryKeyword("ROUND"): 1169 case HashInquiryKeyword("SIGN"): 1170 ToFortranDefaultCharacter(result, length, "UNDEFINED"); 1171 return true; 1172 case HashInquiryKeyword("DIRECT"): 1173 case HashInquiryKeyword("ENCODING"): 1174 case HashInquiryKeyword("FORMATTED"): 1175 case HashInquiryKeyword("READ"): 1176 case HashInquiryKeyword("READWRITE"): 1177 case HashInquiryKeyword("SEQUENTIAL"): 1178 case HashInquiryKeyword("STREAM"): 1179 case HashInquiryKeyword("WRITE"): 1180 case HashInquiryKeyword("UNFORMATTED"): 1181 ToFortranDefaultCharacter(result, length, "UNKNONN"); 1182 return true; 1183 default: 1184 BadInquiryKeywordHashCrash(inquiry); 1185 return false; 1186 } 1187 } 1188 1189 bool InquireNoUnitState::Inquire(InquiryKeywordHash inquiry, bool &result) { 1190 switch (inquiry) { 1191 case HashInquiryKeyword("EXIST"): 1192 result = true; 1193 return true; 1194 case HashInquiryKeyword("NAMED"): 1195 case HashInquiryKeyword("OPENED"): 1196 case HashInquiryKeyword("PENDING"): 1197 result = false; 1198 return true; 1199 default: 1200 BadInquiryKeywordHashCrash(inquiry); 1201 return false; 1202 } 1203 } 1204 1205 bool InquireNoUnitState::Inquire( 1206 InquiryKeywordHash inquiry, std::int64_t, bool &result) { 1207 switch (inquiry) { 1208 case HashInquiryKeyword("PENDING"): 1209 result = false; 1210 return true; 1211 default: 1212 BadInquiryKeywordHashCrash(inquiry); 1213 return false; 1214 } 1215 } 1216 1217 bool InquireNoUnitState::Inquire( 1218 InquiryKeywordHash inquiry, std::int64_t &result) { 1219 switch (inquiry) { 1220 case HashInquiryKeyword("NEXTREC"): 1221 case HashInquiryKeyword("NUMBER"): 1222 case HashInquiryKeyword("POS"): 1223 case HashInquiryKeyword("RECL"): 1224 case HashInquiryKeyword("SIZE"): 1225 result = -1; 1226 return true; 1227 default: 1228 BadInquiryKeywordHashCrash(inquiry); 1229 return false; 1230 } 1231 } 1232 1233 InquireUnconnectedFileState::InquireUnconnectedFileState( 1234 OwningPtr<char> &&path, const char *sourceFile, int sourceLine) 1235 : NoUnitIoStatementState{sourceFile, sourceLine, *this}, path_{std::move( 1236 path)} {} 1237 1238 bool InquireUnconnectedFileState::Inquire( 1239 InquiryKeywordHash inquiry, char *result, std::size_t length) { 1240 const char *str{nullptr}; 1241 switch (inquiry) { 1242 case HashInquiryKeyword("ACCESS"): 1243 case HashInquiryKeyword("ACTION"): 1244 case HashInquiryKeyword("ASYNCHRONOUS"): 1245 case HashInquiryKeyword("BLANK"): 1246 case HashInquiryKeyword("CARRIAGECONTROL"): 1247 case HashInquiryKeyword("CONVERT"): 1248 case HashInquiryKeyword("DECIMAL"): 1249 case HashInquiryKeyword("DELIM"): 1250 case HashInquiryKeyword("FORM"): 1251 case HashInquiryKeyword("PAD"): 1252 case HashInquiryKeyword("POSITION"): 1253 case HashInquiryKeyword("ROUND"): 1254 case HashInquiryKeyword("SIGN"): 1255 str = "UNDEFINED"; 1256 break; 1257 case HashInquiryKeyword("DIRECT"): 1258 case HashInquiryKeyword("ENCODING"): 1259 case HashInquiryKeyword("FORMATTED"): 1260 case HashInquiryKeyword("SEQUENTIAL"): 1261 case HashInquiryKeyword("STREAM"): 1262 case HashInquiryKeyword("UNFORMATTED"): 1263 str = "UNKNONN"; 1264 break; 1265 case HashInquiryKeyword("READ"): 1266 str = MayRead(path_.get()) ? "YES" : "NO"; 1267 break; 1268 case HashInquiryKeyword("READWRITE"): 1269 str = MayReadAndWrite(path_.get()) ? "YES" : "NO"; 1270 break; 1271 case HashInquiryKeyword("WRITE"): 1272 str = MayWrite(path_.get()) ? "YES" : "NO"; 1273 break; 1274 case HashInquiryKeyword("NAME"): 1275 str = path_.get(); 1276 if (!str) { 1277 return true; // result is undefined 1278 } 1279 break; 1280 } 1281 if (str) { 1282 ToFortranDefaultCharacter(result, length, str); 1283 return true; 1284 } else { 1285 BadInquiryKeywordHashCrash(inquiry); 1286 return false; 1287 } 1288 } 1289 1290 bool InquireUnconnectedFileState::Inquire( 1291 InquiryKeywordHash inquiry, bool &result) { 1292 switch (inquiry) { 1293 case HashInquiryKeyword("EXIST"): 1294 result = IsExtant(path_.get()); 1295 return true; 1296 case HashInquiryKeyword("NAMED"): 1297 result = true; 1298 return true; 1299 case HashInquiryKeyword("OPENED"): 1300 result = false; 1301 return true; 1302 case HashInquiryKeyword("PENDING"): 1303 result = false; 1304 return true; 1305 default: 1306 BadInquiryKeywordHashCrash(inquiry); 1307 return false; 1308 } 1309 } 1310 1311 bool InquireUnconnectedFileState::Inquire( 1312 InquiryKeywordHash inquiry, std::int64_t, bool &result) { 1313 switch (inquiry) { 1314 case HashInquiryKeyword("PENDING"): 1315 result = false; 1316 return true; 1317 default: 1318 BadInquiryKeywordHashCrash(inquiry); 1319 return false; 1320 } 1321 } 1322 1323 bool InquireUnconnectedFileState::Inquire( 1324 InquiryKeywordHash inquiry, std::int64_t &result) { 1325 switch (inquiry) { 1326 case HashInquiryKeyword("NEXTREC"): 1327 case HashInquiryKeyword("NUMBER"): 1328 case HashInquiryKeyword("POS"): 1329 case HashInquiryKeyword("RECL"): 1330 case HashInquiryKeyword("SIZE"): 1331 result = -1; 1332 return true; 1333 default: 1334 BadInquiryKeywordHashCrash(inquiry); 1335 return false; 1336 } 1337 } 1338 1339 InquireIOLengthState::InquireIOLengthState( 1340 const char *sourceFile, int sourceLine) 1341 : NoUnitIoStatementState{sourceFile, sourceLine, *this} {} 1342 1343 bool InquireIOLengthState::Emit(const char *, std::size_t n, std::size_t) { 1344 bytes_ += n; 1345 return true; 1346 } 1347 1348 bool InquireIOLengthState::Emit(const char *p, std::size_t n) { 1349 bytes_ += sizeof *p * n; 1350 return true; 1351 } 1352 1353 bool InquireIOLengthState::Emit(const char16_t *p, std::size_t n) { 1354 bytes_ += sizeof *p * n; 1355 return true; 1356 } 1357 1358 bool InquireIOLengthState::Emit(const char32_t *p, std::size_t n) { 1359 bytes_ += sizeof *p * n; 1360 return true; 1361 } 1362 1363 int ErroneousIoStatementState::EndIoStatement() { 1364 SignalError(iostat_); 1365 return IoStatementBase::EndIoStatement(); 1366 } 1367 1368 } // namespace Fortran::runtime::io 1369