]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/BamPipe_p.cpp
Clarified documentation on BamReader::GetNextAlignmentCore
[bamtools.git] / src / api / internal / io / BamPipe_p.cpp
1 // ***************************************************************************
2 // BamPipe_p.cpp (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 18 October 2012 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides BAM pipe-specific IO behavior
8 // ***************************************************************************
9
10 #include "api/internal/io/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 defined( SYSTEM_NODEJS ) && SYSTEM_NODEJS == 1
33     if ( mode == IBamIODevice::ReadOnly )
34         m_stream = stdin;
35     else if ( mode == IBamIODevice::WriteOnly )
36         m_stream = stdout;
37 #else
38     if ( mode == IBamIODevice::ReadOnly )
39         m_stream = freopen(0, "rb", stdin);
40     else if ( mode == IBamIODevice::WriteOnly )
41         m_stream = freopen(0, "wb", stdout);
42 #endif // SYSTEM_NODEJS
43
44     else {
45         const string errorType = string( (mode == IBamIODevice::ReadWrite) ? "unsupported"
46                                                                            : "unknown" );
47         const string message = errorType + " open mode requested";
48         SetErrorString("BamPipe::Open", message);
49         return false;
50     }
51
52     // check that we obtained a valid FILE*
53     if ( m_stream == 0 ) {
54         const string message_base = string("could not open handle on ");
55         const string message = message_base + ( (mode == IBamIODevice::ReadOnly) ? "stdin"
56                                                                                  : "stdout" );
57         SetErrorString("BamPipe::Open", message);
58         return false;
59     }
60
61     // store current IO mode & return success
62     m_mode = mode;
63     return true;
64 }
65
66 bool BamPipe::Seek(const int64_t&, const int) {
67     SetErrorString("BamPipe::Seek", "random access not allowed in FIFO pipe");
68     return false;
69 }