Add parameter 'use_exact_name' to sg_gzifstream's constructor and open() method

This allows one to be sure about which file is opened.
This commit is contained in:
Florent Rougon 2016-10-20 14:07:08 +02:00
parent 11c6e5bf04
commit 7837bd0e11
2 changed files with 22 additions and 13 deletions

View File

@ -43,10 +43,11 @@ sg_gzifstream::sg_gzifstream()
// //
// Open a possibly gzipped file for reading. // Open a possibly gzipped file for reading.
// //
sg_gzifstream::sg_gzifstream( const SGPath& name, ios_openmode io_mode ) sg_gzifstream::sg_gzifstream( const SGPath& name, ios_openmode io_mode,
bool use_exact_name )
: istream(&gzbuf) : istream(&gzbuf)
{ {
this->open( name, io_mode ); this->open( name, io_mode, use_exact_name );
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -62,17 +63,20 @@ sg_gzifstream::sg_gzifstream( int fd, ios_openmode io_mode )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// //
// Open a possibly gzipped file for reading. // Open a possibly gzipped file for reading.
// If the initial open fails and the filename has a ".gz" extension then // If 'use_exact_name' is true, just try to open the indicated file, nothing
// remove the extension and try again. // else. Otherwise:
// If the initial open fails and the filename doesn't have a ".gz" extension // - if the initial open fails and the filename has a ".gz" extension, then
// then append ".gz" and try again. // remove it and try again;
// - if the initial open fails and the filename doesn't have a ".gz"
// extension, then append ".gz" and try again.
// //
void void
sg_gzifstream::open( const SGPath& name, ios_openmode io_mode ) sg_gzifstream::open( const SGPath& name, ios_openmode io_mode,
bool use_exact_name )
{ {
std::string s = name.utf8Str(); std::string s = name.utf8Str();
gzbuf.open( s.c_str(), io_mode ); gzbuf.open( s.c_str(), io_mode );
if ( ! gzbuf.is_open() ) if ( ! (gzbuf.is_open() || use_exact_name) )
{ {
if ( s.substr( s.length() - 3, 3 ) == ".gz" ) if ( s.substr( s.length() - 3, 3 ) == ".gz" )
{ {

View File

@ -54,13 +54,15 @@ public:
sg_gzifstream(); sg_gzifstream();
/** /**
* Constructor that attempt to open a file with and without * Constructor that attempts to open a file.
* ".gz" extension.
* @param name name of file * @param name name of file
* @param io_mode file open mode(s) "or'd" together * @param io_mode file open mode(s) "or'd" together
* @param use_exact_name if false, try to add or remove a ".gz" extension
* in case the indicated file can't be opened
*/ */
sg_gzifstream( const SGPath& name, sg_gzifstream( const SGPath& name,
ios_openmode io_mode = ios_in | ios_binary ); ios_openmode io_mode = ios_in | ios_binary,
bool use_exact_name = false );
/** /**
* Constructor that attaches itself to an existing file descriptor. * Constructor that attaches itself to an existing file descriptor.
@ -70,12 +72,15 @@ public:
sg_gzifstream( int fd, ios_openmode io_mode = ios_in|ios_binary ); sg_gzifstream( int fd, ios_openmode io_mode = ios_in|ios_binary );
/** /**
* Attempt to open a file with and without ".gz" extension. * Attempt to open a file.
* @param name name of file * @param name name of file
* @param io_mode file open mode(s) "or'd" together * @param io_mode file open mode(s) "or'd" together
* @param use_exact_name if false, try to add or remove a ".gz" extension
* in case the indicated file can't be opened
*/ */
void open( const SGPath& name, void open( const SGPath& name,
ios_openmode io_mode = ios_in|ios_binary ); ios_openmode io_mode = ios_in|ios_binary,
bool use_exact_name = false );
/** /**
* Attach to an existing file descriptor. * Attach to an existing file descriptor.