~ubuntu-branches/ubuntu/raring/samtools/raring

« back to all changes in this revision

Viewing changes to razf.c

  • Committer: Bazaar Package Importer
  • Author(s): Charles Plessy
  • Date: 2009-11-17 21:38:24 UTC
  • Revision ID: james.westby@ubuntu.com-20091117213824-dfouynpy3r7ismpj
Tags: 0.1.7a~dfsg-1
* New upstream release: new script sam2vcf.pl, and many other changes.
* Package converted to the format ‘3.0 (quilt)’ (debian/source/format).
* Wrote a manual page for razip (debian/razip.1).
* Better clean the example directory to make the source package
  buildable twice in a row (debian/rules).

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
#include <unistd.h>
39
39
#include "razf.h"
40
40
 
 
41
 
41
42
#if ZLIB_VERNUM < 0x1221
42
43
struct _gz_header_s {
43
44
    int     text;
107
108
}
108
109
#endif
109
110
 
 
111
#ifdef _USE_KNETFILE
 
112
static void load_zindex(RAZF *rz, knetFile *fp){
 
113
#else
110
114
static void load_zindex(RAZF *rz, int fd){
 
115
#endif
111
116
        int32_t i, v32;
112
117
        int is_be;
113
118
        if(!rz->load_index) return;
114
119
        if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
115
120
        is_be = is_big_endian();
 
121
#ifdef _USE_KNETFILE
 
122
        knet_read(fp, &rz->index->size, sizeof(int));
 
123
#else
116
124
        read(fd, &rz->index->size, sizeof(int));
 
125
#endif
117
126
        if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size);
118
127
        rz->index->cap = rz->index->size;
119
128
        v32 = rz->index->size / RZ_BIN_SIZE + 1;
120
129
        rz->index->bin_offsets  = malloc(sizeof(int64_t) * v32);
 
130
#ifdef _USE_KNETFILE
 
131
        knet_read(fp, rz->index->bin_offsets, sizeof(int64_t) * v32);
 
132
#else
121
133
        read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
 
134
#endif
122
135
        rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size);
 
136
#ifdef _USE_KNETFILE
 
137
        knet_read(fp, rz->index->cell_offsets, sizeof(int) * rz->index->size);
 
138
#else
123
139
        read(fd, rz->index->cell_offsets, sizeof(int) * rz->index->size);
 
140
#endif
124
141
        if(!is_be){
125
142
                for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
126
143
                for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
141
158
#endif
142
159
        rz = calloc(1, sizeof(RAZF));
143
160
        rz->mode = 'w';
 
161
#ifdef _USE_KNETFILE
 
162
    rz->x.fpw = fd;
 
163
#else
144
164
        rz->filedes = fd;
 
165
#endif
145
166
        rz->stream = calloc(sizeof(z_stream), 1);
146
167
        rz->inbuf  = malloc(RZ_BUFFER_SIZE);
147
168
        rz->outbuf = malloc(RZ_BUFFER_SIZE);
176
197
                deflate(rz->stream, Z_NO_FLUSH);
177
198
                rz->out += tout - rz->stream->avail_out;
178
199
                if(rz->stream->avail_out) break;
 
200
#ifdef _USE_KNETFILE
 
201
                write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
 
202
#else
179
203
                write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
 
204
#endif
180
205
                rz->stream->avail_out = RZ_BUFFER_SIZE;
181
206
                rz->stream->next_out  = rz->outbuf;
182
207
                if(rz->stream->avail_in == 0) break;
192
217
                rz->buf_off = rz->buf_len = 0;
193
218
        }
194
219
        if(rz->stream->avail_out){
 
220
#ifdef _USE_KNETFILE    
 
221
                write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
 
222
#else        
195
223
                write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
 
224
#endif
196
225
                rz->stream->avail_out = RZ_BUFFER_SIZE;
197
226
                rz->stream->next_out  = rz->outbuf;
198
227
        }
201
230
                deflate(rz->stream, Z_FULL_FLUSH);
202
231
                rz->out += tout - rz->stream->avail_out;
203
232
                if(rz->stream->avail_out == 0){
 
233
#ifdef _USE_KNETFILE    
 
234
                        write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
 
235
#else            
204
236
                        write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
 
237
#endif
205
238
                        rz->stream->avail_out = RZ_BUFFER_SIZE;
206
239
                        rz->stream->next_out  = rz->outbuf;
207
240
                } else break;
221
254
                deflate(rz->stream, Z_FINISH);
222
255
                rz->out += tout - rz->stream->avail_out;
223
256
                if(rz->stream->avail_out < RZ_BUFFER_SIZE){
 
257
#ifdef _USE_KNETFILE        
 
258
                        write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
 
259
#else            
224
260
                        write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
 
261
#endif
225
262
                        rz->stream->avail_out = RZ_BUFFER_SIZE;
226
263
                        rz->stream->next_out  = rz->outbuf;
227
264
                } else break;
308
345
        return n;
309
346
}
310
347
 
 
348
#ifdef _USE_KNETFILE
 
349
static RAZF* razf_open_r(knetFile *fp, int _load_index){
 
350
#else
311
351
static RAZF* razf_open_r(int fd, int _load_index){
 
352
#endif
312
353
        RAZF *rz;
313
354
        int ext_off, ext_len;
314
355
        int n, is_be, ret;
315
356
        int64_t end;
316
357
        unsigned char c[] = "RAZF";
317
 
#ifdef _WIN32
318
 
        setmode(fd, O_BINARY);
319
 
#endif
320
358
        rz = calloc(1, sizeof(RAZF));
321
359
        rz->mode = 'r';
 
360
#ifdef _USE_KNETFILE
 
361
    rz->x.fpr = fp;
 
362
#else
 
363
#ifdef _WIN32
 
364
        setmode(fd, O_BINARY);
 
365
#endif
322
366
        rz->filedes = fd;
 
367
#endif
323
368
        rz->stream = calloc(sizeof(z_stream), 1);
324
369
        rz->inbuf  = malloc(RZ_BUFFER_SIZE);
325
370
        rz->outbuf = malloc(RZ_BUFFER_SIZE);
326
371
        rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
 
372
#ifdef _USE_KNETFILE
 
373
    n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
 
374
#else
327
375
        n = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
 
376
#endif
328
377
        ret = _read_gz_header(rz->inbuf, n, &ext_off, &ext_len);
329
378
        if(ret == 0){
330
379
                PLAIN_FILE:
355
404
        }
356
405
        rz->load_index = _load_index;
357
406
        rz->file_type = FILE_TYPE_RZ;
 
407
#ifdef _USE_KNETFILE
 
408
        if(knet_seek(fp, -16, SEEK_END) == -1){
 
409
#else
358
410
        if(lseek(fd, -16, SEEK_END) == -1){
 
411
#endif
359
412
                UNSEEKABLE:
360
413
                rz->seekable = 0;
361
414
                rz->index = NULL;
363
416
        } else {
364
417
                is_be = is_big_endian();
365
418
                rz->seekable = 1;
 
419
#ifdef _USE_KNETFILE
 
420
        knet_read(fp, &end, sizeof(int64_t));
 
421
#else
366
422
                read(fd, &end, sizeof(int64_t));
 
423
#endif        
367
424
                if(!is_be) rz->src_end = (int64_t)byte_swap_8((uint64_t)end);
368
425
                else rz->src_end = end;
 
426
 
 
427
#ifdef _USE_KNETFILE
 
428
                knet_read(fp, &end, sizeof(int64_t));
 
429
#else
369
430
                read(fd, &end, sizeof(int64_t));
 
431
#endif        
370
432
                if(!is_be) rz->end = (int64_t)byte_swap_8((uint64_t)end);
371
433
                else rz->end = end;
372
434
                if(n > rz->end){
374
436
                        n = rz->end;
375
437
                }
376
438
                if(rz->end > rz->src_end){
 
439
#ifdef _USE_KNETFILE
 
440
            knet_seek(fp, rz->in, SEEK_SET);
 
441
#else
377
442
                        lseek(fd, rz->in, SEEK_SET);
 
443
#endif
378
444
                        goto UNSEEKABLE;
379
445
                }
 
446
#ifdef _USE_KNETFILE
 
447
        knet_seek(fp, rz->end, SEEK_SET);
 
448
                if(knet_tell(fp) != rz->end){
 
449
                        knet_seek(fp, rz->in, SEEK_SET);
 
450
#else
380
451
                if(lseek(fd, rz->end, SEEK_SET) != rz->end){
381
452
                        lseek(fd, rz->in, SEEK_SET);
 
453
#endif
382
454
                        goto UNSEEKABLE;
383
455
                }
 
456
#ifdef _USE_KNETFILE
 
457
                load_zindex(rz, fp);
 
458
                knet_seek(fp, n, SEEK_SET);
 
459
#else
384
460
                load_zindex(rz, fd);
385
461
                lseek(fd, n, SEEK_SET);
 
462
#endif
386
463
        }
387
464
        return rz;
388
465
}
389
466
 
 
467
#ifdef _USE_KNETFILE
 
468
RAZF* razf_dopen(int fd, const char *mode){
 
469
    if (strstr(mode, "r")) fprintf(stderr,"[razf_dopen] implement me\n");
 
470
    else if(strstr(mode, "w")) return razf_open_w(fd);
 
471
        return NULL;
 
472
}
 
473
 
 
474
RAZF* razf_dopen2(int fd, const char *mode)
 
475
{
 
476
    fprintf(stderr,"[razf_dopen2] implement me\n");
 
477
    return NULL;
 
478
}
 
479
#else
390
480
RAZF* razf_dopen(int fd, const char *mode){
391
481
        if(strstr(mode, "r")) return razf_open_r(fd, 1);
392
482
        else if(strstr(mode, "w")) return razf_open_w(fd);
399
489
        else if(strstr(mode, "w")) return razf_open_w(fd);
400
490
        else return NULL;
401
491
}
 
492
#endif
402
493
 
403
494
static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){
404
495
        int fd;
405
496
        RAZF *rz;
406
497
        if(strstr(mode, "r")){
 
498
#ifdef _USE_KNETFILE
 
499
        knetFile *fd = knet_open(filename, "r");
 
500
        if (fd == 0) {
 
501
            fprintf(stderr, "[_razf_open] fail to open %s\n", filename);
 
502
            return NULL;
 
503
        }
 
504
#else
407
505
#ifdef _WIN32
408
506
                fd = open(filename, O_RDONLY | O_BINARY);
409
507
#else
410
508
                fd = open(filename, O_RDONLY);
411
509
#endif
 
510
#endif
 
511
                if(fd < 0) return NULL;
412
512
                rz = razf_open_r(fd, _load_index);
413
513
        } else if(strstr(mode, "w")){
414
514
#ifdef _WIN32
415
 
                fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
 
515
                fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
416
516
#else
417
 
                fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
 
517
                fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
418
518
#endif
 
519
                if(fd < 0) return NULL;
419
520
                rz = razf_open_w(fd);
420
521
        } else return NULL;
421
522
        return rz;
435
536
        switch(rz->file_type){
436
537
                case FILE_TYPE_PLAIN:
437
538
                        if(rz->end == 0x7fffffffffffffffLL){
 
539
#ifdef _USE_KNETFILE
 
540
                                if(knet_seek(rz->x.fpr, 0, SEEK_CUR) == -1) return 0;
 
541
                n = knet_tell(rz->x.fpr);
 
542
                                knet_seek(rz->x.fpr, 0, SEEK_END);
 
543
                rz->end = knet_tell(rz->x.fpr);
 
544
                                knet_seek(rz->x.fpr, n, SEEK_SET);
 
545
#else
438
546
                                if((n = lseek(rz->filedes, 0, SEEK_CUR)) == -1) return 0;
439
547
                                rz->end = lseek(rz->filedes, 0, SEEK_END);
440
548
                                lseek(rz->filedes, n, SEEK_SET);
 
549
#endif                
441
550
                        }
442
551
                        *u_size = *c_size = rz->end;
443
552
                        return 1;
457
566
        int ret, tin;
458
567
        if(rz->z_eof || rz->z_err) return 0;
459
568
        if (rz->file_type == FILE_TYPE_PLAIN) {
 
569
#ifdef _USE_KNETFILE
 
570
                ret = knet_read(rz->x.fpr, data, size);
 
571
#else
460
572
                ret = read(rz->filedes, data, size);
 
573
#endif        
461
574
                if (ret == 0) rz->z_eof = 1;
462
575
                return ret;
463
576
        }
467
580
                if(rz->stream->avail_in == 0){
468
581
                        if(rz->in >= rz->end){ rz->z_eof = 1; break; }
469
582
                        if(rz->end - rz->in < RZ_BUFFER_SIZE){
 
583
#ifdef _USE_KNETFILE
 
584
                                rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, rz->end -rz->in);
 
585
#else
470
586
                                rz->stream->avail_in = read(rz->filedes, rz->inbuf, rz->end -rz->in);
 
587
#endif        
471
588
                        } else {
 
589
#ifdef _USE_KNETFILE
 
590
                                rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
 
591
#else
472
592
                                rz->stream->avail_in = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
 
593
#endif        
473
594
                        }
474
595
                        if(rz->stream->avail_in == 0){
475
596
                                rz->z_eof = 1;
481
602
                ret = inflate(rz->stream, Z_BLOCK);
482
603
                rz->in += tin - rz->stream->avail_in;
483
604
                if(ret == Z_NEED_DICT || ret == Z_MEM_ERROR || ret == Z_DATA_ERROR){
484
 
                        fprintf(stderr, "[_razf_read] inflate error: %d (at %s:%d)\n", ret, __FILE__, __LINE__);
 
605
                        fprintf(stderr, "[_razf_read] inflate error: %d %s (at %s:%d)\n", ret, rz->stream->msg ? rz->stream->msg : "", __FILE__, __LINE__);
485
606
                        rz->z_err = 1;
486
607
                        break;
487
608
                }
566
687
                }
567
688
                if(rz->buf_flush) continue;
568
689
                rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
569
 
                if(rz->z_eof) break;
 
690
                if(rz->z_eof || rz->z_err) break;
570
691
        }
571
692
        rz->out += ori_size - size;
572
693
        return ori_size - size;
573
694
}
574
695
 
575
696
static void _razf_reset_read(RAZF *rz, int64_t in, int64_t out){
 
697
#ifdef _USE_KNETFILE
 
698
        knet_seek(rz->x.fpr, in, SEEK_SET);
 
699
#else
576
700
        lseek(rz->filedes, in, SEEK_SET);
 
701
#endif
577
702
        rz->in  = in;
578
703
        rz->out = out;
579
704
        rz->block_pos = in;
592
717
        if(rz->file_type == FILE_TYPE_PLAIN){
593
718
                rz->buf_off = rz->buf_len = 0;
594
719
                pos = block_start + block_offset;
 
720
#ifdef _USE_KNETFILE
 
721
                knet_seek(rz->x.fpr, pos, SEEK_SET);
 
722
        pos = knet_tell(rz->x.fpr);
 
723
#else
595
724
                pos = lseek(rz->filedes, pos, SEEK_SET);
 
725
#endif
596
726
                rz->out = rz->in = pos;
597
727
                return pos;
598
728
        }
614
744
        if (where == SEEK_CUR) pos += rz->out;
615
745
        else if (where == SEEK_END) pos += rz->src_end;
616
746
        if(rz->file_type == FILE_TYPE_PLAIN){
 
747
#ifdef _USE_KNETFILE
 
748
                knet_seek(rz->x.fpr, pos, SEEK_SET);
 
749
        seek_pos = knet_tell(rz->x.fpr);
 
750
#else
617
751
                seek_pos = lseek(rz->filedes, pos, SEEK_SET);
 
752
#endif
618
753
                rz->buf_off = rz->buf_len = 0;
619
754
                rz->out = rz->in = seek_pos;
620
755
                return seek_pos;
663
798
#ifndef _RZ_READONLY
664
799
                razf_end_flush(rz);
665
800
                deflateEnd(rz->stream);
 
801
#ifdef _USE_KNETFILE
 
802
                save_zindex(rz, rz->x.fpw);
 
803
                if(is_big_endian()){
 
804
                        write(rz->x.fpw, &rz->in, sizeof(int64_t));
 
805
                        write(rz->x.fpw, &rz->out, sizeof(int64_t));
 
806
                } else {
 
807
                        uint64_t v64 = byte_swap_8((uint64_t)rz->in);
 
808
                        write(rz->x.fpw, &v64, sizeof(int64_t));
 
809
                        v64 = byte_swap_8((uint64_t)rz->out);
 
810
                        write(rz->x.fpw, &v64, sizeof(int64_t));
 
811
                }
 
812
#else
666
813
                save_zindex(rz, rz->filedes);
667
814
                if(is_big_endian()){
668
815
                        write(rz->filedes, &rz->in, sizeof(int64_t));
674
821
                        write(rz->filedes, &v64, sizeof(int64_t));
675
822
                }
676
823
#endif
 
824
#endif
677
825
        } else if(rz->mode == 'r'){
678
826
                if(rz->stream) inflateEnd(rz->stream);
679
827
        }
691
839
                free(rz->index);
692
840
        }
693
841
        free(rz->stream);
 
842
#ifdef _USE_KNETFILE
 
843
    if (rz->mode == 'r')
 
844
        knet_close(rz->x.fpr);
 
845
    if (rz->mode == 'w')
 
846
        close(rz->x.fpw);
 
847
#else
694
848
        close(rz->filedes);
 
849
#endif
695
850
        free(rz);
696
851
}
697
852