]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/BamPipe_p.cpp
62fd789aa21e9784dc0cb2819ad6130931785bc8
[bamtools.git] / src / api / internal / BamPipe_p.cpp
1 // ***************************************************************************
2 // BamPipe_p.cpp (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 9 September 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides BAM pipe-specific IO behavior
8 // ***************************************************************************
9
10 #include <api/internal/BamPipe_p.h>
11 using namespace BamTools;
12 using namespace BamTools::Internal;
13
14 #include <cstdio>
15 #include <iostream>
16 using namespace std;
17
18 BamPipe::BamPipe(void) : ILocalIODevice() { }
19
20 BamPipe::~BamPipe(void) { }
21
22 bool BamPipe::IsRandomAccess(void) const {
23     return false;
24 }
25
26 bool BamPipe::Open(const IBamIODevice::OpenMode mode) {
27
28     // make sure we're starting with a fresh pipe
29     Close();
30
31     // open stdin/stdout depending on requested openmode
32     if ( mode == IBamIODevice::ReadOnly )
33         m_stream = freopen(0, "rb", stdin);
34     else if ( mode == IBamIODevice::WriteOnly )
35         m_stream = freopen(0, "wb", stdout);
36     else {
37         SetErrorString("BamPipe ERROR - unsupported device mode");
38         return false;
39     }
40
41     // check that we obtained a valid FILE*
42     if ( m_stream == 0 ) {
43         string error = "BamPipe ERROR - could not open handle on ";
44         error += ( (mode == IBamIODevice::ReadOnly) ? "stdin" : "stdout" );
45         SetErrorString(error);
46         return false;
47     }
48
49     // store current IO mode & return success
50     m_mode = mode;
51     return true;
52 }
53
54 bool BamPipe::Seek(const int64_t& position) {
55 //    (void)position; // suppress compiler warning about unused variable
56 //    return false;   // seeking not allowed in pipe
57
58     BT_ASSERT_X( m_stream, "BamFile::Seek() - null stream" );
59     cerr << "BamPipe::Seek() - about to attempt seek" << endl;
60     bool result = ( fseek64(m_stream, position, SEEK_SET) == 0);
61     if ( !result ) {
62         cerr << "BamPipe can't be seeked in" << endl;
63     }
64     return result;
65
66 //    return ( fseek64(m_stream, position, SEEK_SET) == 0);
67
68 }