]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/api/internal/BamPipe_p.cpp
Refactored shared pipe/file behavior into ILocalIODevice
[bamtools.git] / src / api / internal / BamPipe_p.cpp
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);
 }