annotate PsiCLASS-1.0.2/samtools-0.1.19/razip.c @ 0:903fc43d6227 draft default tip

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