Update in preparation for new upstream release.
[samtools.git] / bgzf.h
1 /* The MIT License
2
3    Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
4
5    Permission is hereby granted, free of charge, to any person obtaining a copy
6    of this software and associated documentation files (the "Software"), to deal
7    in the Software without restriction, including without limitation the rights
8    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9    copies of the Software, and to permit persons to whom the Software is
10    furnished to do so, subject to the following conditions:
11
12    The above copyright notice and this permission notice shall be included in
13    all copies or substantial portions of the Software.
14
15    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21    THE SOFTWARE.
22 */
23
24 #ifndef __BGZF_H
25 #define __BGZF_H
26
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdbool.h>
30 #include <zlib.h>
31 #ifdef _USE_KNETFILE
32 #include "knetfile.h"
33 #endif
34
35 //typedef int8_t bool;
36
37 typedef struct {
38     int file_descriptor;
39     char open_mode;  // 'r' or 'w'
40     bool owned_file, is_uncompressed;
41 #ifdef _USE_KNETFILE
42         union {
43                 knetFile *fpr;
44                 FILE *fpw;
45         } x;
46 #else
47     FILE* file;
48 #endif
49     int uncompressed_block_size;
50     int compressed_block_size;
51     void* uncompressed_block;
52     void* compressed_block;
53     int64_t block_address;
54     int block_length;
55     int block_offset;
56         int cache_size;
57     const char* error;
58         void *cache; // a pointer to a hash table
59 } BGZF;
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 /*
66  * Open an existing file descriptor for reading or writing.
67  * Mode must be either "r" or "w".
68  * A subsequent bgzf_close will not close the file descriptor.
69  * Returns null on error.
70  */
71 BGZF* bgzf_fdopen(int fd, const char* __restrict mode);
72
73 /*
74  * Open the specified file for reading or writing.
75  * Mode must be either "r" or "w".
76  * Returns null on error.
77  */
78 BGZF* bgzf_open(const char* path, const char* __restrict mode);
79
80 /*
81  * Close the BGZ file and free all associated resources.
82  * Does not close the underlying file descriptor if created with bgzf_fdopen.
83  * Returns zero on success, -1 on error.
84  */
85 int bgzf_close(BGZF* fp);
86
87 /*
88  * Read up to length bytes from the file storing into data.
89  * Returns the number of bytes actually read.
90  * Returns zero on end of file.
91  * Returns -1 on error.
92  */
93 int bgzf_read(BGZF* fp, void* data, int length);
94
95 /*
96  * Write length bytes from data to the file.
97  * Returns the number of bytes written.
98  * Returns -1 on error.
99  */
100 int bgzf_write(BGZF* fp, const void* data, int length);
101
102 /*
103  * Return a virtual file pointer to the current location in the file.
104  * No interpetation of the value should be made, other than a subsequent
105  * call to bgzf_seek can be used to position the file at the same point.
106  * Return value is non-negative on success.
107  * Returns -1 on error.
108  */
109 int64_t bgzf_tell(BGZF* fp);
110
111 /*
112  * Set the file to read from the location specified by pos, which must
113  * be a value previously returned by bgzf_tell for this file (but not
114  * necessarily one returned by this file handle).
115  * The where argument must be SEEK_SET.
116  * Seeking on a file opened for write is not supported.
117  * Returns zero on success, -1 on error.
118  */
119 int64_t bgzf_seek(BGZF* fp, int64_t pos, int where);
120
121 /*
122  * Set the cache size. Zero to disable. By default, caching is
123  * disabled. The recommended cache size for frequent random access is
124  * about 8M bytes.
125  */
126 void bgzf_set_cache_size(BGZF *fp, int cache_size);
127
128 int bgzf_check_EOF(BGZF *fp);
129
130 #ifdef __cplusplus
131 }
132 #endif
133
134 #endif