]> git.donarmstrong.com Git - samtools.git/blobdiff - razf.c
* samtools-0.1.5-31 (r448)
[samtools.git] / razf.c
diff --git a/razf.c b/razf.c
index 13d6589b0c2907858d5617786e52fbc177df432c..a5e8f5161a8d0780a820556e0e6640fd9762bb12 100644 (file)
--- a/razf.c
+++ b/razf.c
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
- *
- * To compile razf.c, zlib-1.2.3(or greater) is required.
  */
 
 #ifndef _NO_RAZF
 
 #include <fcntl.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 #include "razf.h"
 
+#if ZLIB_VERNUM < 0x1221
+struct _gz_header_s {
+    int     text;
+    uLong   time;
+    int     xflags;
+    int     os;
+    Bytef   *extra;
+    uInt    extra_len;
+    uInt    extra_max;
+    Bytef   *name;
+    uInt    name_max;
+    Bytef   *comment;
+    uInt    comm_max;
+    int     hcrc;
+    int     done;
+};
+#warning "zlib < 1.2.2.1; RAZF writing is disabled."
+#endif
+
+#define DEF_MEM_LEVEL 8
+
 static inline uint32_t byte_swap_4(uint32_t v){
        v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
        return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
@@ -54,6 +76,7 @@ static inline int is_big_endian(){
        return (c[0] != 0x01);
 }
 
+#ifndef _RZ_READONLY
 static void add_zindex(RAZF *rz, int64_t in, int64_t out){
        if(rz->index->size == rz->index->cap){
                rz->index->cap = rz->index->cap * 1.5 + 2;
@@ -82,6 +105,7 @@ static void save_zindex(RAZF *rz, int fd){
        write(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
        write(fd, rz->index->cell_offsets, sizeof(int32_t) * rz->index->size);
 }
+#endif
 
 static void load_zindex(RAZF *rz, int fd){
        int32_t i, v32;
@@ -103,8 +127,18 @@ static void load_zindex(RAZF *rz, int fd){
        }
 }
 
+#ifdef _RZ_READONLY
+static RAZF* razf_open_w(int fd)
+{
+       fprintf(stderr, "[razf_open_w] Writing is not available with zlib ver < 1.2.2.1\n");
+       return 0;
+}
+#else
 static RAZF* razf_open_w(int fd){
        RAZF *rz;
+#ifdef _WIN32
+       setmode(fd, O_BINARY);
+#endif
        rz = calloc(1, sizeof(RAZF));
        rz->mode = 'w';
        rz->filedes = fd;
@@ -232,6 +266,7 @@ int razf_write(RAZF* rz, const void *data, int size){
        _razf_buffered_write(rz, data, size);
        return ori_size;
 }
+#endif
 
 /* gzip flag byte */
 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
@@ -279,6 +314,9 @@ static RAZF* razf_open_r(int fd, int _load_index){
        int n, is_be, ret;
        int64_t end;
        unsigned char c[] = "RAZF";
+#ifdef _WIN32
+       setmode(fd, O_BINARY);
+#endif
        rz = calloc(1, sizeof(RAZF));
        rz->mode = 'r';
        rz->filedes = fd;
@@ -350,26 +388,34 @@ static RAZF* razf_open_r(int fd, int _load_index){
 }
 
 RAZF* razf_dopen(int fd, const char *mode){
-       if(strcasecmp(mode, "r") == 0) return razf_open_r(fd, 1);
-       else if(strcasecmp(mode, "w") == 0) return razf_open_w(fd);
+       if(strstr(mode, "r")) return razf_open_r(fd, 1);
+       else if(strstr(mode, "w")) return razf_open_w(fd);
        else return NULL;
 }
 
 RAZF* razf_dopen2(int fd, const char *mode)
 {
-       if(strcasecmp(mode, "r") == 0) return razf_open_r(fd, 0);
-       else if(strcasecmp(mode, "w") == 0) return razf_open_w(fd);
+       if(strstr(mode, "r")) return razf_open_r(fd, 0);
+       else if(strstr(mode, "w")) return razf_open_w(fd);
        else return NULL;
 }
 
 static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){
        int fd;
        RAZF *rz;
-       if(strcasecmp(mode, "r") == 0){
+       if(strstr(mode, "r")){
+#ifdef _WIN32
+               fd = open(filename, O_RDONLY | O_BINARY);
+#else
                fd = open(filename, O_RDONLY);
+#endif
                rz = razf_open_r(fd, _load_index);
-       } else if(strcasecmp(mode, "w") == 0){
+       } else if(strstr(mode, "w")){
+#ifdef _WIN32
+               fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
+#else
                fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+#endif
                rz = razf_open_w(fd);
        } else return NULL;
        return rz;
@@ -613,8 +659,8 @@ int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where)
 }
 
 void razf_close(RAZF *rz){
-       uint64_t v64;
        if(rz->mode == 'w'){
+#ifndef _RZ_READONLY
                razf_end_flush(rz);
                deflateEnd(rz->stream);
                save_zindex(rz, rz->filedes);
@@ -622,11 +668,12 @@ void razf_close(RAZF *rz){
                        write(rz->filedes, &rz->in, sizeof(int64_t));
                        write(rz->filedes, &rz->out, sizeof(int64_t));
                } else {
-                       v64 = byte_swap_8((uint64_t)rz->in);
+                       uint64_t v64 = byte_swap_8((uint64_t)rz->in);
                        write(rz->filedes, &v64, sizeof(int64_t));
                        v64 = byte_swap_8((uint64_t)rz->out);
                        write(rz->filedes, &v64, sizeof(int64_t));
                }
+#endif
        } else if(rz->mode == 'r'){
                if(rz->stream) inflateEnd(rz->stream);
        }