lib: added FifoArgs API, for use in passing command line args without command line
[barry.git] / tools / btardump.cc
blobfa88045114739a175e30b83400d9e0adbfd2d78f
1 ///
2 /// \file btardump.cc
3 /// Utility to dump tarball backup records to stdout.
4 ///
6 /*
7 Copyright (C) 2010-2012, 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>
31 #include "barrygetopt.h"
33 using namespace std;
34 using namespace Barry;
36 void Usage()
38 int logical, major, minor;
39 const char *Version = Barry::Version(logical, major, minor);
41 cerr
42 << "btardump - Command line parser for Barry backup files\n"
43 << " Copyright 2010-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
44 << " Using: " << Version << "\n"
45 << "\n"
46 << " -d db Name of database to dump. Can be used multiple times\n"
47 << " to parse multiple databases at once. If not specified\n"
48 << " at all, all available databases from the backup are\n"
49 << " dumped.\n"
50 << " -h This help\n"
51 << " -i cs International charset for string conversions\n"
52 << " Valid values here are available with 'iconv --list'\n"
53 #ifdef __BARRY_SYNC_MODE__
54 << " -V Dump records using MIME vformats where possible\n"
55 #endif
56 << "\n"
57 << " [files...] Backup file(s), created by btool or the backup GUI.\n"
58 << endl;
61 class MyAllRecordDumpStore : public AllRecordStore
63 bool m_vformat_mode;
64 std::ostream &m_os;
66 public:
67 explicit MyAllRecordDumpStore(std::ostream &os, bool vformat_mode=false)
68 : m_vformat_mode(vformat_mode)
69 , m_os(os)
73 #undef HANDLE_PARSER
75 #ifdef __BARRY_SYNC_MODE__
77 #define HANDLE_PARSER(tname) \
78 void operator() (const Barry::tname &r) \
79 { \
80 if( m_vformat_mode ) \
81 MimeDump<tname>::Dump(m_os, r); \
82 else \
83 m_os << r << std::endl; \
86 #else
88 #define HANDLE_PARSER(tname) \
89 void operator() (const Barry::tname &r) \
90 { \
91 m_os << r << std::endl; \
94 #endif
96 ALL_KNOWN_PARSER_TYPES
99 int main(int argc, char *argv[])
101 try {
102 bool vformat_mode = false;
104 vector<string> db_names;
105 vector<string> backup_files;
106 string iconvCharset;
108 // process command line options
109 for(;;) {
110 int cmd = getopt(argc, argv, "d:hi:V");
111 if( cmd == -1 )
112 break;
114 switch( cmd )
116 case 'd': // show dbname
117 db_names.push_back(string(optarg));
118 break;
120 case 'V': // vformat MIME mode
121 #ifdef __BARRY_SYNC_MODE__
122 vformat_mode = true;
123 #else
124 cerr << "-V option not supported - no Sync "
125 "library support available\n";
126 return 1;
127 #endif
128 break;
130 case 'i': // international charset (iconv)
131 iconvCharset = optarg;
132 break;
134 case 'h': // help
135 default:
136 Usage();
137 return 0;
141 // grab all backup filenames
142 while( optind < argc ) {
143 backup_files.push_back(string(argv[optind++]));
146 if( backup_files.size() == 0 ) {
147 Usage();
148 return 0;
153 Barry::Init();
155 // Create an IConverter object if needed
156 auto_ptr<IConverter> ic;
157 if( iconvCharset.size() ) {
158 ic.reset( new IConverter(iconvCharset.c_str(), true) );
161 // create the parser, and use stdout dump objects for output
162 AllRecordParser parser(cout,
163 new HexDumpParser(cout),
164 new MyAllRecordDumpStore(cout, vformat_mode));
166 for( size_t i = 0; i < backup_files.size(); i++ ) {
168 cout << "Reading file: " << backup_files[i] << endl;
170 Restore builder(backup_files[i]);
172 // add desired database names
173 for( size_t j = 0; j < db_names.size(); j++ ) {
174 builder.AddDB(db_names[i]);
177 // create the pipe to connect builder to parser and
178 // move the data
179 Pipe pipe(builder);
180 pipe.PumpFile(parser, ic.get());
184 catch( exception &e ) {
185 cerr << e.what() << endl;
186 return 1;
189 return 0;