| 1 |
#include <iostream> |
| 2 |
#include <fstream> |
| 3 |
#include <cstdlib> |
| 4 |
#include <cstring> |
| 5 |
#include <unistd.h> |
| 6 |
#include <stdexcept> |
| 7 |
|
| 8 |
#include "test_utils.hh" |
| 9 |
|
| 10 |
#include "geners/GeneralCatalog.hh" |
| 11 |
#include "geners/CatalogIO.hh" |
| 12 |
|
| 13 |
using namespace gs; |
| 14 |
using namespace std; |
| 15 |
|
| 16 |
int main(int argc, char* argv[]) |
| 17 |
{ |
| 18 |
const char *progname = strrchr(argv[0], '/'); |
| 19 |
if (progname) |
| 20 |
++progname; |
| 21 |
else |
| 22 |
progname = argv[0]; |
| 23 |
|
| 24 |
bool useRegexForName = true; |
| 25 |
bool useRegexForCategory = true; |
| 26 |
|
| 27 |
int c; |
| 28 |
bool errflg = false; |
| 29 |
extern int optind; |
| 30 |
while((c = getopt(argc, argv, "cn")) != EOF) |
| 31 |
{ |
| 32 |
switch (c) |
| 33 |
{ |
| 34 |
case 'c': |
| 35 |
useRegexForCategory = false; |
| 36 |
break; |
| 37 |
case 'n': |
| 38 |
useRegexForName = false; |
| 39 |
break; |
| 40 |
default: |
| 41 |
errflg = true; |
| 42 |
break; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
if (argc - optind != 3 || errflg) |
| 47 |
{ |
| 48 |
cerr << "\nUsage: " << progname |
| 49 |
<< " [-n] [-c] namePattern categoryPattern filename\n\n" |
| 50 |
<< "Flags -n and -c can be used to turn off regular expression matching\n" |
| 51 |
<< "for names and categories, respectively, and to search for the exact\n" |
| 52 |
<< "match instead.\n" |
| 53 |
<< endl; |
| 54 |
return 1; |
| 55 |
} |
| 56 |
|
| 57 |
char* namePattern = argv[optind]; |
| 58 |
char* categoryPattern = argv[optind+1]; |
| 59 |
char* filename = argv[optind+2]; |
| 60 |
|
| 61 |
ifstream in(filename, ios_base::binary); |
| 62 |
if (!in.is_open()) |
| 63 |
{ |
| 64 |
cerr << "Error: failed to open file \"" << filename << "\"" |
| 65 |
<< std::endl; |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
unsigned compressionCode = 0, mergeLevel = 0; |
| 70 |
std::vector<std::string> annotations; |
| 71 |
CPP11_auto_ptr<GeneralCatalog> cat(readBinaryCatalog<GeneralCatalog>( |
| 72 |
in, &compressionCode, &mergeLevel, &annotations, true)); |
| 73 |
if (cat.get() == 0) |
| 74 |
{ |
| 75 |
cerr << "Error: failed to read catalog from file \"" << filename << "\"" |
| 76 |
<< endl; |
| 77 |
return 1; |
| 78 |
} |
| 79 |
|
| 80 |
const SearchSpecifier& nameSearch = useRegexForName ? |
| 81 |
SearchSpecifier(Regex(namePattern)) : |
| 82 |
SearchSpecifier(namePattern); |
| 83 |
const SearchSpecifier& categorySearch = useRegexForCategory ? |
| 84 |
SearchSpecifier(Regex(categoryPattern)) : |
| 85 |
SearchSpecifier(categoryPattern); |
| 86 |
|
| 87 |
std::vector<unsigned long long> idlist; |
| 88 |
try { |
| 89 |
cat->search(nameSearch, categorySearch, &idlist); |
| 90 |
} |
| 91 |
catch (std::exception& e) { |
| 92 |
cerr << "Error in search: " << e.what() << endl; |
| 93 |
return 1; |
| 94 |
} |
| 95 |
|
| 96 |
const unsigned long sz = idlist.size(); |
| 97 |
if (sz) |
| 98 |
{ |
| 99 |
for (unsigned long item=0; item<sz; ++item) |
| 100 |
{ |
| 101 |
CPP11_shared_ptr<const CatalogEntry> e = |
| 102 |
cat->retrieveEntry(idlist[item]); |
| 103 |
if (item) |
| 104 |
cout << '\n'; |
| 105 |
e->humanReadable(cout); |
| 106 |
} |
| 107 |
} |
| 108 |
else |
| 109 |
cout << "No matching items" << endl; |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |