Skip to content

Commit 76dab02

Browse files
authored
Dedupe is_gzipped (#82454)
1 parent 14599f0 commit 76dab02

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/cata_utility.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,17 @@ std::unique_ptr<std::istream> read_maybe_compressed_file( const std::string &pat
476476
return read_maybe_compressed_file( std::filesystem::u8path( path ) );
477477
}
478478

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+
479490
std::unique_ptr<std::istream> read_maybe_compressed_file( const std::filesystem::path &path )
480491
{
481492
try {
@@ -484,14 +495,7 @@ std::unique_ptr<std::istream> read_maybe_compressed_file( const std::filesystem:
484495
throw std::runtime_error( "opening file failed" );
485496
}
486497

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 ) ) {
495499
std::string outstring = read_compressed_file_to_string( fin );
496500
std::stringstream inflated_contents_stream;
497501
inflated_contents_stream.write( outstring.data(), outstring.size() );
@@ -528,14 +532,7 @@ std::optional<std::string> read_whole_file( const std::filesystem::path &path )
528532
throw std::runtime_error( "opening file failed" );
529533
}
530534

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 ) ) {
539536
outstring = read_compressed_file_to_string( fin );
540537
} else {
541538
fin.seekg( 0, std::ios_base::end );

0 commit comments

Comments
 (0)