Skip to content

Commit

Permalink
Add hts_flush() to flush buffered htsFile output data
Browse files Browse the repository at this point in the history
Implemented by calling through to the various underlying flush routines.
Also add sam_flush() and bcf_flush() wrappers.
  • Loading branch information
jmarshall committed Sep 1, 2021
1 parent 31bf087 commit 238fe32
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Noteworthy changes in release a.b
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* New API function hts_flush()/sam_flush()/bcf_flush() for flushing output
htsFile/samFile/vcfFile streams. (PR #1326, thanks to John Marshall)


Noteworthy changes in release 1.13 (7th July 2021)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
32 changes: 32 additions & 0 deletions hts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,38 @@ int hts_close(htsFile *fp)
return ret;
}

int hts_flush(htsFile *fp)
{
if (fp == NULL) return 0;

switch (fp->format.format) {
case binary_format:
case bam:
case bcf:
return bgzf_flush(fp->fp.bgzf);

case cram:
return cram_flush(fp->fp.cram);

case empty_format:
case text_format:
case bed:
case fasta_format:
case fastq_format:
case sam:
case vcf:
if (fp->format.compression != no_compression)
return bgzf_flush(fp->fp.bgzf);
else
return hflush(fp->fp.hfile);

default:
break;
}

return 0;
}

const htsFormat *hts_get_format(htsFile *fp)
{
return fp? &fp->format : NULL;
Expand Down
9 changes: 9 additions & 0 deletions htslib/hts.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,15 @@ htsFile *hts_open_format(const char *fn, const char *mode, const htsFormat *fmt)
HTSLIB_EXPORT
htsFile *hts_hopen(struct hFILE *fp, const char *fn, const char *mode);

/*!
@abstract For output streams, flush any buffered data
@param fp The file handle to be flushed
@return 0 for success, or negative if an error occurred.
@since 1.14
*/
HTSLIB_EXPORT
int hts_flush(htsFile *fp);

/*!
@abstract Close a file handle, flushing buffered data for output streams
@param fp The file handle to be closed
Expand Down
1 change: 1 addition & 0 deletions htslib/sam.h
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,7 @@ const char *sam_parse_region(sam_hdr_t *h, const char *s, int *tid,

#define sam_open(fn, mode) (hts_open((fn), (mode)))
#define sam_open_format(fn, mode, fmt) (hts_open_format((fn), (mode), (fmt)))
#define sam_flush(fp) hts_flush((fp))
#define sam_close(fp) hts_close(fp)

HTSLIB_EXPORT
Expand Down
1 change: 1 addition & 0 deletions htslib/vcf.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ typedef struct bcf1_t {
typedef htsFile vcfFile;
#define bcf_open(fn, mode) hts_open((fn), (mode))
#define vcf_open(fn, mode) hts_open((fn), (mode))
#define bcf_flush(fp) hts_flush((fp))
#define bcf_close(fp) hts_close(fp)
#define vcf_close(fp) hts_close(fp)

Expand Down

0 comments on commit 238fe32

Please sign in to comment.