1 // ***************************************************************************
2 // BamFile_p.cpp (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 October 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides BAM file-specific IO behavior
8 // ***************************************************************************
10 #include "api/internal/BamFile_p.h"
11 using namespace BamTools;
12 using namespace BamTools::Internal;
18 BamFile::BamFile(const string& filename)
20 , m_filename(filename)
23 BamFile::~BamFile(void) { }
25 void BamFile::Close(void) {
28 ILocalIODevice::Close();
32 bool BamFile::IsRandomAccess(void) const {
36 bool BamFile::Open(const IBamIODevice::OpenMode mode) {
38 // make sure we're starting with a fresh file stream
41 // attempt to open FILE* depending on requested openmode
42 if ( mode == IBamIODevice::ReadOnly )
43 m_stream = fopen(m_filename.c_str(), "rb");
44 else if ( mode == IBamIODevice::WriteOnly )
45 m_stream = fopen(m_filename.c_str(), "wb");
47 SetErrorString("BamFile::Open", "unknown open mode requested");
51 // check that we obtained a valid FILE*
52 if ( m_stream == 0 ) {
53 const string message_base = string("could not open file handle for ");
54 const string message = message_base + ( (m_filename.empty()) ? "empty filename" : m_filename );
55 SetErrorString("BamFile::Open", message);
59 // store current IO mode & return success
64 bool BamFile::Seek(const int64_t& position) {
65 BT_ASSERT_X( m_stream, "BamFile::Seek() - null stream" );
66 return ( fseek64(m_stream, position, SEEK_SET) == 0 );