1 /*
2  * Copyright (c) 2005-2008, 2010, 2013 Genome Research Ltd.
3  * Author(s): James Bonfield
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *    1. Redistributions of source code must retain the above copyright notice,
9  *       this list of conditions and the following disclaimer.
10  *
11  *    2. Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *
16  *    3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
17  *    Institute nor the names of its contributors may be used to endorse
18  *    or promote products derived from this software without specific
19  *    prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
22  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
25  * LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include "io_lib_config.h"
36 #endif
37 
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 
42 #include <io_lib/hash_table.h>
43 #include <io_lib/os.h>
44 
45 /*
46  * Dumps a textual represenation of the hash table to stdout.
47  */
HashTableLongDump(HashFile * hf,FILE * fp,int long_format)48 void HashTableLongDump(HashFile *hf, FILE *fp, int long_format) {
49     HashTable *h = hf->h;
50     int i;
51 
52     for (i = 0; i < h->nbuckets; i++) {
53 	HashItem *hi;
54 	for (hi = h->bucket[i]; hi; hi = hi->next) {
55 	    HashFileItem *hfi;
56 	    hfi = (HashFileItem *)hi->data.p;
57 	    if (long_format) {
58 		char *aname;
59 
60 		if (hf->archives &&
61 		    hfi->archive < hf->narchives) {
62 		    aname = hf->archives[hfi->archive];
63 		} else {
64 		    aname = "?";
65 		}
66 		fprintf(fp, "%10"PRId64" %6"PRId32" %.*s %s\n",
67 			hfi->pos, hfi->size, hi->key_len, hi->key,
68 			aname);
69 		/*
70 		fprintf(fp, "%10ld %6d %.*s\n",
71 			hfi->pos, hfi->size, hi->key_len, hi->key);
72 		*/
73 	    } else {
74 		fprintf(fp, "%.*s\n", hi->key_len, hi->key);
75 	    }
76 	}
77     }
78 }
79 
80 
81 /*
82  * Lists the contents of a .hash file
83  */
main(int argc,char ** argv)84 int main(int argc, char **argv) {
85     FILE *fp;
86     HashFile *hf;
87     int long_format = 0;
88 
89     /* process command line arguments of the form -arg */
90     if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
91 	long_format = 1;
92 	argc--;
93 	argv++;
94     }
95     if (argc >= 2) {
96 	fp = fopen(argv[1], "rb");
97 	if (NULL == fp) {
98 	    perror(argv[1]);
99 	    return 1;
100 	}
101     } else {
102 	fp = stdin;
103     }
104 
105     hf = HashFileLoad(fp);
106     if (hf) {
107 	HashTableLongDump(hf, stdout, long_format);
108 	HashFileDestroy(hf);
109     }
110 
111     return 0;
112 }
113