diff --git a/src/utils/bedFile/bedFile.cpp b/src/utils/bedFile/bedFile.cpp index 43ca3de2bb9a7075df7f064dee1cce0ffe5ed6c1..4b7bdc6348ecea298318bbc4ca7ccce89ea4c0ca 100644 --- a/src/utils/bedFile/bedFile.cpp +++ b/src/utils/bedFile/bedFile.cpp @@ -132,39 +132,18 @@ void BedFile::Open(void) { if (bedFile == "stdin" || bedFile == "-") { _bedStream = &cin; } - // New method thanks to Assaf Gordon - else if ((isGzipFile(bedFile) == false) && (isRegularFile(bedFile) == true)) { - // open an ifstream - ifstream beds(bedFile.c_str(), ios::in); - - // can we open the file? - if ( !beds ) { - cerr << "Error: The requested bed file (" << bedFile << ") could not be opened. Exiting!" << endl; - exit (1); - } - else { - // if so, close it (this was just a test) - beds.close(); - // now set a pointer to the stream so that we - _bedStream = new ifstream(bedFile.c_str(), ios::in); + else { + _bedStream = new ifstream(bedFile.c_str(), ios::in); + + if (isGzipFile(_bedStream) == true) { + delete _bedStream; + _bedStream = new igzstream(bedFile.c_str(), ios::in); } - } - else if ((isGzipFile(bedFile) == true) && (isRegularFile(bedFile) == true)) { - igzstream beds(bedFile.c_str(), ios::in); - if ( !beds ) { + // can we open the file? + if ( !(_bedStream->good()) ) { cerr << "Error: The requested bed file (" << bedFile << ") could not be opened. Exiting!" << endl; exit (1); } - else { - // if so, close it (this was just a test) - beds.close(); - // now set a pointer to the stream so that we - _bedStream = new igzstream(bedFile.c_str(), ios::in); - } - } - else { - cerr << "Error: Unexpected file type (" << bedFile << "). Exiting!" << endl; - exit(1); } } diff --git a/src/utils/fileType/fileType.cpp b/src/utils/fileType/fileType.cpp index 5aa05b88224bd1cd50b632a91e8831ae372a059a..bda6d5821fb8ef8545028b3eebbf1ce1af0d34e3 100644 --- a/src/utils/fileType/fileType.cpp +++ b/src/utils/fileType/fileType.cpp @@ -1,64 +1,61 @@ /***************************************************************************** - fileType.cpp +fileType.cpp - (c) 2009 - Aaron Quinlan - Hall Laboratory - Department of Biochemistry and Molecular Genetics - University of Virginia - aaronquinlan@gmail.com +(c) 2009 - Aaron Quinlan +Hall Laboratory +Department of Biochemistry and Molecular Genetics +University of Virginia +aaronquinlan@gmail.com - Licensed under the GNU General Public License 2.0 license. +Licensed under the GNU General Public License 2.0 license. ******************************************************************************/ #include "fileType.h" /* - returns TRUE if the file is a regular file: - not a pipe/device. +returns TRUE if the file is a regular file: +not a pipe/device. - This implies that the file can be opened/closed/seek'd multiple times without losing information - */ +This implies that the file can be opened/closed/seek'd multiple times without losing information +*/ bool isRegularFile(const string& filename) { - struct stat buf ; - int i; - - i = stat(filename.c_str(), &buf); - if (i!=0) { - cerr << "Error: can't determine file type of '" << filename << "': " << strerror(errno) << endl; - exit(1); - } - if (S_ISREG(buf.st_mode)) - return true; - - return false; + struct stat buf ; + int i; + + i = stat(filename.c_str(), &buf); + if (i!=0) { + cerr << "Error: can't determine file type of '" << filename << "': " << strerror(errno) << endl; + exit(1); + } + if (S_ISREG(buf.st_mode)) + return true; + + return false; } /* - returns TRUE if the file has a GZIP header. - Should only be run on regular files. - */ -bool isGzipFile(const string& filename) { +returns TRUE if the file has a GZIP header. +Should only be run on regular files. +*/ +bool isGzipFile(istream *file) { //see http://www.gzip.org/zlib/rfc-gzip.html#file-format - struct { - unsigned char id1; - unsigned char id2; - unsigned char cm; - } gzip_header; - ifstream f(filename.c_str(), ios::in|ios::binary); - if (!f) - return false; - - if (!f.read((char*)&gzip_header, sizeof(gzip_header))) - return false; - - if ( gzip_header.id1 == 0x1f - && - gzip_header.id2 == 0x8b - && - gzip_header.cm == 8 ) - return true; - - return false; + struct { + unsigned char id1; + unsigned char id2; + unsigned char cm; + } gzip_header; + + if (!file->read((char*)&gzip_header, sizeof(gzip_header))) + return false; + + if ( gzip_header.id1 == 0x1f + && + gzip_header.id2 == 0x8b + && + gzip_header.cm == 8 ) + return true; + + return false; } diff --git a/src/utils/fileType/fileType.h b/src/utils/fileType/fileType.h index adf2d6215fd1e277375dbc0d20b74baf4f08a679..b1d422440273ae97bda27518b9e6de7b26718258 100644 --- a/src/utils/fileType/fileType.h +++ b/src/utils/fileType/fileType.h @@ -33,6 +33,6 @@ using namespace std; ******************************************************************************/ string string_error(int errnum); bool isRegularFile(const string& filename); -bool isGzipFile(const string& filename); +bool isGzipFile(istream *file); #endif /* FILETYPE_H */