Bumped copyright dates for 2013
[barry.git] / tools / btardump.cc
blob2c5e2112fd9f88d2cf9f358ebf5de6f3f225bfcb
1 ///
2 /// \file btardump.cc
3 /// Utility to dump tarball backup records to stdout.
4 ///
6 /*
7 Copyright (C) 2010-2013, 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 #endif
26 #include <barry/barrybackup.h>
27 #include <iostream>
28 #include <iomanip>
30 #include "barrygetopt.h"
31 #include "i18n.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 #ifdef __BARRY_SYNC_MODE__
42 string sync_mode = _(" -V Dump records using MIME vformats where possible");
43 #else
44 string sync_mode;
45 #endif
47 cerr << string_vprintf(
48 _("btardump - Command line parser for Barry backup files\n"
49 " Copyright 2010-2013, Net Direct Inc. (http://www.netdirect.ca/)\n"
50 " Using: %s\n"
51 "\n"
52 " -d db Name of database to dump. Can be used multiple times\n"
53 " to parse multiple databases at once. If not specified\n"
54 " at all, all available databases from the backup are\n"
55 " dumped.\n"
56 " -h This help\n"
57 " -i cs International charset for string conversions\n"
58 " Valid values here are available with 'iconv --list'\n"
59 "%s\n"
60 "\n"
61 " [files...] Backup file(s), created by btool or the backup GUI.\n"),
62 Version,
63 sync_mode.c_str())
64 << endl;
67 class MyAllRecordDumpStore : public AllRecordStore
69 bool m_vformat_mode;
70 std::ostream &m_os;
72 public:
73 explicit MyAllRecordDumpStore(std::ostream &os, bool vformat_mode=false)
74 : m_vformat_mode(vformat_mode)
75 , m_os(os)
79 #undef HANDLE_PARSER
81 #ifdef __BARRY_SYNC_MODE__
83 #define HANDLE_PARSER(tname) \
84 void operator() (const Barry::tname &r) \
85 { \
86 if( m_vformat_mode ) \
87 MimeDump<tname>::Dump(m_os, r); \
88 else \
89 m_os << r << std::endl; \
92 #else
94 #define HANDLE_PARSER(tname) \
95 void operator() (const Barry::tname &r) \
96 { \
97 m_os << r << std::endl; \
100 #endif
102 ALL_KNOWN_PARSER_TYPES
105 int main(int argc, char *argv[])
107 INIT_I18N(PACKAGE);
109 try {
110 bool vformat_mode = false;
112 vector<string> db_names;
113 vector<string> backup_files;
114 string iconvCharset;
116 // process command line options
117 for(;;) {
118 int cmd = getopt(argc, argv, "d:hi:V");
119 if( cmd == -1 )
120 break;
122 switch( cmd )
124 case 'd': // show dbname
125 db_names.push_back(string(optarg));
126 break;
128 case 'V': // vformat MIME mode
129 #ifdef __BARRY_SYNC_MODE__
130 vformat_mode = true;
131 #else
132 cerr << _("-V option not supported - no Sync library support available\n");
133 return 1;
134 #endif
135 break;
137 case 'i': // international charset (iconv)
138 iconvCharset = optarg;
139 break;
141 case 'h': // help
142 default:
143 Usage();
144 return 0;
148 // grab all backup filenames
149 while( optind < argc ) {
150 backup_files.push_back(string(argv[optind++]));
153 if( backup_files.size() == 0 ) {
154 Usage();
155 return 0;
160 Barry::Init();
162 // Create an IConverter object if needed
163 auto_ptr<IConverter> ic;
164 if( iconvCharset.size() ) {
165 ic.reset( new IConverter(iconvCharset.c_str(), true) );
168 // create the parser, and use stdout dump objects for output
169 AllRecordParser parser(cout,
170 new HexDumpParser(cout),
171 new MyAllRecordDumpStore(cout, vformat_mode));
173 for( size_t i = 0; i < backup_files.size(); i++ ) {
175 cout << _("Reading file: ") << backup_files[i] << endl;
177 Restore builder(backup_files[i]);
179 // add desired database names
180 for( size_t j = 0; j < db_names.size(); j++ ) {
181 builder.AddDB(db_names[i]);
184 // create the pipe to connect builder to parser and
185 // move the data
186 Pipe pipe(builder);
187 pipe.PumpFile(parser, ic.get());
191 catch( exception &e ) {
192 cerr << e.what() << endl;
193 return 1;
196 return 0;