]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/api/BGZF.cpp
Attempt to fix SamHeaderVersion compile bug
[bamtools.git] / src / api / BGZF.cpp
index 92afb96f83e058960aa4ebf0b2789de947944c0b..701fa7f0fb033aafac618a979aac7188580c470c 100644 (file)
@@ -3,7 +3,7 @@
 // Marth Lab, Department of Biology, Boston College\r
 // All rights reserved.\r
 // ---------------------------------------------------------------------------\r
-// Last modified: 16 August 2010 (DB)\r
+// Last modified: 19 November 2010 (DB)\r
 // ---------------------------------------------------------------------------\r
 // BGZF routines were adapted from the bgzf.c code developed at the Broad\r
 // Institute.\r
 // Provides the basic functionality for reading & writing BGZF files\r
 // ***************************************************************************\r
 \r
-#include <algorithm>\r
-#include "BGZF.h"\r
+#include <api/BGZF.h>\r
 using namespace BamTools;\r
-using std::string;\r
-using std::min;\r
+\r
+#include <algorithm>\r
+using namespace std;\r
 \r
 BgzfData::BgzfData(void)\r
     : UncompressedBlockSize(DEFAULT_BLOCK_SIZE)\r
@@ -34,7 +34,7 @@ BgzfData::BgzfData(void)
         CompressedBlock   = new char[CompressedBlockSize];\r
         UncompressedBlock = new char[UncompressedBlockSize];\r
     } catch( std::bad_alloc& ba ) {\r
-        printf("BGZF ERROR: unable to allocate memory for our BGZF object.\n");\r
+        fprintf(stderr, "BGZF ERROR: unable to allocate memory for our BGZF object.\n");\r
         exit(1);\r
     }\r
 }\r
@@ -103,7 +103,7 @@ int BgzfData::DeflateBlock(void) {
 \r
         // initialize the zlib compression algorithm\r
         if ( deflateInit2(&zs, compressionLevel, Z_DEFLATED, GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK ) {\r
-            printf("BGZF ERROR: zlib deflate initialization failed.\n");\r
+            fprintf(stderr, "BGZF ERROR: zlib deflate initialization failed.\n");\r
             exit(1);\r
         }\r
 \r
@@ -117,26 +117,26 @@ int BgzfData::DeflateBlock(void) {
             if ( status == Z_OK ) {\r
                 inputLength -= 1024;\r
                 if( inputLength < 0 ) {\r
-                    printf("BGZF ERROR: input reduction failed.\n");\r
+                    fprintf(stderr, "BGZF ERROR: input reduction failed.\n");\r
                     exit(1);\r
                 }\r
                 continue;\r
             }\r
 \r
-            printf("BGZF ERROR: zlib::deflateEnd() failed.\n");\r
+            fprintf(stderr, "BGZF ERROR: zlib::deflateEnd() failed.\n");\r
             exit(1);\r
         }\r
 \r
         // finalize the compression routine\r
         if ( deflateEnd(&zs) != Z_OK ) {\r
-            printf("BGZF ERROR: zlib::deflateEnd() failed.\n");\r
+            fprintf(stderr, "BGZF ERROR: zlib::deflateEnd() failed.\n");\r
             exit(1);\r
         }\r
 \r
         compressedLength = zs.total_out;\r
         compressedLength += BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH;\r
         if ( compressedLength > MAX_BLOCK_SIZE ) {\r
-            printf("BGZF ERROR: deflate overflow.\n");\r
+            fprintf(stderr, "BGZF ERROR: deflate overflow.\n");\r
             exit(1);\r
         }\r
 \r
@@ -156,7 +156,7 @@ int BgzfData::DeflateBlock(void) {
     int remaining = BlockOffset - inputLength;\r
     if ( remaining > 0 ) {\r
         if ( remaining > inputLength ) {\r
-            printf("BGZF ERROR: after deflate, remainder too large.\n");\r
+            fprintf(stderr, "BGZF ERROR: after deflate, remainder too large.\n");\r
             exit(1);\r
         }\r
         memcpy(UncompressedBlock, UncompressedBlock + inputLength, remaining);\r
@@ -179,7 +179,7 @@ void BgzfData::FlushBlock(void) {
         int numBytesWritten = fwrite(CompressedBlock, 1, blockLength, Stream);\r
 \r
         if ( numBytesWritten != blockLength ) {\r
-          printf("BGZF ERROR: expected to write %u bytes during flushing, but wrote %u bytes.\n", blockLength, numBytesWritten);\r
+          fprintf(stderr, "BGZF ERROR: expected to write %u bytes during flushing, but wrote %u bytes.\n", blockLength, numBytesWritten);\r
           exit(1);\r
         }\r
               \r
@@ -201,20 +201,20 @@ int BgzfData::InflateBlock(const int& blockLength) {
 \r
     int status = inflateInit2(&zs, GZIP_WINDOW_BITS);\r
     if ( status != Z_OK ) {\r
-        printf("BGZF ERROR: could not decompress block - zlib::inflateInit() failed\n");\r
+        fprintf(stderr, "BGZF ERROR: could not decompress block - zlib::inflateInit() failed\n");\r
         return -1;\r
     }\r
 \r
     status = inflate(&zs, Z_FINISH);\r
     if ( status != Z_STREAM_END ) {\r
         inflateEnd(&zs);\r
-        printf("BGZF ERROR: could not decompress block - zlib::inflate() failed\n");\r
+        fprintf(stderr, "BGZF ERROR: could not decompress block - zlib::inflate() failed\n");\r
         return -1;\r
     }\r
 \r
     status = inflateEnd(&zs);\r
     if ( status != Z_OK ) {\r
-        printf("BGZF ERROR: could not decompress block - zlib::inflateEnd() failed\n");\r
+        fprintf(stderr, "BGZF ERROR: could not decompress block - zlib::inflateEnd() failed\n");\r
         return -1;\r
     }\r
 \r
@@ -230,7 +230,7 @@ bool BgzfData::Open(const string& filename, const char* mode, bool isWriteUncomp
     else if ( strcmp(mode, "wb") == 0) \r
         IsWriteOnly = true;\r
     else {\r
-        printf("BGZF ERROR: unknown file mode: %s\n", mode);\r
+        fprintf(stderr, "BGZF ERROR: unknown file mode: %s\n", mode);\r
         return false; \r
     }\r
 \r
@@ -251,7 +251,7 @@ bool BgzfData::Open(const string& filename, const char* mode, bool isWriteUncomp
         Stream = freopen(NULL, mode, stdout);\r
 \r
     if ( !Stream ) {\r
-        printf("BGZF ERROR: unable to open file %s\n", filename.c_str() );\r
+        fprintf(stderr, "BGZF ERROR: unable to open file %s\n", filename.c_str() );\r
         return false;\r
     }\r
     \r
@@ -308,12 +308,12 @@ bool BgzfData::ReadBlock(void) {
     }\r
 \r
     if ( count != sizeof(header) ) {\r
-        printf("BGZF ERROR: read block failed - could not read block header\n");\r
+        fprintf(stderr, "BGZF ERROR: read block failed - could not read block header\n");\r
         return false;\r
     }\r
 \r
     if ( !BgzfData::CheckBlockHeader(header) ) {\r
-        printf("BGZF ERROR: read block failed - invalid block header\n");\r
+        fprintf(stderr, "BGZF ERROR: read block failed - invalid block header\n");\r
         return false;\r
     }\r
 \r
@@ -324,13 +324,13 @@ bool BgzfData::ReadBlock(void) {
 \r
     count = fread(&compressedBlock[BLOCK_HEADER_LENGTH], 1, remaining, Stream);\r
     if ( count != remaining ) {\r
-        printf("BGZF ERROR: read block failed - could not read data from block\n");\r
+        fprintf(stderr, "BGZF ERROR: read block failed - could not read data from block\n");\r
         return false;\r
     }\r
 \r
     count = InflateBlock(blockLength);\r
     if ( count < 0 ) { \r
-      printf("BGZF ERROR: read block failed - could not decompress block data\n");\r
+      fprintf(stderr, "BGZF ERROR: read block failed - could not decompress block data\n");\r
       return false;\r
     }\r
 \r
@@ -351,7 +351,7 @@ bool BgzfData::Seek(int64_t position) {
     int64_t blockAddress = (position >> 16) & 0xFFFFFFFFFFFFLL;\r
 \r
     if ( fseek64(Stream, blockAddress, SEEK_SET) != 0 ) {\r
-        printf("BGZF ERROR: unable to seek in file\n");\r
+        fprintf(stderr, "BGZF ERROR: unable to seek in file\n");\r
         return false;\r
     }\r
 \r