From f79903413907b542b16b3fee2eb238e6bf50fbd0 Mon Sep 17 00:00:00 2001
From: Aaron <aaronquinlan@gmail.com>
Date: Wed, 31 Aug 2011 15:31:55 -0400
Subject: [PATCH] BEDTools now supports FIFOs and named pipes. Many thanks Nate
 Weeks and Michael Hoffman for the suggestions.

---
 src/utils/bedFile/bedFile.cpp   | 37 +++-----------
 src/utils/fileType/fileType.cpp | 91 ++++++++++++++++-----------------
 src/utils/fileType/fileType.h   |  2 +-
 3 files changed, 53 insertions(+), 77 deletions(-)

diff --git a/src/utils/bedFile/bedFile.cpp b/src/utils/bedFile/bedFile.cpp
index 43ca3de2..4b7bdc63 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 5aa05b88..bda6d582 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 adf2d621..b1d42244 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 */
-- 
GitLab