]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/BgzfStream_p.h
Removed STDERR pollution by API
[bamtools.git] / src / api / internal / BgzfStream_p.h
1 // ***************************************************************************
2 // BgzfStream_p.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 5 April 2011(DB)
6 // ---------------------------------------------------------------------------
7 // Based on BGZF routines developed at the Broad Institute.
8 // Provides the basic functionality for reading & writing BGZF files
9 // Replaces the old BGZF.* files to avoid clashing with other toolkits
10 // ***************************************************************************
11
12 #ifndef BGZFSTREAM_P_H
13 #define BGZFSTREAM_P_H
14
15 //  -------------
16 //  W A R N I N G
17 //  -------------
18 //
19 // This file is not part of the BamTools API.  It exists purely as an
20 // implementation detail. This header file may change from version to version
21 // without notice, or even be removed.
22 //
23 // We mean it.
24
25 #include <api/BamAux.h>
26 #include <api/BamConstants.h>
27 #include "zlib.h"
28 #include <cstdio>
29 #include <memory>
30 #include <string>
31
32 namespace BamTools {
33 namespace Internal {
34
35 class BgzfStream {
36
37     // constructor & destructor
38     public:
39         BgzfStream(void);
40         ~BgzfStream(void);
41
42     // main interface methods
43     public:
44         // closes BGZF file
45         void Close(void);
46         // opens the BGZF file (mode is either "rb" for reading, or "wb" for writing)
47         void Open(const std::string& filename, const char* mode);
48         // reads BGZF data into a byte buffer
49         size_t Read(char* data, const size_t dataLength);
50         // seek to position in BGZF file
51         void Seek(const int64_t& position);
52         // enable/disable compressed output
53         void SetWriteCompressed(bool ok);
54         // get file position in BGZF file
55         int64_t Tell(void) const;
56         // writes the supplied data into the BGZF buffer
57         size_t Write(const char* data, const size_t dataLength);
58
59     // internal methods
60     private:
61         // compresses the current block
62         size_t DeflateBlock(void);
63         // flushes the data in the BGZF block
64         void FlushBlock(void);
65         // de-compresses the current block
66         size_t InflateBlock(const size_t& blockLength);
67         // reads a BGZF block
68         void ReadBlock(void);
69
70     // static 'utility' methods
71     public:
72         // checks BGZF block header
73         static bool CheckBlockHeader(char* header);
74
75     // data members
76     public:
77         unsigned int BlockLength;
78         unsigned int BlockOffset;
79         int64_t BlockAddress;
80         bool IsOpen;
81         bool IsWriteOnly;
82         bool IsWriteCompressed;
83
84         struct RaiiWrapper {
85             RaiiWrapper(void);
86             ~RaiiWrapper(void);
87             char* UncompressedBlock;
88             char* CompressedBlock;
89             FILE* Stream;
90         };
91         RaiiWrapper Resources;
92
93 };
94
95 } // namespace Internal
96 } // namespace BamTools
97
98 #endif // BGZFSTREAM_P_H