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