@@ -476,6 +476,17 @@ std::unique_ptr<std::istream> read_maybe_compressed_file( const std::string &pat
476
476
return read_maybe_compressed_file ( std::filesystem::u8path ( path ) );
477
477
}
478
478
479
+ static bool is_gzipped ( std::ifstream &fin )
480
+ {
481
+ // (byte1 == 0x1f) && (byte2 == 0x8b)
482
+ std::array<char , 2 > header;
483
+ fin.read ( header.data (), 2 );
484
+ fin.clear ();
485
+ fin.seekg ( 0 , std::ios::beg ); // reset read position
486
+
487
+ return ( header[0 ] == ' \x1f ' ) && ( header[1 ] == ' \x8b ' );
488
+ }
489
+
479
490
std::unique_ptr<std::istream> read_maybe_compressed_file ( const std::filesystem::path &path )
480
491
{
481
492
try {
@@ -484,14 +495,7 @@ std::unique_ptr<std::istream> read_maybe_compressed_file( const std::filesystem:
484
495
throw std::runtime_error ( " opening file failed" );
485
496
}
486
497
487
- // check if file is gzipped
488
- // (byte1 == 0x1f) && (byte2 == 0x8b)
489
- std::array<char , 2 > header;
490
- fin.read ( header.data (), 2 );
491
- fin.clear ();
492
- fin.seekg ( 0 , std::ios::beg ); // reset read position
493
-
494
- if ( ( header[0 ] == ' \x1f ' ) && ( header[1 ] == ' \x8b ' ) ) {
498
+ if ( is_gzipped ( fin ) ) {
495
499
std::string outstring = read_compressed_file_to_string ( fin );
496
500
std::stringstream inflated_contents_stream;
497
501
inflated_contents_stream.write ( outstring.data (), outstring.size () );
@@ -528,14 +532,7 @@ std::optional<std::string> read_whole_file( const std::filesystem::path &path )
528
532
throw std::runtime_error ( " opening file failed" );
529
533
}
530
534
531
- // check if file is gzipped
532
- // (byte1 == 0x1f) && (byte2 == 0x8b)
533
- std::array<char , 2 > header;
534
- fin.read ( header.data (), 2 );
535
- fin.clear ();
536
- fin.seekg ( 0 , std::ios::beg ); // reset read position
537
-
538
- if ( ( header[0 ] == ' \x1f ' ) && ( header[1 ] == ' \x8b ' ) ) {
535
+ if ( is_gzipped ( fin ) ) {
539
536
outstring = read_compressed_file_to_string ( fin );
540
537
} else {
541
538
fin.seekg ( 0 , std::ios_base::end );
0 commit comments