@@ -480,73 +480,74 @@ class BitStreamWriter
480
480
class AutoFile
481
481
{
482
482
protected:
483
- FILE* file ;
483
+ std:: FILE* m_file ;
484
484
485
485
public:
486
- explicit AutoFile (FILE* filenew ) : file{filenew } {}
486
+ explicit AutoFile (std:: FILE* file ) : m_file{file } {}
487
487
488
- ~AutoFile ()
489
- {
490
- fclose ();
491
- }
488
+ ~AutoFile () { fclose (); }
492
489
493
490
// Disallow copies
494
491
AutoFile (const AutoFile&) = delete ;
495
492
AutoFile& operator =(const AutoFile&) = delete ;
496
493
494
+ bool feof () const { return std::feof (m_file); }
495
+
497
496
int fclose ()
498
497
{
499
- int retval{0 };
500
- if (file) {
501
- retval = ::fclose (file);
502
- file = nullptr ;
503
- }
504
- return retval;
498
+ if (auto rel{release ()}) return std::fclose (rel);
499
+ return 0 ;
505
500
}
506
501
507
502
/* * Get wrapped FILE* with transfer of ownership.
508
503
* @note This will invalidate the AutoFile object, and makes it the responsibility of the caller
509
504
* of this function to clean up the returned FILE*.
510
505
*/
511
- FILE* release () { FILE* ret = file; file = nullptr ; return ret; }
506
+ std::FILE* release ()
507
+ {
508
+ std::FILE* ret{m_file};
509
+ m_file = nullptr ;
510
+ return ret;
511
+ }
512
512
513
513
/* * Get wrapped FILE* without transfer of ownership.
514
514
* @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
515
515
* AutoFile outlives use of the passed pointer.
516
516
*/
517
- FILE* Get () const { return file ; }
517
+ std:: FILE* Get () const { return m_file ; }
518
518
519
519
/* * Return true if the wrapped FILE* is nullptr, false otherwise.
520
520
*/
521
- bool IsNull () const { return (file == nullptr ) ; }
521
+ bool IsNull () const { return m_file == nullptr ; }
522
522
523
523
//
524
524
// Stream subset
525
525
//
526
526
void read (Span<std::byte> dst)
527
527
{
528
- if (!file ) throw std::ios_base::failure (" AutoFile::read: file handle is nullptr" );
529
- if (fread (dst.data (), 1 , dst.size (), file ) != dst.size ()) {
530
- throw std::ios_base::failure (feof (file ) ? " AutoFile::read: end of file" : " AutoFile::read: fread failed" );
528
+ if (!m_file ) throw std::ios_base::failure (" AutoFile::read: file handle is nullptr" );
529
+ if (std:: fread (dst.data (), 1 , dst.size (), m_file ) != dst.size ()) {
530
+ throw std::ios_base::failure (feof () ? " AutoFile::read: end of file" : " AutoFile::read: fread failed" );
531
531
}
532
532
}
533
533
534
534
void ignore (size_t nSize)
535
535
{
536
- if (!file ) throw std::ios_base::failure (" AutoFile::ignore: file handle is nullptr" );
536
+ if (!m_file ) throw std::ios_base::failure (" AutoFile::ignore: file handle is nullptr" );
537
537
unsigned char data[4096 ];
538
538
while (nSize > 0 ) {
539
539
size_t nNow = std::min<size_t >(nSize, sizeof (data));
540
- if (fread (data, 1 , nNow, file) != nNow)
541
- throw std::ios_base::failure (feof (file) ? " AutoFile::ignore: end of file" : " AutoFile::read: fread failed" );
540
+ if (std::fread (data, 1 , nNow, m_file) != nNow) {
541
+ throw std::ios_base::failure (feof () ? " AutoFile::ignore: end of file" : " AutoFile::ignore: fread failed" );
542
+ }
542
543
nSize -= nNow;
543
544
}
544
545
}
545
546
546
547
void write (Span<const std::byte> src)
547
548
{
548
- if (!file ) throw std::ios_base::failure (" AutoFile::write: file handle is nullptr" );
549
- if (fwrite (src.data (), 1 , src.size (), file ) != src.size ()) {
549
+ if (!m_file ) throw std::ios_base::failure (" AutoFile::write: file handle is nullptr" );
550
+ if (std:: fwrite (src.data (), 1 , src.size (), m_file ) != src.size ()) {
550
551
throw std::ios_base::failure (" AutoFile::write: write failed" );
551
552
}
552
553
}
0 commit comments