Mercurial > repos > dawe > srf2fastq
comparison srf2fastq/io_lib-1.12.2/progs/srf_index_hash.c @ 0:d901c9f41a6a default tip
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
author | dawe |
---|---|
date | Tue, 07 Jun 2011 17:48:05 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d901c9f41a6a |
---|---|
1 /* | |
2 * This adds a hash table index (".hsh" v1.01 format) to an SRF archive. | |
3 * It does this either inline on the file itself (provided it doesn't already | |
4 * have an index) or by producing an external index file. | |
5 */ | |
6 | |
7 /* ---------------------------------------------------------------------- */ | |
8 | |
9 #ifdef HAVE_CONFIG_H | |
10 #include "io_lib_config.h" | |
11 #endif | |
12 | |
13 #include <stdio.h> | |
14 #include <string.h> | |
15 #include <errno.h> | |
16 #include <stdlib.h> | |
17 #include <unistd.h> | |
18 #include <sys/types.h> | |
19 #include <io_lib/hash_table.h> | |
20 #include <io_lib/os.h> | |
21 #include <io_lib/array.h> | |
22 #include <io_lib/srf.h> | |
23 | |
24 /* ------------------------------------------------------------------------ */ | |
25 void usage(int code) { | |
26 printf("Usage: srf_index_hash [-c] srf_file\n"); | |
27 printf(" Options:\n"); | |
28 printf(" -c check an existing index, don't re-index\n"); | |
29 exit(code); | |
30 } | |
31 | |
32 int main(int argc, char **argv) { | |
33 srf_t *srf; | |
34 uint64_t pos; | |
35 char name[512]; | |
36 int i, type; | |
37 char *archive; | |
38 int dbh_pos_stored_sep = 0; | |
39 int check = 0; | |
40 off_t old_index = 0; | |
41 srf_index_t *idx; | |
42 | |
43 /* Parse args */ | |
44 for (i = 1; i < argc && argv[i][0] == '-'; i++) { | |
45 if (!strcmp(argv[i], "-")) { | |
46 break; | |
47 } else if (!strcmp(argv[i], "-c")) { | |
48 check = 1; | |
49 } else if (!strcmp(argv[i], "-h")) { | |
50 usage(0); | |
51 } else { | |
52 usage(1); | |
53 } | |
54 } | |
55 | |
56 if (argc != (i+1)) { | |
57 usage(1); | |
58 } | |
59 | |
60 archive = argv[i]; | |
61 | |
62 if( check ){ | |
63 srf = srf_open(archive, "rb"); | |
64 } else { | |
65 srf = srf_open(archive, "r+b"); | |
66 } | |
67 if (NULL == srf ){ | |
68 perror(argv[i]); | |
69 return 1; | |
70 } | |
71 | |
72 | |
73 idx = srf_index_create(NULL, NULL, dbh_pos_stored_sep); | |
74 if (NULL == idx) | |
75 return 1; | |
76 | |
77 /* Scan through file gathering the details to index in memory */ | |
78 while ((type = srf_next_block_details(srf, &pos, name)) >= 0) { | |
79 /* Only want this set if the last block in the file is an index */ | |
80 old_index = 0; | |
81 | |
82 switch (type) { | |
83 case SRFB_CONTAINER: | |
84 if (srf_index_add_cont_hdr(idx, pos)) | |
85 return 1; | |
86 break; | |
87 | |
88 case SRFB_TRACE_HEADER: | |
89 if (srf_index_add_trace_hdr(idx, pos)) | |
90 return 1; | |
91 break; | |
92 | |
93 case SRFB_TRACE_BODY: | |
94 if (srf_index_add_trace_body(idx, name, pos)) | |
95 return 1; | |
96 break; | |
97 | |
98 case SRFB_INDEX: | |
99 /* An old index */ | |
100 old_index = pos; | |
101 break; | |
102 | |
103 case SRFB_NULL_INDEX: | |
104 old_index = pos; | |
105 break; | |
106 | |
107 default: | |
108 abort(); | |
109 } | |
110 } | |
111 | |
112 /* the type should be -1 (EOF) */ | |
113 if( type != -1 ) | |
114 abort(); | |
115 | |
116 /* are we really at the end of the srf file */ | |
117 pos = ftell(srf->fp); | |
118 fseek(srf->fp, 0, SEEK_END); | |
119 if( pos != ftell(srf->fp) ){ | |
120 fprintf(stderr, "srf file is corrupt\n"); | |
121 return 1; | |
122 } | |
123 | |
124 if (check) { | |
125 srf_index_destroy(idx); | |
126 srf_destroy(srf, 1); | |
127 return 0; | |
128 } | |
129 | |
130 /* Write out the index */ | |
131 if (old_index) | |
132 fseeko(srf->fp, old_index, SEEK_SET); | |
133 | |
134 srf_index_stats(idx, NULL); | |
135 srf_index_write(srf, idx); | |
136 | |
137 /* Truncate incase we've somehow overwritten an old longer index */ | |
138 ftruncate(fileno(srf->fp), ftello(srf->fp)); | |
139 | |
140 srf_index_destroy(idx); | |
141 srf_destroy(srf, 1); | |
142 | |
143 return 0; | |
144 } |