]> git.donarmstrong.com Git - samtools.git/blob - bgzf.h
* samtools-0.1.4-7 (r337)
[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 <stdbool.h>
18 #include <zlib.h>
19
20 //typedef int8_t bool;
21
22 typedef struct {
23     int file_descriptor;
24     char open_mode;  // 'r' or 'w'
25     bool owned_file, is_uncompressed;
26     FILE* file;
27     int uncompressed_block_size;
28     int compressed_block_size;
29     void* uncompressed_block;
30     void* compressed_block;
31     int64_t block_address;
32     int block_length;
33     int block_offset;
34     const char* error;
35 } BGZF;
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /*
42  * Open an existing file descriptor for reading or writing.
43  * Mode must be either "r" or "w".
44  * A subsequent bgzf_close will not close the file descriptor.
45  * Returns null on error.
46  */
47 BGZF* bgzf_fdopen(int fd, const char* __restrict mode);
48
49 /*
50  * Open the specified file for reading or writing.
51  * Mode must be either "r" or "w".
52  * Returns null on error.
53  */
54 BGZF* bgzf_open(const char* path, const char* __restrict mode);
55
56 /*
57  * Close the BGZ file and free all associated resources.
58  * Does not close the underlying file descriptor if created with bgzf_fdopen.
59  * Returns zero on success, -1 on error.
60  */
61 int bgzf_close(BGZF* fp);
62
63 /*
64  * Read up to length bytes from the file storing into data.
65  * Returns the number of bytes actually read.
66  * Returns zero on end of file.
67  * Returns -1 on error.
68  */
69 int bgzf_read(BGZF* fp, void* data, int length);
70
71 /*
72  * Write length bytes from data to the file.
73  * Returns the number of bytes written.
74  * Returns -1 on error.
75  */
76 int bgzf_write(BGZF* fp, const void* data, int length);
77
78 /*
79  * Return a virtual file pointer to the current location in the file.
80  * No interpetation of the value should be made, other than a subsequent
81  * call to bgzf_seek can be used to position the file at the same point.
82  * Return value is non-negative on success.
83  * Returns -1 on error.
84  */
85 int64_t bgzf_tell(BGZF* fp);
86
87 /*
88  * Set the file to read from the location specified by pos, which must
89  * be a value previously returned by bgzf_tell for this file (but not
90  * necessarily one returned by this file handle).
91  * The where argument must be SEEK_SET.
92  * Seeking on a file opened for write is not supported.
93  * Returns zero on success, -1 on error.
94  */
95 int64_t bgzf_seek(BGZF* fp, int64_t pos, int where);
96
97 #ifdef __cplusplus
98 }
99 #endif
100
101 #endif