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 #include <zlib.h>
00030 #ifdef _USE_KNETFILE
00031 #include "knetfile.h"
00032 #endif
00033 
00034 //typedef int8_t bool;
00035 
00036 typedef struct {
00037     int file_descriptor;
00038     char open_mode;  // 'r' or 'w'
00039     int16_t owned_file, compress_level;
00040 #ifdef _USE_KNETFILE
00041     union {
00042         knetFile *fpr;
00043         FILE *fpw;
00044     } x;
00045 #else
00046     FILE* file;
00047 #endif
00048     int uncompressed_block_size;
00049     int compressed_block_size;
00050     void* uncompressed_block;
00051     void* compressed_block;
00052     int64_t block_address;
00053     int block_length;
00054     int block_offset;
00055     int cache_size;
00056     const char* error;
00057     void *cache; // a pointer to a hash table
00058 } BGZF;
00059 
00060 #ifdef __cplusplus
00061 extern "C" {
00062 #endif
00063 
00064 /*
00065  * Open an existing file descriptor for reading or writing.
00066  * Mode must be either "r" or "w".
00067  * A subsequent bgzf_close will not close the file descriptor.
00068  * Returns null on error.
00069  */
00070 BGZF* bgzf_fdopen(int fd, const char* __restrict mode);
00071 
00072 /*
00073  * Open the specified file for reading or writing.
00074  * Mode must be either "r" or "w".
00075  * Returns null on error.
00076  */
00077 BGZF* bgzf_open(const char* path, const char* __restrict mode);
00078 
00079 /*
00080  * Close the BGZ file and free all associated resources.
00081  * Does not close the underlying file descriptor if created with bgzf_fdopen.
00082  * Returns zero on success, -1 on error.
00083  */
00084 int bgzf_close(BGZF* fp);
00085 
00086 /*
00087  * Read up to length bytes from the file storing into data.
00088  * Returns the number of bytes actually read.
00089  * Returns zero on end of file.
00090  * Returns -1 on error.
00091  */
00092 int bgzf_read(BGZF* fp, void* data, int length);
00093 
00094 /*
00095  * Write length bytes from data to the file.
00096  * Returns the number of bytes written.
00097  * Returns -1 on error.
00098  */
00099 int bgzf_write(BGZF* fp, const void* data, int length);
00100 
00101 /*
00102  * Return a virtual file pointer to the current location in the file.
00103  * No interpetation of the value should be made, other than a subsequent
00104  * call to bgzf_seek can be used to position the file at the same point.
00105  * Return value is non-negative on success.
00106  * Returns -1 on error.
00107  */
00108 #define bgzf_tell(fp) ((fp->block_address << 16) | (fp->block_offset & 0xFFFF))
00109 
00110 /*
00111  * Set the file to read from the location specified by pos, which must
00112  * be a value previously returned by bgzf_tell for this file (but not
00113  * necessarily one returned by this file handle).
00114  * The where argument must be SEEK_SET.
00115  * Seeking on a file opened for write is not supported.
00116  * Returns zero on success, -1 on error.
00117  */
00118 int64_t bgzf_seek(BGZF* fp, int64_t pos, int where);
00119 
00120 /*
00121  * Set the cache size. Zero to disable. By default, caching is
00122  * disabled. The recommended cache size for frequent random access is
00123  * about 8M bytes.
00124  */
00125 void bgzf_set_cache_size(BGZF *fp, int cache_size);
00126 
00127 int bgzf_check_EOF(BGZF *fp);
00128 int bgzf_read_block(BGZF* fp);
00129 int bgzf_flush(BGZF* fp);
00130 int bgzf_flush_try(BGZF *fp, int size);
00131 int bgzf_check_bgzf(const char *fn);
00132 
00133 #ifdef __cplusplus
00134 }
00135 #endif
00136 
00137 static inline int bgzf_getc(BGZF *fp)
00138 {
00139     int c;
00140     if (fp->block_offset >= fp->block_length) {
00141         if (bgzf_read_block(fp) != 0) return -2; /* error */
00142         if (fp->block_length == 0) return -1; /* end-of-file */
00143     }
00144     c = ((unsigned char*)fp->uncompressed_block)[fp->block_offset++];
00145     if (fp->block_offset == fp->block_length) {
00146 #ifdef _USE_KNETFILE
00147         fp->block_address = knet_tell(fp->x.fpr);
00148 #else
00149         fp->block_address = ftello(fp->file);
00150 #endif
00151         fp->block_offset = 0;
00152         fp->block_length = 0;
00153     }
00154     return c;
00155 }
00156 
00157 #endif
Generated on Tue Sep 6 17:52:01 2011 for libStatGen Software by  doxygen 1.6.3