@@ -53,8 +53,9 @@ ExternalFileUnit &ExternalFileUnit::LookUpOrCreate(
53
53
return GetUnitMap ().LookUpOrCreate (unit, terminator, wasExtant);
54
54
}
55
55
56
- ExternalFileUnit &ExternalFileUnit::LookUpOrCreateAnonymous (
57
- int unit, Direction dir, bool isUnformatted, const Terminator &terminator) {
56
+ ExternalFileUnit &ExternalFileUnit::LookUpOrCreateAnonymous (int unit,
57
+ Direction dir, std::optional<bool > isUnformatted,
58
+ const Terminator &terminator) {
58
59
bool exists{false };
59
60
ExternalFileUnit &result{
60
61
GetUnitMap ().LookUpOrCreate (unit, terminator, exists)};
@@ -313,7 +314,7 @@ std::optional<char32_t> ExternalFileUnit::GetCurrentChar(
313
314
314
315
const char *ExternalFileUnit::FrameNextInput (
315
316
IoErrorHandler &handler, std::size_t bytes) {
316
- RUNTIME_CHECK (handler, ! isUnformatted);
317
+ RUNTIME_CHECK (handler, isUnformatted. has_value () && !* isUnformatted);
317
318
if (static_cast <std::int64_t >(positionInRecord + bytes) <=
318
319
recordLength.value_or (positionInRecord + bytes)) {
319
320
auto at{recordOffsetInFrame_ + positionInRecord};
@@ -367,10 +368,13 @@ bool ExternalFileUnit::BeginReadingRecord(IoErrorHandler &handler) {
367
368
if (got < need) {
368
369
handler.SignalEnd ();
369
370
}
370
- } else if (isUnformatted) {
371
- BeginSequentialVariableUnformattedInputRecord (handler);
372
- } else { // formatted
373
- BeginSequentialVariableFormattedInputRecord (handler);
371
+ } else {
372
+ RUNTIME_CHECK (handler, isUnformatted.has_value ());
373
+ if (isUnformatted.value_or (false )) {
374
+ BeginSequentialVariableUnformattedInputRecord (handler);
375
+ } else { // formatted
376
+ BeginSequentialVariableFormattedInputRecord (handler);
377
+ }
374
378
}
375
379
}
376
380
}
@@ -390,18 +394,21 @@ void ExternalFileUnit::FinishReadingRecord(IoErrorHandler &handler) {
390
394
if (isFixedRecordLength) {
391
395
frameOffsetInFile_ += recordOffsetInFrame_ + *recordLength;
392
396
recordOffsetInFrame_ = 0 ;
393
- } else if (isUnformatted) {
394
- // Retain footer in frame for more efficient BACKSPACE
395
- frameOffsetInFile_ += recordOffsetInFrame_ + *recordLength;
396
- recordOffsetInFrame_ = sizeof (std::uint32_t );
397
- recordLength.reset ();
398
- } else { // formatted
399
- if (Frame ()[recordOffsetInFrame_ + *recordLength] == ' \r ' ) {
400
- ++recordOffsetInFrame_;
397
+ } else {
398
+ RUNTIME_CHECK (handler, isUnformatted.has_value ());
399
+ if (isUnformatted.value_or (false )) {
400
+ // Retain footer in frame for more efficient BACKSPACE
401
+ frameOffsetInFile_ += recordOffsetInFrame_ + *recordLength;
402
+ recordOffsetInFrame_ = sizeof (std::uint32_t );
403
+ recordLength.reset ();
404
+ } else { // formatted
405
+ if (Frame ()[recordOffsetInFrame_ + *recordLength] == ' \r ' ) {
406
+ ++recordOffsetInFrame_;
407
+ }
408
+ recordOffsetInFrame_ += *recordLength + 1 ;
409
+ RUNTIME_CHECK (handler, Frame ()[recordOffsetInFrame_ - 1 ] == ' \n ' );
410
+ recordLength.reset ();
401
411
}
402
- recordOffsetInFrame_ += *recordLength + 1 ;
403
- RUNTIME_CHECK (handler, Frame ()[recordOffsetInFrame_ - 1 ] == ' \n ' );
404
- recordLength.reset ();
405
412
}
406
413
}
407
414
++currentRecordNumber;
@@ -414,17 +421,19 @@ bool ExternalFileUnit::AdvanceRecord(IoErrorHandler &handler) {
414
421
return BeginReadingRecord (handler);
415
422
} else { // Direction::Output
416
423
bool ok{true };
424
+ RUNTIME_CHECK (handler, isUnformatted.has_value ());
417
425
if (isFixedRecordLength && recordLength) {
418
426
// Pad remainder of fixed length record
419
427
if (furthestPositionInRecord < *recordLength) {
420
428
WriteFrame (
421
429
frameOffsetInFile_, recordOffsetInFrame_ + *recordLength, handler);
422
430
std::memset (Frame () + recordOffsetInFrame_ + furthestPositionInRecord,
423
- isUnformatted ? 0 : ' ' , *recordLength - furthestPositionInRecord);
431
+ isUnformatted.value_or (false ) ? 0 : ' ' ,
432
+ *recordLength - furthestPositionInRecord);
424
433
}
425
434
} else {
426
435
positionInRecord = furthestPositionInRecord;
427
- if (isUnformatted) {
436
+ if (isUnformatted. value_or ( false ) ) {
428
437
// Append the length of a sequential unformatted variable-length record
429
438
// as its footer, then overwrite the reserved first four bytes of the
430
439
// record with its length as its header. These four bytes were skipped
@@ -466,10 +475,13 @@ void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
466
475
--currentRecordNumber;
467
476
if (isFixedRecordLength) {
468
477
BackspaceFixedRecord (handler);
469
- } else if (isUnformatted) {
470
- BackspaceVariableUnformattedRecord (handler);
471
478
} else {
472
- BackspaceVariableFormattedRecord (handler);
479
+ RUNTIME_CHECK (handler, isUnformatted.has_value ());
480
+ if (isUnformatted.value_or (false )) {
481
+ BackspaceVariableUnformattedRecord (handler);
482
+ } else {
483
+ BackspaceVariableFormattedRecord (handler);
484
+ }
473
485
}
474
486
}
475
487
}
0 commit comments