annotate pyPRADA_1.2/tools/samtools-0.1.16/razip.c @ 3:f17965495ec9 draft default tip

Uploaded
author siyuan
date Tue, 11 Mar 2014 12:14:01 -0400
parents acc2ca1a3ba4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #include <stdio.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 #include <fcntl.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 #include <unistd.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 #include <errno.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 #include <stdlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 #include "razf.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 #define WINDOW_SIZE 4096
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 static int razf_main_usage()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 printf("\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 printf("Usage: razip [options] [file] ...\n\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 printf("Options: -c write on standard output, keep original files unchanged\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 printf(" -d decompress\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 printf(" -l list compressed file contents\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 printf(" -b INT decompress at INT position in the uncompressed file\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 printf(" -s INT decompress INT bytes in the uncompressed file\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 printf(" -h give this help\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 printf("\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 static int write_open(const char *fn, int is_forced)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 int fd = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 char c;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 if (!is_forced) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0666)) < 0 && errno == EEXIST) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 printf("razip: %s already exists; do you wish to overwrite (y or n)? ", fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 scanf("%c", &c);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 if (c != 'Y' && c != 'y') {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 printf("razip: not overwritten\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 exit(1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 if (fd < 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 fprintf(stderr, "razip: %s: Fail to write\n", fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 exit(1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 return fd;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 int main(int argc, char **argv)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 int c, compress, pstdout, is_forced;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 RAZF *rz;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 void *buffer;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 long start, end, size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 compress = 1; pstdout = 0; start = 0; size = -1; end = -1; is_forced = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 while((c = getopt(argc, argv, "cdlhfb:s:")) >= 0){
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 switch(c){
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 case 'h': return razf_main_usage();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 case 'd': compress = 0; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 case 'c': pstdout = 1; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 case 'l': compress = 2; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 case 'b': start = atol(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 case 's': size = atol(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 case 'f': is_forced = 1; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 if (size >= 0) end = start + size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 if(end >= 0 && end < start){
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 fprintf(stderr, " -- Illegal region: [%ld, %ld] --\n", start, end);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 if(compress == 1){
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 int f_src, f_dst = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 if(argc > optind){
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 if((f_src = open(argv[optind], O_RDONLY)) < 0){
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 fprintf(stderr, " -- Cannot open file: %s --\n", argv[optind]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 if(pstdout){
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 f_dst = fileno(stdout);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 char *name = malloc(sizeof(strlen(argv[optind]) + 5));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 strcpy(name, argv[optind]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 strcat(name, ".rz");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 f_dst = write_open(name, is_forced);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 if (f_dst < 0) return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 free(name);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 } else if(pstdout){
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 f_src = fileno(stdin);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 f_dst = fileno(stdout);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 } else return razf_main_usage();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 rz = razf_dopen(f_dst, "w");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 buffer = malloc(WINDOW_SIZE);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 while((c = read(f_src, buffer, WINDOW_SIZE)) > 0) razf_write(rz, buffer, c);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 razf_close(rz); // f_dst will be closed here
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 if (argc > optind && !pstdout) unlink(argv[optind]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 free(buffer);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 close(f_src);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 if(argc <= optind) return razf_main_usage();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 if(compress == 2){
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 rz = razf_open(argv[optind], "r");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 if(rz->file_type == FILE_TYPE_RZ) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 printf("%20s%20s%7s %s\n", "compressed", "uncompressed", "ratio", "name");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 printf("%20lld%20lld%6.1f%% %s\n", (long long)rz->end, (long long)rz->src_end, rz->end * 100.0f / rz->src_end,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 argv[optind]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 } else fprintf(stdout, "%s is not a regular rz file\n", argv[optind]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 int f_dst;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 if (argc > optind && !pstdout) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 char *name;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 if (strstr(argv[optind], ".rz") - argv[optind] != strlen(argv[optind]) - 3) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 printf("razip: %s: unknown suffix -- ignored\n", argv[optind]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 name = strdup(argv[optind]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119 name[strlen(name) - 3] = '\0';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 f_dst = write_open(name, is_forced);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 free(name);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 } else f_dst = fileno(stdout);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 rz = razf_open(argv[optind], "r");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 buffer = malloc(WINDOW_SIZE);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 razf_seek(rz, start, SEEK_SET);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 while(1){
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 if(end < 0) c = razf_read(rz, buffer, WINDOW_SIZE);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 else c = razf_read(rz, buffer, (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 if(c <= 0) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 start += c;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131 write(f_dst, buffer, c);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 if(end >= 0 && start >= end) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 free(buffer);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 if (!pstdout) unlink(argv[optind]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 razf_close(rz);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141