]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/ILocalIODevice_p.cpp
Cleaned up intra-API includes & moved version numbers to 2.0.0
[bamtools.git] / src / api / internal / ILocalIODevice_p.cpp
1 // ***************************************************************************
2 // ILocalIODevice_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 shared behavior for files & pipes
8 // ***************************************************************************
9
10 #include "api/internal/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 size_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-only mode");
44     return 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 size_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-only mode" );
55     return fwrite(data, sizeof(char), numBytes, m_stream);
56 }