]> git.donarmstrong.com Git - samtools.git/blob - bgzf.h
* Merge from branches/dev/
[samtools.git] / bgzf.h
1 /*
2  * The Broad Institute
3  * SOFTWARE COPYRIGHT NOTICE AGREEMENT
4  * This software and its documentation are copyright 2008 by the
5  * Broad Institute/Massachusetts Institute of Technology. All rights are reserved.
6  *
7  * This software is supplied without any warranty or guaranteed support whatsoever.
8  * Neither the Broad Institute nor MIT can be responsible for its use, misuse,
9  * or functionality.
10  */
11
12 #ifndef __BGZF_H
13 #define __BGZF_H
14
15 #include <stdint.h>
16 #include <stdio.h>
17 #include "zlib.h"
18 #include <stdbool.h>
19 //#include "zutil.h"
20
21 //typedef int8_t bool;
22
23 typedef struct {
24     int file_descriptor;
25     char open_mode;  // 'r' or 'w'
26     bool owned_file;
27     FILE* file;
28     int uncompressed_block_size;
29     int compressed_block_size;
30     void* uncompressed_block;
31     void* compressed_block;
32     int64_t block_address;
33     int block_length;
34     int block_offset;
35     const char* error;
36 } BGZF;
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 /*
43  * Open an existing file descriptor for reading or writing.
44  * Mode must be either "r" or "w".
45  * A subsequent bgzf_close will not close the file descriptor.
46  * Returns null on error.
47  */
48 BGZF* bgzf_fdopen(int fd, const char* __restrict mode);
49
50 /*
51  * Open the specified file for reading or writing.
52  * Mode must be either "r" or "w".
53  * Returns null on error.
54  */
55 BGZF* bgzf_open(const char* path, const char* __restrict mode);
56
57 /*
58  * Close the BGZ file and free all associated resources.
59  * Does not close the underlying file descriptor if created with bgzf_fdopen.
60  * Returns zero on success, -1 on error.
61  */
62 int bgzf_close(BGZF* fp);
63
64 /*
65  * Read up to length bytes from the file storing into data.
66  * Returns the number of bytes actually read.
67  * Returns zero on end of file.
68  * Returns -1 on error.
69  */
70 int bgzf_read(BGZF* fp, void* data, int length);
71
72 /*
73  * Write length bytes from data to the file.
74  * Returns the number of bytes written.
75  * Returns -1 on error.
76  */
77 int bgzf_write(BGZF* fp, const void* data, int length);
78
79 /*
80  * Return a virtual file pointer to the current location in the file.
81  * No interpetation of the value should be made, other than a subsequent
82  * call to bgzf_seek can be used to position the file at the same point.
83  * Return value is non-negative on success.
84  * Returns -1 on error.
85  */
86 int64_t bgzf_tell(BGZF* fp);
87
88 /*
89  * Set the file to read from the location specified by pos, which must
90  * be a value previously returned by bgzf_tell for this file (but not
91  * necessarily one returned by this file handle).
92  * The where argument must be SEEK_SET.
93  * Seeking on a file opened for write is not supported.
94  * Returns zero on success, -1 on error.
95  */
96 int64_t bgzf_seek(BGZF* fp, int64_t pos, int where);
97
98 #ifdef __cplusplus
99 }
100 #endif
101
102 #endif