1 // ***************************************************************************
2 // BamFile_p.cpp (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 25 October 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides BAM file-specific IO behavior
8 // ***************************************************************************
10 #include "api/internal/io/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");
46 else if ( mode == IBamIODevice::ReadWrite )
47 m_stream = fopen(m_filename.c_str(), "w+b");
49 SetErrorString("BamFile::Open", "unknown open mode requested");
53 // check that we obtained a valid FILE*
54 if ( m_stream == 0 ) {
55 const string message_base = string("could not open file handle for ");
56 const string message = message_base + ( (m_filename.empty()) ? "empty filename" : m_filename );
57 SetErrorString("BamFile::Open", message);
61 // store current IO mode & return success
66 bool BamFile::Seek(const int64_t& position, const int origin) {
67 BT_ASSERT_X( m_stream, "BamFile::Seek() - null stream" );
68 return ( fseek64(m_stream, position, origin) == 0 );