bgzf.h

00001 /* The MIT License
00002 
00003    Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
00004 
00005    Permission is hereby granted, free of charge, to any person obtaining a copy
00006    of this software and associated documentation files (the "Software"), to deal
00007    in the Software without restriction, including without limitation the rights
00008    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009    copies of the Software, and to permit persons to whom the Software is
00010    furnished to do so, subject to the following conditions:
00011 
00012    The above copyright notice and this permission notice shall be included in
00013    all copies or substantial portions of the Software.
00014 
00015    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021    THE SOFTWARE.
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 //typedef int8_t bool;
00037 
00038 typedef struct {
00039     int file_descriptor;
00040     char open_mode;  // 'r' or 'w'
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; // a pointer to a hash table
00060 } BGZF;
00061 
00062 #ifdef __cplusplus
00063 extern "C" {
00064 #endif
00065 
00066 /*
00067  * Open an existing file descriptor for reading or writing.
00068  * Mode must be either "r" or "w".
00069  * A subsequent bgzf_close will not close the file descriptor.
00070  * Returns null on error.
00071  */
00072 BGZF* bgzf_fdopen(int fd, const char* __restrict mode);
00073 
00074 /*
00075  * Open the specified file for reading or writing.
00076  * Mode must be either "r" or "w".
00077  * Returns null on error.
00078  */
00079 BGZF* bgzf_open(const char* path, const char* __restrict mode);
00080 
00081 /*
00082  * Close the BGZ file and free all associated resources.
00083  * Does not close the underlying file descriptor if created with bgzf_fdopen.
00084  * Returns zero on success, -1 on error.
00085  */
00086 int bgzf_close(BGZF* fp);
00087 
00088 /*
00089  * Read up to length bytes from the file storing into data.
00090  * Returns the number of bytes actually read.
00091  * Returns zero on end of file.
00092  * Returns -1 on error.
00093  */
00094 int bgzf_read(BGZF* fp, void* data, int length);
00095 
00096 /*
00097  * Write length bytes from data to the file.
00098  * Returns the number of bytes written.
00099  * Returns -1 on error.
00100  */
00101 int bgzf_write(BGZF* fp, const void* data, int length);
00102 
00103 /*
00104  * Return a virtual file pointer to the current location in the file.
00105  * No interpetation of the value should be made, other than a subsequent
00106  * call to bgzf_seek can be used to position the file at the same point.
00107  * Return value is non-negative on success.
00108  * Returns -1 on error.
00109  */
00110 #define bgzf_tell(fp) ((fp->block_address << 16) | (fp->block_offset & 0xFFFF))
00111 
00112 /*
00113  * Set the file to read from the location specified by pos, which must
00114  * be a value previously returned by bgzf_tell for this file (but not
00115  * necessarily one returned by this file handle).
00116  * The where argument must be SEEK_SET.
00117  * Seeking on a file opened for write is not supported.
00118  * Returns zero on success, -1 on error.
00119  */
00120 int64_t bgzf_seek(BGZF* fp, int64_t pos, int where);
00121 
00122 /*
00123  * Set the cache size. Zero to disable. By default, caching is
00124  * disabled. The recommended cache size for frequent random access is
00125  * about 8M bytes.
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; /* error */
00144         if (fp->block_length == 0) return -1; /* end-of-file */
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
Generated on Mon Feb 11 13:45:19 2013 for libStatGen Software by  doxygen 1.6.3