]> git.donarmstrong.com Git - samtools.git/blobdiff - bgzf.c
* samtools-0.1.5-17 (r422)
[samtools.git] / bgzf.c
diff --git a/bgzf.c b/bgzf.c
index 71d8c81460044cb8b88c655d41ba918874641f61..2cd1c5e514a94c5569f5819c7f6b23af8985d444 100644 (file)
--- a/bgzf.c
+++ b/bgzf.c
@@ -571,6 +571,14 @@ bgzf_close(BGZF* fp)
         if (flush_block(fp) != 0) {
             return -1;
         }
+               { // add an empty block
+                       int count, block_length = deflate_block(fp, 0);
+#ifdef _USE_KNETFILE
+                       count = fwrite(fp->compressed_block, 1, block_length, fp->x.fpw);
+#else
+                       count = fwrite(fp->compressed_block, 1, block_length, fp->file);
+#endif
+               }
 #ifdef _USE_KNETFILE
         if (fflush(fp->x.fpw) != 0) {
 #else
@@ -610,6 +618,25 @@ void bgzf_set_cache_size(BGZF *fp, int cache_size)
        if (fp) fp->cache_size = cache_size;
 }
 
+int bgzf_check_EOF(BGZF *fp)
+{
+       static uint8_t magic[28] = "\037\213\010\4\0\0\0\0\0\377\6\0\102\103\2\0\033\0\3\0\0\0\0\0\0\0\0\0";
+       uint8_t buf[28];
+       off_t offset;
+#ifdef _USE_KNETFILE
+       offset = knet_tell(fp->x.fpr);
+       if (knet_seek(fp->x.fpr, -28, SEEK_END) != 0) return -1;
+       knet_read(fp->x.fpr, buf, 28);
+       knet_seek(fp->x.fpr, offset, SEEK_SET);
+#else
+       offset = ftello(fp->file);
+       if (fseeko(fp->file, -28, SEEK_END) != 0) return -1;
+       fread(buf, 1, 28, fp->file);
+       fseeko(fp->file, offset, SEEK_SET);
+#endif
+       return (memcmp(magic, buf, 28) == 0)? 1 : 0;
+}
+
 int64_t
 bgzf_seek(BGZF* fp, int64_t pos, int where)
 {
@@ -636,4 +663,3 @@ bgzf_seek(BGZF* fp, int64_t pos, int where)
     fp->block_offset = block_offset;
     return 0;
 }
-