bgzf.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __BGZF_H
00025 #define __BGZF_H
00026
00027 #include <stdint.h>
00028 #include <stdio.h>
00029 #ifdef __ZLIB_AVAILABLE__
00030 #include <zlib.h>
00031 #endif
00032 #ifdef _USE_KNETFILE
00033 #include "knetfile.h"
00034 #endif
00035
00036
00037
00038 typedef struct {
00039 int file_descriptor;
00040 char open_mode;
00041 int16_t owned_file, compress_level;
00042 #ifdef _USE_KNETFILE
00043 union {
00044 knetFile *fpr;
00045 FILE *fpw;
00046 } x;
00047 #else
00048 FILE* file;
00049 #endif
00050 int uncompressed_block_size;
00051 int compressed_block_size;
00052 void* uncompressed_block;
00053 void* compressed_block;
00054 int64_t block_address;
00055 int block_length;
00056 int block_offset;
00057 int cache_size;
00058 const char* error;
00059 void *cache;
00060 } BGZF;
00061
00062 #ifdef __cplusplus
00063 extern "C" {
00064 #endif
00065
00066
00067
00068
00069
00070
00071
00072 BGZF* bgzf_fdopen(int fd, const char* __restrict mode);
00073
00074
00075
00076
00077
00078
00079 BGZF* bgzf_open(const char* path, const char* __restrict mode);
00080
00081
00082
00083
00084
00085
00086 int bgzf_close(BGZF* fp);
00087
00088
00089
00090
00091
00092
00093
00094 int bgzf_read(BGZF* fp, void* data, int length);
00095
00096
00097
00098
00099
00100
00101 int bgzf_write(BGZF* fp, const void* data, int length);
00102
00103
00104
00105
00106
00107
00108
00109
00110 #define bgzf_tell(fp) ((fp->block_address << 16) | (fp->block_offset & 0xFFFF))
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 int64_t bgzf_seek(BGZF* fp, int64_t pos, int where);
00121
00122
00123
00124
00125
00126
00127 void bgzf_set_cache_size(BGZF *fp, int cache_size);
00128
00129 int bgzf_check_EOF(BGZF *fp);
00130 int bgzf_read_block(BGZF* fp);
00131 int bgzf_flush(BGZF* fp);
00132 int bgzf_flush_try(BGZF *fp, int size);
00133 int bgzf_check_bgzf(const char *fn);
00134
00135 #ifdef __cplusplus
00136 }
00137 #endif
00138
00139 static inline int bgzf_getc(BGZF *fp)
00140 {
00141 int c;
00142 if (fp->block_offset >= fp->block_length) {
00143 if (bgzf_read_block(fp) != 0) return -2;
00144 if (fp->block_length == 0) return -1;
00145 }
00146 c = ((unsigned char*)fp->uncompressed_block)[fp->block_offset++];
00147 if (fp->block_offset == fp->block_length) {
00148 #ifdef _USE_KNETFILE
00149 fp->block_address = knet_tell(fp->x.fpr);
00150 #else
00151 #if defined(_WIN32) || defined(_MSC_VER)
00152 fp->block_address = ftell(fp->file);
00153 #else
00154 fp->block_address = ftello(fp->file);
00155 #endif
00156 #endif
00157 fp->block_offset = 0;
00158 fp->block_length = 0;
00159 }
00160 return c;
00161 }
00162
00163 #endif