lib: added DBDB::SortByName()
[barry.git] / tools / btardump.cc
blob17e3c0408d675bc01622cd37c986cb69b9db0c04
1 ///
2 /// \file tardump.cc
3 /// Utility to dump tarball backup records to stdout.
4 ///
6 /*
7 Copyright (C) 2010-2011, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include <barry/barry.h>
23 #ifdef __BARRY_SYNC_MODE__
24 #include <barry/barrysync.h>
25 #include "mimedump.h"
26 #endif
27 #include <barry/barrybackup.h>
28 #include <iostream>
29 #include <iomanip>
30 #include <getopt.h>
32 using namespace std;
33 using namespace Barry;
35 void Usage()
37 int major, minor;
38 const char *Version = Barry::Version(major, minor);
40 cerr
41 << "btardump - Command line parser for Barry backup files\n"
42 << " Copyright 2010-2011, Net Direct Inc. (http://www.netdirect.ca/)\n"
43 << " Using: " << Version << "\n"
44 << "\n"
45 << " -d db Name of database to dump. Can be used multiple times\n"
46 << " to parse multiple databases at once. If not specified\n"
47 << " at all, all available databases from the backup are\n"
48 << " dumped.\n"
49 << " -h This help\n"
50 << " -i cs International charset for string conversions\n"
51 << " Valid values here are available with 'iconv --list'\n"
52 #ifdef __BARRY_SYNC_MODE__
53 << " -V Dump records using MIME vformats where possible\n"
54 #endif
55 << "\n"
56 << " [files...] Backup file(s), created by btool or the backup GUI.\n"
57 << endl;
60 class MyAllRecordDumpStore : public AllRecordStore
62 bool m_vformat_mode;
63 std::ostream &m_os;
65 public:
66 explicit MyAllRecordDumpStore(std::ostream &os, bool vformat_mode=false)
67 : m_vformat_mode(vformat_mode)
68 , m_os(os)
72 #undef HANDLE_PARSER
73 #define HANDLE_PARSER(tname) \
74 void operator() (const Barry::tname &r) \
75 { \
76 if( m_vformat_mode ) \
77 MimeDump<tname>::Dump(m_os, r); \
78 else \
79 m_os << r << std::endl; \
82 ALL_KNOWN_PARSER_TYPES
85 int main(int argc, char *argv[])
87 try {
88 bool vformat_mode = false;
90 vector<string> db_names;
91 vector<string> backup_files;
92 string iconvCharset;
94 // process command line options
95 for(;;) {
96 int cmd = getopt(argc, argv, "d:hi:V");
97 if( cmd == -1 )
98 break;
100 switch( cmd )
102 case 'd': // show dbname
103 db_names.push_back(string(optarg));
104 break;
106 case 'V': // vformat MIME mode
107 #ifdef __BARRY_SYNC_MODE__
108 vformat_mode = true;
109 #else
110 cerr << "-V option not supported - no Sync "
111 "library support available\n";
112 return 1;
113 #endif
114 break;
116 case 'i': // international charset (iconv)
117 iconvCharset = optarg;
118 break;
120 case 'h': // help
121 default:
122 Usage();
123 return 0;
127 // grab all backup filenames
128 while( optind < argc ) {
129 backup_files.push_back(string(argv[optind++]));
132 if( backup_files.size() == 0 ) {
133 Usage();
134 return 0;
139 Barry::Init();
141 // Create an IConverter object if needed
142 auto_ptr<IConverter> ic;
143 if( iconvCharset.size() ) {
144 ic.reset( new IConverter(iconvCharset.c_str(), true) );
147 // create the parser, and use stdout dump objects for output
148 AllRecordParser parser(cout,
149 new HexDumpParser(cout),
150 new MyAllRecordDumpStore(cout, vformat_mode));
152 for( size_t i = 0; i < backup_files.size(); i++ ) {
154 cout << "Reading file: " << backup_files[i] << endl;
156 Restore builder(backup_files[i]);
158 // add desired database names
159 for( size_t j = 0; j < db_names.size(); j++ ) {
160 builder.AddDB(db_names[i]);
163 // create the pipe to connect builder to parser and
164 // move the data
165 Pipe pipe(builder);
166 pipe.PumpFile(parser, ic.get());
170 catch( exception &e ) {
171 cerr << e.what() << endl;
172 return 1;
175 return 0;