@@ -480,73 +480,74 @@ class BitStreamWriter
480480class AutoFile
481481{
482482protected:
483- FILE* file ;
483+ std:: FILE* m_file ;
484484
485485public:
486- explicit AutoFile (FILE* filenew ) : file{filenew } {}
486+ explicit AutoFile (std:: FILE* file ) : m_file{file } {}
487487
488- ~AutoFile ()
489- {
490- fclose ();
491- }
488+ ~AutoFile () { fclose (); }
492489
493490 // Disallow copies
494491 AutoFile (const AutoFile&) = delete ;
495492 AutoFile& operator =(const AutoFile&) = delete ;
496493
494+ bool feof () const { return std::feof (m_file); }
495+
497496 int fclose ()
498497 {
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 ;
505500 }
506501
507502 /* * Get wrapped FILE* with transfer of ownership.
508503 * @note This will invalidate the AutoFile object, and makes it the responsibility of the caller
509504 * of this function to clean up the returned FILE*.
510505 */
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+ }
512512
513513 /* * Get wrapped FILE* without transfer of ownership.
514514 * @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
515515 * AutoFile outlives use of the passed pointer.
516516 */
517- FILE* Get () const { return file ; }
517+ std:: FILE* Get () const { return m_file ; }
518518
519519 /* * Return true if the wrapped FILE* is nullptr, false otherwise.
520520 */
521- bool IsNull () const { return (file == nullptr ) ; }
521+ bool IsNull () const { return m_file == nullptr ; }
522522
523523 //
524524 // Stream subset
525525 //
526526 void read (Span<std::byte> dst)
527527 {
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" );
531531 }
532532 }
533533
534534 void ignore (size_t nSize)
535535 {
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" );
537537 unsigned char data[4096 ];
538538 while (nSize > 0 ) {
539539 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+ }
542543 nSize -= nNow;
543544 }
544545 }
545546
546547 void write (Span<const std::byte> src)
547548 {
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 ()) {
550551 throw std::ios_base::failure (" AutoFile::write: write failed" );
551552 }
552553 }
0 commit comments