X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2Fapi%2Finternal%2FBamFile_p.cpp;h=74c4ed68d559d30aee7cf243fc8bc042b21bb5ea;hb=9f1ce8c47aeadb6dc1320b52ee671c3341b97935;hp=d9c8673a8163d88757dc64096172548cb76b8f47;hpb=88577e25bbf4b6b43642cb679c5f9f5cba026fec;p=bamtools.git diff --git a/src/api/internal/BamFile_p.cpp b/src/api/internal/BamFile_p.cpp index d9c8673..74c4ed6 100644 --- a/src/api/internal/BamFile_p.cpp +++ b/src/api/internal/BamFile_p.cpp @@ -2,42 +2,31 @@ // BamFile_p.cpp (c) 2011 Derek Barnett // Marth Lab, Department of Biology, Boston College // --------------------------------------------------------------------------- -// Last modified: 8 September 2011 (DB) +// Last modified: 10 October 2011 (DB) // --------------------------------------------------------------------------- -// Provides reading of local BAM files +// Provides BAM file-specific IO behavior // *************************************************************************** -#include +#include "api/internal/BamFile_p.h" using namespace BamTools; using namespace BamTools::Internal; #include +#include 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 { @@ -55,15 +44,15 @@ bool BamFile::Open(const IBamIODevice::OpenMode mode) { else if ( mode == IBamIODevice::WriteOnly ) m_stream = fopen(m_filename.c_str(), "wb"); else { - SetErrorString("BamFile ERROR - unknown device open mode"); + SetErrorString("BamFile::Open", "unknown open mode requested"); return false; } // check that we obtained a valid FILE* if ( m_stream == 0 ) { - string error = "BamFile ERROR - could not open handle on "; - error += ( (m_filename.empty()) ? "empty filename" : m_filename ); - SetErrorString(error); + const string message_base = string("could not open file handle for "); + const string message = message_base + ( (m_filename.empty()) ? "empty filename" : m_filename ); + SetErrorString("BamFile::Open", message); return false; } @@ -72,24 +61,7 @@ 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" ); - 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); + return ( fseek64(m_stream, position, SEEK_SET) == 0 ); }