]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/ILocalIODevice_p.cpp
Fixed: improper mode check in local I/O device
[bamtools.git] / src / api / internal / io / ILocalIODevice_p.cpp
1 // ***************************************************************************
2 // ILocalIODevice_p.cpp (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 27 July 2012 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides shared behavior for files & pipes
8 // ***************************************************************************
9
10 #include "api/internal/io/ILocalIODevice_p.h"
11 using namespace BamTools;
12 using namespace BamTools::Internal;
13
14 #include <cstdio>
15 using namespace std;
16
17 ILocalIODevice::ILocalIODevice(void)
18     : IBamIODevice()
19     , m_stream(0)
20 { }
21
22 ILocalIODevice::~ILocalIODevice(void) {
23     Close();
24 }
25
26 void ILocalIODevice::Close(void) {
27
28     // skip if not open
29     if ( !IsOpen() )
30         return;
31
32     // flush & close FILE*
33     fflush(m_stream);
34     fclose(m_stream);
35     m_stream = 0;
36
37     // reset other device state
38     m_mode = IBamIODevice::NotOpen;
39 }
40
41 int64_t ILocalIODevice::Read(char* data, const unsigned int numBytes) {
42     BT_ASSERT_X( m_stream, "ILocalIODevice::Read: trying to read from null stream" );
43     BT_ASSERT_X( (m_mode & IBamIODevice::ReadOnly), "ILocalIODevice::Read: device not in read-able mode");
44     return static_cast<int64_t>( fread(data, sizeof(char), numBytes, m_stream) );
45 }
46
47 int64_t ILocalIODevice::Tell(void) const {
48     BT_ASSERT_X( m_stream, "ILocalIODevice::Tell: trying to get file position fromnull stream" );
49     return ftell64(m_stream);
50 }
51
52 int64_t ILocalIODevice::Write(const char* data, const unsigned int numBytes) {
53     BT_ASSERT_X( m_stream, "ILocalIODevice::Write: tryint to write to null stream" );
54     BT_ASSERT_X( (m_mode & IBamIODevice::WriteOnly), "ILocalIODevice::Write: device not in write-able mode" );
55     return static_cast<int64_t>( fwrite(data, sizeof(char), numBytes, m_stream) );
56 }