// 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
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
\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
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
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
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
\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
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
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
}\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
\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
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