comparison srf2fastq/io_lib-1.12.2/progs/hash_extract.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 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 #include <io_lib/hash_table.h>
6
7 /*
8 * Copies a single named file to stdout.
9 * Returns 0 on success
10 * 1 on failure
11 */
12 int extract(HashFile *hf, char *file) {
13 size_t len;
14 char *data;
15
16 if (data = HashFileExtract(hf, file, &len)) {
17 fwrite(data, len, 1, stdout);
18 free(data);
19 return 0;
20 }
21 return 1;
22 }
23
24 int main(int argc, char **argv) {
25 char *fofn = NULL;
26 char *hash;
27 HashFile *hf;
28 int ret = 0;
29
30 /* process command line arguments of the form -arg */
31 for (argc--, argv++; argc > 0; argc--, argv++) {
32 if (**argv != '-' || strcmp(*argv, "--") == 0)
33 break;
34
35 if (strcmp(*argv, "-I") == 0) {
36 argv++;
37 fofn = *argv;
38 argc--;
39 }
40 }
41
42 if (argc < 2 && !fofn) {
43 fprintf(stderr, "Usage: hash_extract [-I fofn] hashfile [name ...]\n");
44 return 1;
45 }
46 hash = argv[0];
47 argc--;
48 argv++;
49
50 if (NULL == (hf = HashFileOpen(hash))) {
51 perror(hash);
52 return 1;
53 }
54
55 if (fofn) {
56 FILE *fofnfp;
57 char file[256];
58
59 if (strcmp(fofn, "-") == 0) {
60 fofnfp = stdin;
61 } else {
62 if (NULL == (fofnfp = fopen(fofn, "r"))) {
63 perror(fofn);
64 return 1;
65 }
66 }
67
68 while (fgets(file, 255, fofnfp)) {
69 char *c;
70 if (c = strchr(file, '\n'))
71 *c = 0;
72
73 ret |= extract(hf, file);
74 }
75
76 fclose(fofnfp);
77 }
78
79 #ifdef _WIN32
80 _setmode(_fileno(stdout), _O_BINARY);
81 #endif
82 for (; argc; argc--, argv++) {
83 ret |= extract(hf, *argv);
84 }
85
86 HashFileDestroy(hf);
87
88 return ret;
89 }