From 1ee9b6b3d98a4bfd5a60c583bc7847c545a60e32 Mon Sep 17 00:00:00 2001
From: Erik Garrison <erik.garrison@bc.edu>
Date: Fri, 20 Aug 2010 12:22:34 -0400
Subject: [PATCH] change printf's to fprint(stderr,

This pushes errors to the expected output, stderr, instead of mixing
them into the standard output stream.
---
 src/api/BGZF.cpp      | 36 ++++++++++++++++++------------------
 src/api/BamAux.h      | 10 +++++-----
 src/api/BamIndex.cpp  | 18 +++++++++---------
 src/api/BamReader.cpp | 12 ++++++------
 src/api/BamWriter.cpp |  6 +++---
 5 files changed, 41 insertions(+), 41 deletions(-)

diff --git a/src/api/BGZF.cpp b/src/api/BGZF.cpp
index 92afb96..2b74343 100644
--- a/src/api/BGZF.cpp
+++ b/src/api/BGZF.cpp
@@ -34,7 +34,7 @@ BgzfData::BgzfData(void)
         CompressedBlock   = new char[CompressedBlockSize];
         UncompressedBlock = new char[UncompressedBlockSize];
     } catch( std::bad_alloc& ba ) {
-        printf("BGZF ERROR: unable to allocate memory for our BGZF object.\n");
+        fprintf(stderr, "BGZF ERROR: unable to allocate memory for our BGZF object.\n");
         exit(1);
     }
 }
@@ -103,7 +103,7 @@ int BgzfData::DeflateBlock(void) {
 
         // initialize the zlib compression algorithm
         if ( deflateInit2(&zs, compressionLevel, Z_DEFLATED, GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK ) {
-            printf("BGZF ERROR: zlib deflate initialization failed.\n");
+            fprintf(stderr, "BGZF ERROR: zlib deflate initialization failed.\n");
             exit(1);
         }
 
@@ -117,26 +117,26 @@ int BgzfData::DeflateBlock(void) {
             if ( status == Z_OK ) {
                 inputLength -= 1024;
                 if( inputLength < 0 ) {
-                    printf("BGZF ERROR: input reduction failed.\n");
+                    fprintf(stderr, "BGZF ERROR: input reduction failed.\n");
                     exit(1);
                 }
                 continue;
             }
 
-            printf("BGZF ERROR: zlib::deflateEnd() failed.\n");
+            fprintf(stderr, "BGZF ERROR: zlib::deflateEnd() failed.\n");
             exit(1);
         }
 
         // finalize the compression routine
         if ( deflateEnd(&zs) != Z_OK ) {
-            printf("BGZF ERROR: zlib::deflateEnd() failed.\n");
+            fprintf(stderr, "BGZF ERROR: zlib::deflateEnd() failed.\n");
             exit(1);
         }
 
         compressedLength = zs.total_out;
         compressedLength += BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH;
         if ( compressedLength > MAX_BLOCK_SIZE ) {
-            printf("BGZF ERROR: deflate overflow.\n");
+            fprintf(stderr, "BGZF ERROR: deflate overflow.\n");
             exit(1);
         }
 
@@ -156,7 +156,7 @@ int BgzfData::DeflateBlock(void) {
     int remaining = BlockOffset - inputLength;
     if ( remaining > 0 ) {
         if ( remaining > inputLength ) {
-            printf("BGZF ERROR: after deflate, remainder too large.\n");
+            fprintf(stderr, "BGZF ERROR: after deflate, remainder too large.\n");
             exit(1);
         }
         memcpy(UncompressedBlock, UncompressedBlock + inputLength, remaining);
@@ -179,7 +179,7 @@ void BgzfData::FlushBlock(void) {
         int numBytesWritten = fwrite(CompressedBlock, 1, blockLength, Stream);
 
         if ( numBytesWritten != blockLength ) {
-          printf("BGZF ERROR: expected to write %u bytes during flushing, but wrote %u bytes.\n", blockLength, numBytesWritten);
+          fprintf(stderr, "BGZF ERROR: expected to write %u bytes during flushing, but wrote %u bytes.\n", blockLength, numBytesWritten);
           exit(1);
         }
               
@@ -201,20 +201,20 @@ int BgzfData::InflateBlock(const int& blockLength) {
 
     int status = inflateInit2(&zs, GZIP_WINDOW_BITS);
     if ( status != Z_OK ) {
-        printf("BGZF ERROR: could not decompress block - zlib::inflateInit() failed\n");
+        fprintf(stderr, "BGZF ERROR: could not decompress block - zlib::inflateInit() failed\n");
         return -1;
     }
 
     status = inflate(&zs, Z_FINISH);
     if ( status != Z_STREAM_END ) {
         inflateEnd(&zs);
-        printf("BGZF ERROR: could not decompress block - zlib::inflate() failed\n");
+        fprintf(stderr, "BGZF ERROR: could not decompress block - zlib::inflate() failed\n");
         return -1;
     }
 
     status = inflateEnd(&zs);
     if ( status != Z_OK ) {
-        printf("BGZF ERROR: could not decompress block - zlib::inflateEnd() failed\n");
+        fprintf(stderr, "BGZF ERROR: could not decompress block - zlib::inflateEnd() failed\n");
         return -1;
     }
 
@@ -230,7 +230,7 @@ bool BgzfData::Open(const string& filename, const char* mode, bool isWriteUncomp
     else if ( strcmp(mode, "wb") == 0) 
         IsWriteOnly = true;
     else {
-        printf("BGZF ERROR: unknown file mode: %s\n", mode);
+        fprintf(stderr, "BGZF ERROR: unknown file mode: %s\n", mode);
         return false; 
     }
 
@@ -251,7 +251,7 @@ bool BgzfData::Open(const string& filename, const char* mode, bool isWriteUncomp
         Stream = freopen(NULL, mode, stdout);
 
     if ( !Stream ) {
-        printf("BGZF ERROR: unable to open file %s\n", filename.c_str() );
+        fprintf(stderr, "BGZF ERROR: unable to open file %s\n", filename.c_str() );
         return false;
     }
     
@@ -308,12 +308,12 @@ bool BgzfData::ReadBlock(void) {
     }
 
     if ( count != sizeof(header) ) {
-        printf("BGZF ERROR: read block failed - could not read block header\n");
+        fprintf(stderr, "BGZF ERROR: read block failed - could not read block header\n");
         return false;
     }
 
     if ( !BgzfData::CheckBlockHeader(header) ) {
-        printf("BGZF ERROR: read block failed - invalid block header\n");
+        fprintf(stderr, "BGZF ERROR: read block failed - invalid block header\n");
         return false;
     }
 
@@ -324,13 +324,13 @@ bool BgzfData::ReadBlock(void) {
 
     count = fread(&compressedBlock[BLOCK_HEADER_LENGTH], 1, remaining, Stream);
     if ( count != remaining ) {
-        printf("BGZF ERROR: read block failed - could not read data from block\n");
+        fprintf(stderr, "BGZF ERROR: read block failed - could not read data from block\n");
         return false;
     }
 
     count = InflateBlock(blockLength);
     if ( count < 0 ) { 
-      printf("BGZF ERROR: read block failed - could not decompress block data\n");
+      fprintf(stderr, "BGZF ERROR: read block failed - could not decompress block data\n");
       return false;
     }
 
@@ -351,7 +351,7 @@ bool BgzfData::Seek(int64_t position) {
     int64_t blockAddress = (position >> 16) & 0xFFFFFFFFFFFFLL;
 
     if ( fseek64(Stream, blockAddress, SEEK_SET) != 0 ) {
-        printf("BGZF ERROR: unable to seek in file\n");
+        fprintf(stderr, "BGZF ERROR: unable to seek in file\n");
         return false;
     }
 
diff --git a/src/api/BamAux.h b/src/api/BamAux.h
index 73e8838..a39abb0 100644
--- a/src/api/BamAux.h
+++ b/src/api/BamAux.h
@@ -783,12 +783,12 @@ bool BamAlignment::GetTag(const std::string& tag, uint32_t& destination) const {
             case 'f':
             case 'Z':
             case 'H':
-                printf("ERROR: Cannot store tag of type %c in integer destination\n", type);
+                fprintf(stderr, "ERROR: Cannot store tag of type %c in integer destination\n", type);
                 return false;
 
             // unknown tag type
             default:
-                printf("ERROR: Unknown tag storage class encountered: [%c]\n", type);
+                fprintf(stderr, "ERROR: Unknown tag storage class encountered: [%c]\n", type);
                 return false;
         }
           
@@ -851,12 +851,12 @@ bool BamAlignment::GetTag(const std::string& tag, float& destination) const {
             // unsupported type (var-length strings)
             case 'Z':
             case 'H':
-                printf("ERROR: Cannot store tag of type %c in integer destination\n", type);
+                fprintf(stderr, "ERROR: Cannot store tag of type %c in integer destination\n", type);
                 return false;
 
             // unknown tag type
             default:
-                printf("ERROR: Unknown tag storage class encountered: [%c]\n", type);
+                fprintf(stderr, "ERROR: Unknown tag storage class encountered: [%c]\n", type);
                 return false;
         }
           
@@ -978,7 +978,7 @@ bool BamAlignment::SkipToNextTag(const char storageType, char* &pTagData, unsign
 
         default: 
             // error case
-            printf("ERROR: Unknown tag storage class encountered: [%c]\n", storageType);
+            fprintf(stderr, "ERROR: Unknown tag storage class encountered: [%c]\n", storageType);
             return false;
     }
     
diff --git a/src/api/BamIndex.cpp b/src/api/BamIndex.cpp
index d74e751..5f636d1 100644
--- a/src/api/BamIndex.cpp
+++ b/src/api/BamIndex.cpp
@@ -213,8 +213,8 @@ bool BamDefaultIndex::Build(void) {
 
         // if lastCoordinate greater than BAM position - file not sorted properly
         else if ( lastCoordinate > bAlignment.Position ) {
-            printf("BAM file not properly sorted:\n");
-            printf("Alignment %s : %d > %d on reference (id = %d)", bAlignment.Name.c_str(), lastCoordinate, bAlignment.Position, bAlignment.RefID);
+            fprintf(stderr, "BAM file not properly sorted:\n");
+            fprintf(stderr, "Alignment %s : %d > %d on reference (id = %d)", bAlignment.Name.c_str(), lastCoordinate, bAlignment.Position, bAlignment.RefID);
             exit(1);
         }
 
@@ -255,7 +255,7 @@ bool BamDefaultIndex::Build(void) {
 
         // make sure that current file pointer is beyond lastOffset
         if ( m_BGZF->Tell() <= (int64_t)lastOffset ) {
-            printf("Error in BGZF offsets.\n");
+            fprintf(stderr, "Error in BGZF offsets.\n");
             exit(1);
         }
 
@@ -397,7 +397,7 @@ bool BamDefaultIndex::Load(const string& filename)  {
     // open index file, abort on error
     FILE* indexStream = fopen(filename.c_str(), "rb");
     if( !indexStream ) {
-        printf("ERROR: Unable to open the BAM index file %s for reading.\n", filename.c_str());
+        fprintf(stderr, "ERROR: Unable to open the BAM index file %s for reading.\n", filename.c_str());
         return false;
     }
 
@@ -408,7 +408,7 @@ bool BamDefaultIndex::Load(const string& filename)  {
     char magic[4];
     elementsRead = fread(magic, 1, 4, indexStream);
     if ( strncmp(magic, "BAI\1", 4) ) {
-        printf("Problem with index file - invalid format.\n");
+        fprintf(stderr, "Problem with index file - invalid format.\n");
         fclose(indexStream);
         return false;
     }
@@ -578,7 +578,7 @@ bool BamDefaultIndex::Write(const std::string& bamFilename) {
     string indexFilename = bamFilename + ".bai";
     FILE* indexStream = fopen(indexFilename.c_str(), "wb");
     if ( indexStream == 0 ) {
-        printf("ERROR: Could not open file to save index.\n");
+        fprintf(stderr, "ERROR: Could not open file to save index.\n");
         return false;
     }
 
@@ -810,7 +810,7 @@ bool BamToolsIndex::Load(const string& filename) {
     // open index file, abort on error
     FILE* indexStream = fopen(filename.c_str(), "rb");
     if( !indexStream ) {
-        printf("ERROR: Unable to open the BAM index file %s for reading.\n", filename.c_str());
+        fprintf(stderr, "ERROR: Unable to open the BAM index file %s for reading.\n", filename.c_str());
         return false;
     }
 
@@ -821,7 +821,7 @@ bool BamToolsIndex::Load(const string& filename) {
     char magic[4];
     elementsRead = fread(magic, 1, 4, indexStream);
     if ( strncmp(magic, "BTI\1", 4) ) {
-        printf("Problem with index file - invalid format.\n");
+        fprintf(stderr, "Problem with index file - invalid format.\n");
         fclose(indexStream);
         return false;
     }
@@ -876,7 +876,7 @@ bool BamToolsIndex::Write(const std::string& bamFilename) {
     string indexFilename = bamFilename + ".bti";
     FILE* indexStream = fopen(indexFilename.c_str(), "wb");
     if ( indexStream == 0 ) {
-        printf("ERROR: Could not open file to save index.\n");
+        fprintf(stderr, "ERROR: Could not open file to save index.\n");
         return false;
     }
 
diff --git a/src/api/BamReader.cpp b/src/api/BamReader.cpp
index b1b1b5d..bb70f1f 100644
--- a/src/api/BamReader.cpp
+++ b/src/api/BamReader.cpp
@@ -291,7 +291,7 @@ bool BamReader::BamReaderPrivate::BuildCharData(BamAlignment& bAlignment) {
                     break;  // for 'H' - hard clip, do nothing to AlignedBases, move to next op
                     
                 default:
-                    printf("ERROR: Invalid Cigar op type\n"); // shouldn't get here
+                    fprintf(stderr, "ERROR: Invalid Cigar op type\n"); // shouldn't get here
                     exit(1);
             }
         }
@@ -339,7 +339,7 @@ bool BamReader::BamReaderPrivate::BuildCharData(BamAlignment& bAlignment) {
                     break;
                 
                 default : 
-                    printf("ERROR: Invalid tag value type\n"); // shouldn't get here
+                    fprintf(stderr, "ERROR: Invalid tag value type\n"); // shouldn't get here
                     exit(1);
             }
         }
@@ -525,7 +525,7 @@ bool BamReader::BamReaderPrivate::Jump(int refID, int position) {
     // determine possible offsets
     vector<int64_t> offsets;
     if ( !NewIndex->GetOffsets(Region, IsRightBoundSpecified, offsets) ) {
-        printf("ERROR: Could not jump: unable to calculate offset for specified region.\n");
+        fprintf(stderr, "ERROR: Could not jump: unable to calculate offset for specified region.\n");
         return false;
     }
       
@@ -555,12 +555,12 @@ void BamReader::BamReaderPrivate::LoadHeaderData(void) {
     // check to see if proper BAM header
     char buffer[4];
     if (mBGZF.Read(buffer, 4) != 4) {
-        printf("Could not read header type\n");
+        fprintf(stderr, "Could not read header type\n");
         exit(1);
     }
 
     if (strncmp(buffer, "BAM\001", 4)) {
-        printf("wrong header type!\n");
+        fprintf(stderr, "wrong header type!\n");
         exit(1);
     }
 
@@ -602,7 +602,7 @@ bool BamReader::BamReaderPrivate::LoadIndex(void) {
     
     // else unknown
     else {
-        printf("ERROR: Unknown index file extension.\n");
+        fprintf(stderr, "ERROR: Unknown index file extension.\n");
         return false;
     }
     
diff --git a/src/api/BamWriter.cpp b/src/api/BamWriter.cpp
index f83ff1c..2be38bb 100644
--- a/src/api/BamWriter.cpp
+++ b/src/api/BamWriter.cpp
@@ -131,7 +131,7 @@ void BamWriter::BamWriterPrivate::CreatePackedCigar(const vector<CigarOp>& cigar
                   cigarOp = BAM_CPAD;
                   break;
             default:
-                  printf("ERROR: Unknown cigar operation found: %c\n", coIter->Type);
+                  fprintf(stderr, "ERROR: Unknown cigar operation found: %c\n", coIter->Type);
                   exit(1);
         }
 
@@ -182,7 +182,7 @@ void BamWriter::BamWriterPrivate::EncodeQuerySequence(const string& query, strin
                 break;
             
             default:
-                printf("ERROR: Only the following bases are supported in the BAM format: {=, A, C, G, T, N}. Found [%c]\n", *pQuery);
+                fprintf(stderr, "ERROR: Only the following bases are supported in the BAM format: {=, A, C, G, T, N}. Found [%c]\n", *pQuery);
                 exit(1);
         }
 
@@ -417,7 +417,7 @@ void BamWriter::BamWriterPrivate::SaveAlignment(const BamAlignment& al) {
                         break;
                                 
                     default : 
-                        printf("ERROR: Invalid tag value type\n"); // shouldn't get here
+                        fprintf(stderr, "ERROR: Invalid tag value type\n"); // shouldn't get here
                         free(tagData);
                         exit(1); 
                 }
-- 
2.39.5