]> git.donarmstrong.com Git - bamtools.git/blobdiff - BGZF.h
Minor formatting cleanup in BamIndex.*
[bamtools.git] / BGZF.h
diff --git a/BGZF.h b/BGZF.h
index 0cca24ccc344b893a04705f791d0de06d50ce090..37bcff75bd2a3a93bb9665c21b250f2b69321246 100644 (file)
--- a/BGZF.h
+++ b/BGZF.h
@@ -3,7 +3,7 @@
 // Marth Lab, Department of Biology, Boston College\r
 // All rights reserved.\r
 // ---------------------------------------------------------------------------\r
-// Last modified: 11 January 2010 (DB)\r
+// Last modified: 16 August 2010 (DB)\r
 // ---------------------------------------------------------------------------\r
 // BGZF routines were adapted from the bgzf.c code developed at the Broad\r
 // Institute.\r
 // zlib includes\r
 #include "zlib.h"\r
 \r
+// Platform-specific large-file support\r
+#ifndef BAMTOOLS_LFS\r
+#define BAMTOOLS_LFS\r
+    #ifdef WIN32\r
+        #define ftell64(a)     _ftelli64(a)\r
+        #define fseek64(a,b,c) _fseeki64(a,b,c)\r
+    #else\r
+        #define ftell64(a)     ftello(a)\r
+        #define fseek64(a,b,c) fseeko(a,b,c) \r
+    #endif\r
+#endif // BAMTOOLS_LFS\r
+\r
 // Platform-specific type definitions\r
 #ifndef BAMTOOLS_TYPES\r
 #define BAMTOOLS_TYPES\r
-       #ifdef _MSC_VER\r
-               typedef char                 int8_t;\r
-               typedef unsigned char       uint8_t;\r
-               typedef short               int16_t;\r
-               typedef unsigned short     uint16_t;\r
-               typedef int                 int32_t;\r
-               typedef unsigned int       uint32_t;\r
-               typedef long long           int64_t;\r
-               typedef unsigned long long uint64_t;\r
-       #else\r
-               #include <stdint.h>\r
-       #endif\r
+    #ifdef _MSC_VER\r
+        typedef char                 int8_t;\r
+        typedef unsigned char       uint8_t;\r
+        typedef short               int16_t;\r
+        typedef unsigned short     uint16_t;\r
+        typedef int                 int32_t;\r
+        typedef unsigned int       uint32_t;\r
+        typedef long long           int64_t;\r
+        typedef unsigned long long uint64_t;\r
+    #else    \r
+        #include <stdint.h>\r
+    #endif\r
 #endif // BAMTOOLS_TYPES\r
 \r
 namespace BamTools {\r
@@ -66,78 +78,82 @@ const int DEFAULT_BLOCK_SIZE  = 65536;
 struct BgzfData {\r
 \r
     // data members\r
-    unsigned int UncompressedBlockSize;\r
-    unsigned int CompressedBlockSize;\r
-    unsigned int BlockLength;\r
-    unsigned int BlockOffset;\r
-    uint64_t BlockAddress;\r
-    bool     IsOpen;\r
-    bool     IsWriteOnly;\r
-    FILE*    Stream;\r
-    char*    UncompressedBlock;\r
-    char*    CompressedBlock;\r
+    public:\r
+        unsigned int UncompressedBlockSize;\r
+        unsigned int CompressedBlockSize;\r
+        unsigned int BlockLength;\r
+        unsigned int BlockOffset;\r
+        uint64_t BlockAddress;\r
+        bool     IsOpen;\r
+        bool     IsWriteOnly;\r
+        bool     IsWriteUncompressed;\r
+        FILE*    Stream;\r
+        char*    UncompressedBlock;\r
+        char*    CompressedBlock;\r
 \r
     // constructor & destructor\r
-    BgzfData(void);\r
-    ~BgzfData(void);\r
-\r
-    // closes BGZF file\r
-    void Close(void);\r
-    // compresses the current block\r
-    int DeflateBlock(void);\r
-    // flushes the data in the BGZF block\r
-    void FlushBlock(void);\r
-    // de-compresses the current block\r
-    int InflateBlock(const int& blockLength);\r
-    // opens the BGZF file for reading (mode is either "rb" for reading, or "wb" for writing\r
-    void Open(const std::string& filename, const char* mode);\r
-    // reads BGZF data into a byte buffer\r
-    int Read(char* data, const unsigned int dataLength);\r
-    // reads BGZF block\r
-    int ReadBlock(void);\r
-    // seek to position in BAM file\r
-    bool Seek(int64_t position);\r
-    // get file position in BAM file\r
-    int64_t Tell(void);\r
-    // writes the supplied data into the BGZF buffer\r
-    unsigned int Write(const char* data, const unsigned int dataLen);\r
-\r
-    // checks BGZF block header\r
-    static inline bool CheckBlockHeader(char* header);\r
-    // packs an unsigned integer into the specified buffer\r
-    static inline void PackUnsignedInt(char* buffer, unsigned int value);\r
-    // packs an unsigned short into the specified buffer\r
-    static inline void PackUnsignedShort(char* buffer, unsigned short value);\r
-    \r
-    // unpacks a buffer into a signed int\r
-    static inline signed int UnpackSignedInt(char* buffer);\r
-    // unpacks a buffer into an unsigned int\r
-    static inline unsigned int UnpackUnsignedInt(char* buffer);\r
-    // unpacks a buffer into a signed short\r
-    static inline signed short UnpackSignedShort(char* buffer);\r
-    // unpacks a buffer into an unsigned short\r
-    static inline unsigned short UnpackUnsignedShort(char* buffer);\r
-    // unpacks a buffer into a double\r
-    static inline double UnpackDouble(char* buffer);\r
-    // unpacks a buffer into a float\r
-    static inline float UnpackFloat(char* buffer); \r
+    public:\r
+        BgzfData(void);\r
+        ~BgzfData(void);\r
+\r
+    // main interface methods\r
+    public:       \r
+        // closes BGZF file\r
+        void Close(void);\r
+        // opens the BGZF file (mode is either "rb" for reading, or "wb" for writing)\r
+        bool Open(const std::string& filename, const char* mode, bool isWriteUncompressed = false);\r
+        // reads BGZF data into a byte buffer\r
+        int Read(char* data, const unsigned int dataLength);\r
+        // seek to position in BGZF file\r
+        bool Seek(int64_t position);\r
+        // get file position in BGZF file\r
+        int64_t Tell(void);\r
+        // writes the supplied data into the BGZF buffer\r
+        unsigned int Write(const char* data, const unsigned int dataLen);\r
+\r
+    // internal methods\r
+    private:\r
+        // compresses the current block\r
+        int DeflateBlock(void);\r
+        // flushes the data in the BGZF block\r
+        void FlushBlock(void);\r
+        // de-compresses the current block\r
+        int InflateBlock(const int& blockLength);\r
+        // reads a BGZF block\r
+        bool ReadBlock(void);\r
     \r
-    // unpacks a buffer into a signed int\r
-    static inline signed int UnpackSignedInt(const char* buffer);\r
-    // unpacks a buffer into an unsigned int\r
-    static inline unsigned int UnpackUnsignedInt(const char* buffer);\r
-    // unpacks a buffer into a signed short\r
-    static inline signed short UnpackSignedShort(const char* buffer);\r
-    // unpacks a buffer into an unsigned short\r
-    static inline unsigned short UnpackUnsignedShort(const char* buffer);\r
-    // unpacks a buffer into a double\r
-    static inline double UnpackDouble(const char* buffer);\r
-    // unpacks a buffer into a float\r
-    static inline float UnpackFloat(const char* buffer); \r
+    // static 'utility' methods\r
+    public:\r
+        // checks BGZF block header\r
+        static inline bool CheckBlockHeader(char* header);\r
+        // packs an unsigned integer into the specified buffer\r
+        static inline void PackUnsignedInt(char* buffer, unsigned int value);\r
+        // packs an unsigned short into the specified buffer\r
+        static inline void PackUnsignedShort(char* buffer, unsigned short value);\r
+        // unpacks a buffer into a double\r
+        static inline double UnpackDouble(char* buffer);\r
+        static inline double UnpackDouble(const char* buffer);\r
+        // unpacks a buffer into a float\r
+        static inline float UnpackFloat(char* buffer);\r
+        static inline float UnpackFloat(const char* buffer);\r
+        // unpacks a buffer into a signed int\r
+        static inline signed int UnpackSignedInt(char* buffer);\r
+        static inline signed int UnpackSignedInt(const char* buffer);\r
+        // unpacks a buffer into a signed short\r
+        static inline signed short UnpackSignedShort(char* buffer);\r
+        static inline signed short UnpackSignedShort(const char* buffer);\r
+        // unpacks a buffer into an unsigned int\r
+        static inline unsigned int UnpackUnsignedInt(char* buffer);\r
+        static inline unsigned int UnpackUnsignedInt(const char* buffer);\r
+        // unpacks a buffer into an unsigned short\r
+        static inline unsigned short UnpackUnsignedShort(char* buffer);\r
+        static inline unsigned short UnpackUnsignedShort(const char* buffer);\r
 };\r
 \r
 // -------------------------------------------------------------\r
+// static 'utility' method implementations\r
 \r
+// checks BGZF block header\r
 inline\r
 bool BgzfData::CheckBlockHeader(char* header) {\r
     return (header[0] == GZIP_ID1 &&\r
@@ -166,70 +182,64 @@ void BgzfData::PackUnsignedShort(char* buffer, unsigned short value) {
     buffer[1] = (char)(value >> 8);\r
 }\r
 \r
-// 'unpacks' a buffer into a signed int\r
+// 'unpacks' a buffer into a double (includes both non-const & const char* flavors)\r
 inline\r
-signed int BgzfData::UnpackSignedInt(char* buffer) {\r
-    union { signed int value; unsigned char valueBuffer[sizeof(signed int)]; } un;\r
+double BgzfData::UnpackDouble(char* buffer) {\r
+    union { double value; unsigned char valueBuffer[sizeof(double)]; } un;\r
     un.value = 0;\r
     un.valueBuffer[0] = buffer[0];\r
     un.valueBuffer[1] = buffer[1];\r
     un.valueBuffer[2] = buffer[2];\r
     un.valueBuffer[3] = buffer[3];\r
+    un.valueBuffer[4] = buffer[4];\r
+    un.valueBuffer[5] = buffer[5];\r
+    un.valueBuffer[6] = buffer[6];\r
+    un.valueBuffer[7] = buffer[7];\r
     return un.value;\r
 }\r
 \r
-// 'unpacks' a buffer into an unsigned int\r
 inline\r
-unsigned int BgzfData::UnpackUnsignedInt(char* buffer) {\r
-    union { unsigned int value; unsigned char valueBuffer[sizeof(unsigned int)]; } un;\r
+double BgzfData::UnpackDouble(const char* buffer) {\r
+    union { double value; unsigned char valueBuffer[sizeof(double)]; } un;\r
     un.value = 0;\r
     un.valueBuffer[0] = buffer[0];\r
     un.valueBuffer[1] = buffer[1];\r
     un.valueBuffer[2] = buffer[2];\r
     un.valueBuffer[3] = buffer[3];\r
+    un.valueBuffer[4] = buffer[4];\r
+    un.valueBuffer[5] = buffer[5];\r
+    un.valueBuffer[6] = buffer[6];\r
+    un.valueBuffer[7] = buffer[7];\r
     return un.value;\r
 }\r
 \r
-// 'unpacks' a buffer into a signed short\r
-inline\r
-signed short BgzfData::UnpackSignedShort(char* buffer) {\r
-    union { signed short value; unsigned char valueBuffer[sizeof(signed short)]; } un;\r
-    un.value = 0;\r
-    un.valueBuffer[0] = buffer[0];\r
-    un.valueBuffer[1] = buffer[1];\r
-    return un.value;\r
-}\r
-\r
-// 'unpacks' a buffer into an unsigned short\r
+// 'unpacks' a buffer into a float (includes both non-const & const char* flavors)\r
 inline\r
-unsigned short BgzfData::UnpackUnsignedShort(char* buffer) {\r
-    union { unsigned short value; unsigned char valueBuffer[sizeof(unsigned short)]; } un;\r
+float BgzfData::UnpackFloat(char* buffer) {\r
+    union { float value; unsigned char valueBuffer[sizeof(float)]; } un;\r
     un.value = 0;\r
     un.valueBuffer[0] = buffer[0];\r
     un.valueBuffer[1] = buffer[1];\r
+    un.valueBuffer[2] = buffer[2];\r
+    un.valueBuffer[3] = buffer[3];\r
     return un.value;\r
 }\r
 \r
-// 'unpacks' a buffer into a double\r
 inline\r
-double BgzfData::UnpackDouble(char* buffer) {\r
-    union { double value; unsigned char valueBuffer[sizeof(double)]; } un;\r
+float BgzfData::UnpackFloat(const char* buffer) {\r
+    union { float value; unsigned char valueBuffer[sizeof(float)]; } un;\r
     un.value = 0;\r
     un.valueBuffer[0] = buffer[0];\r
     un.valueBuffer[1] = buffer[1];\r
     un.valueBuffer[2] = buffer[2];\r
     un.valueBuffer[3] = buffer[3];\r
-    un.valueBuffer[4] = buffer[4];\r
-    un.valueBuffer[5] = buffer[5];\r
-    un.valueBuffer[6] = buffer[6];\r
-    un.valueBuffer[7] = buffer[7];\r
     return un.value;\r
 }\r
 \r
-// 'unpacks' a buffer into a float\r
+// 'unpacks' a buffer into a signed int (includes both non-const & const char* flavors)\r
 inline\r
-float BgzfData::UnpackFloat(char* buffer) {\r
-    union { float value; unsigned char valueBuffer[sizeof(float)]; } un;\r
+signed int BgzfData::UnpackSignedInt(char* buffer) {\r
+    union { signed int value; unsigned char valueBuffer[sizeof(signed int)]; } un;\r
     un.value = 0;\r
     un.valueBuffer[0] = buffer[0];\r
     un.valueBuffer[1] = buffer[1];\r
@@ -238,9 +248,6 @@ float BgzfData::UnpackFloat(char* buffer) {
     return un.value;\r
 }\r
 \r
-// ---------\r
-\r
-// 'unpacks' a buffer into a signed int\r
 inline\r
 signed int BgzfData::UnpackSignedInt(const char* buffer) {\r
     union { signed int value; unsigned char valueBuffer[sizeof(signed int)]; } un;\r
@@ -252,19 +259,16 @@ signed int BgzfData::UnpackSignedInt(const char* buffer) {
     return un.value;\r
 }\r
 \r
-// 'unpacks' a buffer into an unsigned int\r
+// 'unpacks' a buffer into a signed short (includes both non-const & const char* flavors)\r
 inline\r
-unsigned int BgzfData::UnpackUnsignedInt(const char* buffer) {\r
-    union { unsigned int value; unsigned char valueBuffer[sizeof(unsigned int)]; } un;\r
+signed short BgzfData::UnpackSignedShort(char* buffer) {\r
+    union { signed short value; unsigned char valueBuffer[sizeof(signed short)]; } un;\r
     un.value = 0;\r
     un.valueBuffer[0] = buffer[0];\r
     un.valueBuffer[1] = buffer[1];\r
-    un.valueBuffer[2] = buffer[2];\r
-    un.valueBuffer[3] = buffer[3];\r
     return un.value;\r
 }\r
 \r
-// 'unpacks' a buffer into a signed short\r
 inline\r
 signed short BgzfData::UnpackSignedShort(const char* buffer) {\r
     union { signed short value; unsigned char valueBuffer[sizeof(signed short)]; } un;\r
@@ -274,41 +278,45 @@ signed short BgzfData::UnpackSignedShort(const char* buffer) {
     return un.value;\r
 }\r
 \r
-// 'unpacks' a buffer into an unsigned short\r
+// 'unpacks' a buffer into an unsigned int (includes both non-const & const char* flavors)\r
 inline\r
-unsigned short BgzfData::UnpackUnsignedShort(const char* buffer) {\r
-    union { unsigned short value; unsigned char valueBuffer[sizeof(unsigned short)]; } un;\r
+unsigned int BgzfData::UnpackUnsignedInt(char* buffer) {\r
+    union { unsigned int value; unsigned char valueBuffer[sizeof(unsigned int)]; } un;\r
     un.value = 0;\r
     un.valueBuffer[0] = buffer[0];\r
     un.valueBuffer[1] = buffer[1];\r
+    un.valueBuffer[2] = buffer[2];\r
+    un.valueBuffer[3] = buffer[3];\r
     return un.value;\r
 }\r
 \r
-// 'unpacks' a buffer into a double\r
 inline\r
-double BgzfData::UnpackDouble(const char* buffer) {\r
-    union { double value; unsigned char valueBuffer[sizeof(double)]; } un;\r
+unsigned int BgzfData::UnpackUnsignedInt(const char* buffer) {\r
+    union { unsigned int value; unsigned char valueBuffer[sizeof(unsigned int)]; } un;\r
     un.value = 0;\r
     un.valueBuffer[0] = buffer[0];\r
     un.valueBuffer[1] = buffer[1];\r
     un.valueBuffer[2] = buffer[2];\r
     un.valueBuffer[3] = buffer[3];\r
-    un.valueBuffer[4] = buffer[4];\r
-    un.valueBuffer[5] = buffer[5];\r
-    un.valueBuffer[6] = buffer[6];\r
-    un.valueBuffer[7] = buffer[7];\r
     return un.value;\r
 }\r
 \r
-// 'unpacks' a buffer into a float\r
+// 'unpacks' a buffer into an unsigned short (includes both non-const & const char* flavors)\r
 inline\r
-float BgzfData::UnpackFloat(const char* buffer) {\r
-    union { float value; unsigned char valueBuffer[sizeof(float)]; } un;\r
+unsigned short BgzfData::UnpackUnsignedShort(char* buffer) {\r
+    union { unsigned short value; unsigned char valueBuffer[sizeof(unsigned short)]; } un;\r
+    un.value = 0;\r
+    un.valueBuffer[0] = buffer[0];\r
+    un.valueBuffer[1] = buffer[1];\r
+    return un.value;\r
+}\r
+\r
+inline\r
+unsigned short BgzfData::UnpackUnsignedShort(const char* buffer) {\r
+    union { unsigned short value; unsigned char valueBuffer[sizeof(unsigned short)]; } un;\r
     un.value = 0;\r
     un.valueBuffer[0] = buffer[0];\r
     un.valueBuffer[1] = buffer[1];\r
-    un.valueBuffer[2] = buffer[2];\r
-    un.valueBuffer[3] = buffer[3];\r
     return un.value;\r
 }\r
 \r