Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/klib/blob/master/bgzf.h # klib [Ported from github.com/attractivechaos/klib] added block arena Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `klib / bgzf.h` 196 lines ยท 5861 bytes Raw Blame History 1 /* The MIT License 2 3 Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology 4 2011 Attractive Chaos 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 */ 24 25 /* The BGZF library was originally written by Bob Handsaker from the Broad 26 * Institute. It was later improved by the SAMtools developers. */ 27 28 #ifndef __BGZF_H 29 #define __BGZF_H 30 31 #include 32 #include 33 #include 34 35 #define BGZF_BLOCK_SIZE 0x10000 36 #define BGZF_MAX_BLOCK_SIZE 0x10000 37 38 #define BGZF_ERR_ZLIB 1 39 #define BGZF_ERR_HEADER 2 40 #define BGZF_ERR_IO 4 41 #define BGZF_ERR_MISUSE 8 42 43 typedef struct { 44 int open_mode:8, compress_level:8, errcode:16; 45 int cache_size; 46 int block_length, block_offset; 47 int64_t block_address; 48 void *uncompressed_block, *compressed_block; 49 void *cache; // a pointer to a hash table 50 void *fp; // actual file handler; FILE* on writing; FILE* or knetFile* on reading 51 } BGZF; 52 53 #ifndef KSTRING_T 54 #define KSTRING_T kstring_t 55 typedef struct __kstring_t { 56 size_t l, m; 57 char *s; 58 } kstring_t; 59 #endif 60 61 #ifdef __cplusplus 62 extern "C" { 63 #endif 64 65 /****************** 66 * Basic routines * 67 ******************/ 68 69 /** 70 * Open an existing file descriptor for reading or writing. 71 * 72 * @param fd file descriptor 73 * @param mode mode matching /[rwu0-9]+/: 'r' for reading, 'w' for writing and a digit specifies 74 * the zlib compression level; if both 'r' and 'w' are present, 'w' is ignored. 75 * @return BGZF file handler; 0 on error 76 */ 77 BGZF* bgzf_dopen(int fd, const char *mode); 78 79 #define bgzf_fdopen(fd, mode) bgzf_dopen((fd), (mode)) // for backward compatibility 80 81 /** 82 * Open the specified file for reading or writing. 83 */ 84 BGZF* bgzf_open(const char* path, const char *mode); 85 86 /** 87 * Close the BGZF and free all associated resources. 88 * 89 * @param fp BGZF file handler 90 * @return 0 on success and -1 on error 91 */ 92 int bgzf_close(BGZF *fp); 93 94 /** 95 * Read up to _length_ bytes from the file storing into _data_. 96 * 97 * @param fp BGZF file handler 98 * @param data data array to read into 99 * @param length size of data to read 100 * @return number of bytes actually read; 0 on end-of-file and -1 on error 101 */ 102 ssize_t bgzf_read(BGZF *fp, void *data, ssize_t length); 103 104 /** 105 * Write _length_ bytes from _data_ to the file. 106 * 107 * @param fp BGZF file handler 108 * @param data data array to write 109 * @param length size of data to write 110 * @return number of bytes actually written; -1 on error 111 */ 112 ssize_t bgzf_write(BGZF *fp, const void *data, ssize_t length); 113 114 /** 115 * Write the data in the buffer to the file. 116 */ 117 int bgzf_flush(BGZF *fp); 118 119 /** 120 * Return a virtual file pointer to the current location in the file. 121 * No interpetation of the value should be made, other than a subsequent 122 * call to bgzf_seek can be used to position the file at the same point. 123 * Return value is non-negative on success. 124 */ 125 #define bgzf_tell(fp) ((fp->block_address << 16) | (fp->block_offset & 0xFFFF)) 126 127 /** 128 * Set the file to read from the location specified by _pos_. 129 * 130 * @param fp BGZF file handler 131 * @param pos virtual file offset returned by bgzf_tell() 132 * @param whence must be SEEK_SET 133 * @return 0 on success and -1 on error 134 */ 135 int64_t bgzf_seek(BGZF *fp, int64_t pos, int whence); 136 137 /** 138 * Check if the BGZF end-of-file (EOF) marker is present 139 * 140 * @param fp BGZF file handler opened for reading 141 * @return 1 if EOF is present; 0 if not or on I/O error 142 */ 143 int bgzf_check_EOF(BGZF *fp); 144 145 /** 146 * Check if a file is in the BGZF format 147 * 148 * @param fn file name 149 * @return 1 if _fn_ is BGZF; 0 if not or on I/O error 150 */ 151 int bgzf_is_bgzf(const char *fn); 152 153 /********************* 154 * Advanced routines * 155 *********************/ 156 157 /** 158 * Set the cache size. Only effective when compiled with -DBGZF_CACHE. 159 * 160 * @param fp BGZF file handler 161 * @param size size of cache in bytes; 0 to disable caching (default) 162 */ 163 void bgzf_set_cache_size(BGZF *fp, int size); 164 165 /** 166 * Flush the file if the remaining buffer size is smaller than _size_ 167 */ 168 int bgzf_flush_try(BGZF *fp, ssize_t size); 169 170 /** 171 * Read one byte from a BGZF file. It is faster than bgzf_read() 172 * @param fp BGZF file handler 173 * @return byte read; -1 on end-of-file or error 174 */ 175 int bgzf_getc(BGZF *fp); 176 177 /** 178 * Read one line from a BGZF file. It is faster than bgzf_getc() 179 * 180 * @param fp BGZF file handler 181 * @param delim delimitor 182 * @param str string to write to; must be initialized 183 * @return length of the string; 0 on end-of-file; negative on error 184 */ 185 int bgzf_getline(BGZF *fp, int delim, kstring_t *str); 186 187 /** 188 * Read the next BGZF block. 189 */ 190 int bgzf_read_block(BGZF *fp); 191 192 #ifdef __cplusplus 193 } 194 #endif 195 196 #endif