]> git.donarmstrong.com Git - bamtools.git/commitdiff
Refactored shared pipe/file behavior into ILocalIODevice
authorderek <derekwbarnett@gmail.com>
Fri, 9 Sep 2011 19:09:53 +0000 (15:09 -0400)
committerderek <derekwbarnett@gmail.com>
Fri, 9 Sep 2011 19:09:53 +0000 (15:09 -0400)
13 files changed:
src/api/CMakeLists.txt
src/api/IBamIODevice.h
src/api/internal/BamFile_p.cpp
src/api/internal/BamFile_p.h
src/api/internal/BamPipe_p.cpp
src/api/internal/BamPipe_p.h
src/api/internal/BamReader_p.cpp
src/api/internal/BamWriter_p.cpp
src/api/internal/BgzfStream_p.cpp
src/api/internal/ILocalIODevice_p.cpp [new file with mode: 0644]
src/api/internal/ILocalIODevice_p.h [new file with mode: 0644]
src/api/internal/IRemoteIODevice_p.cpp [new file with mode: 0644]
src/api/internal/IRemoteIODevice_p.h [new file with mode: 0644]

index e9c3b65785969721e4b591752d7485f7ff239f9f..5a0371269b0d24a640ebd00d7aa2939c1f3aa6b7 100644 (file)
@@ -39,6 +39,8 @@ set( BamToolsAPISources
         internal/BamToolsIndex_p.cpp
         internal/BamWriter_p.cpp
         internal/BgzfStream_p.cpp
+        internal/ILocalIODevice_p.cpp
+        internal/IRemoteIODevice_p.cpp
         internal/SamFormatParser_p.cpp
         internal/SamFormatPrinter_p.cpp
         internal/SamHeaderValidator_p.cpp
index c59bb4a9a88bb0060c25b25b1cc0ea7c8ceb1d5f..99454b21be0f2e837dcb9a15333cae5b9353ef84 100644 (file)
@@ -21,6 +21,8 @@ class API_EXPORT IBamIODevice {
 
     // IBamIODevice interface
     public:
+
+        // pure virtuals
         virtual void Close(void) =0;
         virtual bool IsRandomAccess(void) const =0;
         virtual bool Open(const OpenMode mode) =0;
@@ -28,7 +30,8 @@ class API_EXPORT IBamIODevice {
         virtual bool Seek(const int64_t& position) =0;
         virtual int64_t Tell(void) const =0;
         virtual size_t Write(const char* data, const unsigned int numBytes) =0;
-    public:
+
+        // default implementation provided
         virtual std::string ErrorString(void);
         virtual bool IsOpen(void) const;
         virtual OpenMode Mode(void) const;
index d9c8673a8163d88757dc64096172548cb76b8f47..6cb5be49845d445f04d0908f709027375c58ae6e 100644 (file)
@@ -2,9 +2,9 @@
 // BamFile_p.cpp (c) 2011 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 8 September 2011 (DB)
+// Last modified: 9 September 2011 (DB)
 // ---------------------------------------------------------------------------
-// Provides reading of local BAM files
+// Provides BAM file-specific IO behavior
 // ***************************************************************************
 
 #include <api/internal/BamFile_p.h>
@@ -12,32 +12,21 @@ using namespace BamTools;
 using namespace BamTools::Internal;
 
 #include <cstdio>
+#include <iostream>
 using namespace std;
 
 BamFile::BamFile(const string& filename)
-    : IBamIODevice()
-    , m_stream(0)
+    : ILocalIODevice()
     , m_filename(filename)
 { }
 
-BamFile::~BamFile(void) {
-    Close();
-}
+BamFile::~BamFile(void) { }
 
 void BamFile::Close(void) {
-
-    // skip if not open
-    if ( !IsOpen() )
-        return;
-
-    // flush & close FILE*
-    fflush(m_stream);
-    fclose(m_stream);
-
-    // reset internals
-    m_mode = IBamIODevice::NotOpen;
-    m_stream = 0;
-    m_filename.clear();
+    if ( IsOpen() ) {
+        m_filename.clear();
+        ILocalIODevice::Close();
+    }
 }
 
 bool BamFile::IsRandomAccess(void) const {
@@ -72,24 +61,8 @@ bool BamFile::Open(const IBamIODevice::OpenMode mode) {
     return true;
 }
 
-size_t BamFile::Read(char* data, const unsigned int numBytes) {
-    BT_ASSERT_X( m_stream, "BamFile::Read() - null stream" );
-    BT_ASSERT_X( (m_mode == IBamIODevice::ReadOnly), "BamFile::Read() - device not in read-only mode");
-    return fread(data, sizeof(char), numBytes, m_stream);
-}
-
 bool BamFile::Seek(const int64_t& position) {
     BT_ASSERT_X( m_stream, "BamFile::Seek() - null stream" );
+    cerr << "BamFile::Seek() - about to attempt seek" << endl;
     return ( fseek64(m_stream, position, SEEK_SET) == 0);
 }
-
-int64_t BamFile::Tell(void) const {
-    BT_ASSERT_X( m_stream, "BamFile::Tell() - null stream" );
-    return ftell64(m_stream);
-}
-
-size_t BamFile::Write(const char* data, const unsigned int numBytes) {
-    BT_ASSERT_X( m_stream, "BamFile::Write() - null stream" );
-    BT_ASSERT_X( (m_mode == IBamIODevice::WriteOnly), "BamFile::Write() - device not in write-only mode" );
-    return fwrite(data, sizeof(char), numBytes, m_stream);
-}
index d0712ef0bb870b11f4738d698cd453c9ef9c5f40..fd25a3e007787032c3bf0ec746de9e336a2bbf83 100644 (file)
@@ -2,9 +2,9 @@
 // BamFile_p.h (c) 2011 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 8 September 2011 (DB)
+// Last modified: 9 September 2011 (DB)
 // ---------------------------------------------------------------------------
-// Provides reading/writing of local BAM files
+// Provides BAM file-specific IO behavior
 // ***************************************************************************
 
 #ifndef BAMFILE_P_H
 //
 // We mean it.
 
-#include <api/IBamIODevice.h>
+#include <api/internal/ILocalIODevice_p.h>
 #include <string>
 
 namespace BamTools {
 namespace Internal {
 
-class BamFile : public IBamIODevice {
+class BamFile : public ILocalIODevice {
 
     // ctor & dtor
     public:
         BamFile(const std::string& filename);
         ~BamFile(void);
 
-    // IBamIODevice implementation
+    // ILocalIODevice implementation
     public:
         void Close(void);
         bool IsRandomAccess(void) const;
         bool Open(const IBamIODevice::OpenMode mode);
-        size_t Read(char* data, const unsigned int numBytes);
         bool Seek(const int64_t& position);
-        int64_t Tell(void) const;
-        size_t Write(const char* data, const unsigned int numBytes);
-
-    // internal methods
-    private:
 
     // data members
     private:
-        FILE* m_stream;
         std::string m_filename;
 };
 
index 6bfbf6f7397bc0ff558b7b0b73d827bb5bf3ad6d..62fd789aa21e9784dc0cb2819ad6130931785bc8 100644 (file)
@@ -2,9 +2,9 @@
 // BamPipe_p.cpp (c) 2011 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 8 September 2011 (DB)
+// Last modified: 9 September 2011 (DB)
 // ---------------------------------------------------------------------------
-// Provides reading/writing of piped BAM files (stdin/stdout)
+// Provides BAM pipe-specific IO behavior
 // ***************************************************************************
 
 #include <api/internal/BamPipe_p.h>
@@ -12,30 +12,12 @@ using namespace BamTools;
 using namespace BamTools::Internal;
 
 #include <cstdio>
+#include <iostream>
 using namespace std;
 
-BamPipe::BamPipe(void)
-    : IBamIODevice()
-    , m_stream(0)
-{ }
+BamPipe::BamPipe(void) : ILocalIODevice() { }
 
-BamPipe::~BamPipe(void) {
-    Close();
-}
-
-void BamPipe::Close(void) {
-
-    // skip if not open
-    if ( !IsOpen() ) return;
-
-    // flush & close FILE*
-    fflush(m_stream);
-    fclose(m_stream);
-
-    // reset internals
-    m_mode = IBamIODevice::NotOpen;
-    m_stream = 0;
-}
+BamPipe::~BamPipe(void) { }
 
 bool BamPipe::IsRandomAccess(void) const {
     return false;
@@ -69,24 +51,18 @@ bool BamPipe::Open(const IBamIODevice::OpenMode mode) {
     return true;
 }
 
-size_t BamPipe::Read(char* data, const unsigned int numBytes) {
-    BT_ASSERT_X( m_stream, "BamPipe::Read() - null stream" );
-    BT_ASSERT_X( (m_mode == IBamIODevice::ReadOnly), "BamPipe::Read() - device not in read-only mode");
-    return fread(data, sizeof(char), numBytes, m_stream);
-}
-
 bool BamPipe::Seek(const int64_t& position) {
-    (void)position; // suppress compiler warning about unused variable
-    return false;   // seeking not allowed in pipe
-}
+//    (void)position; // suppress compiler warning about unused variable
+//    return false;   // seeking not allowed in pipe
+
+    BT_ASSERT_X( m_stream, "BamFile::Seek() - null stream" );
+    cerr << "BamPipe::Seek() - about to attempt seek" << endl;
+    bool result = ( fseek64(m_stream, position, SEEK_SET) == 0);
+    if ( !result ) {
+        cerr << "BamPipe can't be seeked in" << endl;
+    }
+    return result;
 
-int64_t BamPipe::Tell(void) const {
-    BT_ASSERT_X( m_stream, "BamPipe::Tell() - null stream" );
-    return ftell64(m_stream);
-}
+//    return ( fseek64(m_stream, position, SEEK_SET) == 0);
 
-size_t BamPipe::Write(const char* data, const unsigned int numBytes) {
-    BT_ASSERT_X( m_stream, "BamPipe::Write() - null stream" );
-    BT_ASSERT_X( (m_mode == IBamIODevice::WriteOnly), "BamPipe::Write() - device not in write-only mode" );
-    return fwrite(data, sizeof(char), numBytes, m_stream);
 }
index 386ede0b922e6f225f51c90ad8e0dbd37341c710..a9725be7153bea5a08dbd221affe744bc903c55d 100644 (file)
@@ -4,7 +4,7 @@
 // ---------------------------------------------------------------------------
 // Last modified: 8 September 2011 (DB)
 // ---------------------------------------------------------------------------
-// Provides reading/writing of piped BAM files (stdin/stdout)
+// Provides BAM pipe-specific IO behavior
 // ***************************************************************************
 
 #ifndef BAMPIPE_P_H
 //
 // We mean it.
 
-#include <api/IBamIODevice.h>
+#include <api/internal/ILocalIODevice_p.h>
 #include <string>
 
 namespace BamTools {
 namespace Internal {
 
-class BamPipe : public IBamIODevice {
+class BamPipe : public ILocalIODevice {
 
     // ctor & dtor
     public:
@@ -35,20 +35,9 @@ class BamPipe : public IBamIODevice {
 
     // IBamIODevice implementation
     public:
-        void Close(void);
         bool IsRandomAccess(void) const;
         bool Open(const IBamIODevice::OpenMode mode);
-        size_t Read(char* data, const unsigned int numBytes);
         bool Seek(const int64_t& position);
-        int64_t Tell(void) const;
-        size_t Write(const char* data, const unsigned int numBytes);
-
-    // internal methods
-    private:
-
-    // data members
-    private:
-        FILE* m_stream;
 };
 
 } // namespace Internal
index 87fe310499847103e4e6a213a5a7134b5378e4ed..e4e2310bb451b0490f6086cb5f88ced80060f7aa 100644 (file)
@@ -344,17 +344,23 @@ bool BamReaderPrivate::OpenIndex(const std::string& indexFilename) {
 // returns BAM file pointer to beginning of alignment data
 bool BamReaderPrivate::Rewind(void) {
 
-    if ( !m_stream.IsOpen() )
+    if ( !m_stream.IsOpen() ) {
+        cerr << "BRP::Rewind() - stream not open" << endl;
         return false;
+    }
 
     // attempt rewind to first alignment
-    if ( !m_stream.Seek(m_alignmentsBeginOffset) )
+    if ( !m_stream.Seek(m_alignmentsBeginOffset) ) {
+        cerr << "BRP::Rewind() - could not seek to ABO (1st time)" << endl;
         return false;
+    }
 
     // verify that we can read first alignment
     BamAlignment al;
-    if ( !LoadNextAlignment(al) )
+    if ( !LoadNextAlignment(al) ) {
+        cerr << "BRP::Rewind() - could not read first alignment" << endl;
         return false;
+    }
 
     // reset region
     m_randomAccessController.ClearRegion();
index 508b6da06ed08b461287558bee8f7fa7ed8ca1c1..8f1b167d26e42a021c934071cd1b5899d175b261 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <api/BamAlignment.h>
 #include <api/BamConstants.h>
+#include <api/IBamIODevice.h>
 #include <api/internal/BamWriter_p.h>
 using namespace BamTools;
 using namespace BamTools::Internal;
@@ -134,7 +135,7 @@ bool BamWriterPrivate::Open(const string& filename,
                             const RefVector& referenceSequences)
 {
     // open the BGZF file for writing, return failure if error
-    if ( !m_stream.Open(filename, "wb") )
+    if ( !m_stream.Open(filename, IBamIODevice::WriteOnly) )
         return false;
 
     // write BAM file 'metadata' components
index b79164decabcf7d49feba9c6455da4eed96fa979..4db9b15e25bbda89cec73ce2a0b4f17d8eabba5c 100644 (file)
@@ -428,8 +428,10 @@ bool BgzfStream::Seek(const int64_t& position) {
     BT_ASSERT_X( m_device, "BgzfStream::Seek() - trying to seek on null IO device");
 
     // skip if not open or not seek-able
-    if ( !IsOpen() || !m_device->IsRandomAccess() )
+    if ( !IsOpen() /*|| !m_device->IsRandomAccess()*/ ) {
+        cerr << "BgzfStream::Seek() - device not open" << endl;
         return false;
+    }
 
     // determine adjusted offset & address
     int     blockOffset  = (position & 0xFFFF);
@@ -437,7 +439,7 @@ bool BgzfStream::Seek(const int64_t& position) {
 
     // attempt seek in file
     if ( !m_device->Seek(blockAddress) ) {
-        fprintf(stderr, "BgzfStream ERROR: unable to seek in file\n");
+        cerr << "BgzfStream ERROR: unable to seek in file" << endl;
         return false;
     }
 
diff --git a/src/api/internal/ILocalIODevice_p.cpp b/src/api/internal/ILocalIODevice_p.cpp
new file mode 100644 (file)
index 0000000..9e9a2b3
--- /dev/null
@@ -0,0 +1,56 @@
+// ***************************************************************************
+// ILocalIODevice_p.cpp (c) 2011 Derek Barnett
+// Marth Lab, Department of Biology, Boston College
+// ---------------------------------------------------------------------------
+// Last modified: 8 September 2011 (DB)
+// ---------------------------------------------------------------------------
+// Provides shared behavior for files & pipes
+// ***************************************************************************
+
+#include <api/internal/ILocalIODevice_p.h>
+using namespace BamTools;
+using namespace BamTools::Internal;
+
+#include <cstdio>
+using namespace std;
+
+ILocalIODevice::ILocalIODevice(void)
+    : IBamIODevice()
+    , m_stream(0)
+{ }
+
+ILocalIODevice::~ILocalIODevice(void) {
+    Close();
+}
+
+void ILocalIODevice::Close(void) {
+
+    // skip if not open
+    if ( !IsOpen() )
+        return;
+
+    // flush & close FILE*
+    fflush(m_stream);
+    fclose(m_stream);
+
+    // reset internals
+    m_mode = IBamIODevice::NotOpen;
+    m_stream = 0;
+}
+
+size_t ILocalIODevice::Read(char* data, const unsigned int numBytes) {
+    BT_ASSERT_X( m_stream, "ILocalIODevice::Read() - null stream" );
+    BT_ASSERT_X( (m_mode == IBamIODevice::ReadOnly), "ILocalIODevice::Read() - device not in read-only mode");
+    return fread(data, sizeof(char), numBytes, m_stream);
+}
+
+int64_t ILocalIODevice::Tell(void) const {
+    BT_ASSERT_X( m_stream, "ILocalIODevice::Tell() - null stream" );
+    return ftell64(m_stream);
+}
+
+size_t ILocalIODevice::Write(const char* data, const unsigned int numBytes) {
+    BT_ASSERT_X( m_stream, "ILocalIODevice::Write() - null stream" );
+    BT_ASSERT_X( (m_mode == IBamIODevice::WriteOnly), "ILocalIODevice::Write() - device not in write-only mode" );
+    return fwrite(data, sizeof(char), numBytes, m_stream);
+}
diff --git a/src/api/internal/ILocalIODevice_p.h b/src/api/internal/ILocalIODevice_p.h
new file mode 100644 (file)
index 0000000..1e64899
--- /dev/null
@@ -0,0 +1,50 @@
+// ***************************************************************************
+// ILocalIODevice_p.h (c) 2011 Derek Barnett
+// Marth Lab, Department of Biology, Boston College
+// ---------------------------------------------------------------------------
+// Last modified: 8 September 2011 (DB)
+// ---------------------------------------------------------------------------
+// Provides shared behavior for files & pipes
+// ***************************************************************************
+
+#ifndef ILOCALIODEVICE_P_H
+#define ILOCALIODEVICE_P_H
+
+//  -------------
+//  W A R N I N G
+//  -------------
+//
+// This file is not part of the BamTools API.  It exists purely as an
+// implementation detail. This header file may change from version to version
+// without notice, or even be removed.
+//
+// We mean it.
+
+#include <api/IBamIODevice.h>
+
+namespace BamTools {
+namespace Internal {
+
+class ILocalIODevice : public IBamIODevice {
+
+    // ctor & dtor
+    public:
+        ILocalIODevice(void);
+        virtual ~ILocalIODevice(void);
+
+    // IBamIODevice implementation
+    public:
+        virtual void Close(void);
+        virtual size_t Read(char* data, const unsigned int numBytes);
+        virtual int64_t Tell(void) const;
+        virtual size_t Write(const char* data, const unsigned int numBytes);
+
+    // data members
+    protected:
+        FILE* m_stream;
+};
+
+} // namespace Internal
+} // namespace BamTools
+
+#endif // ILOCALIODEVICE_P_H
diff --git a/src/api/internal/IRemoteIODevice_p.cpp b/src/api/internal/IRemoteIODevice_p.cpp
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/api/internal/IRemoteIODevice_p.h b/src/api/internal/IRemoteIODevice_p.h
new file mode 100644 (file)
index 0000000..e69de29