lib: moved mimedump.h from tools/ into the library (mimeio.h)
[barry/progweb.git] / tools / btardump.cc
blob60236d6263f8b783fbe5890ef0b678e00d8a6582
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 #endif
26 #include <barry/barrybackup.h>
27 #include <iostream>
28 #include <iomanip>
30 #include "barrygetopt.h"
32 using namespace std;
33 using namespace Barry;
35 void Usage()
37 int logical, major, minor;
38 const char *Version = Barry::Version(logical, major, minor);
40 cerr
41 << "btardump - Command line parser for Barry backup files\n"
42 << " Copyright 2010-2012, 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
74 #ifdef __BARRY_SYNC_MODE__
76 #define HANDLE_PARSER(tname) \
77 void operator() (const Barry::tname &r) \
78 { \
79 if( m_vformat_mode ) \
80 MimeDump<tname>::Dump(m_os, r); \
81 else \
82 m_os << r << std::endl; \
85 #else
87 #define HANDLE_PARSER(tname) \
88 void operator() (const Barry::tname &r) \
89 { \
90 m_os << r << std::endl; \
93 #endif
95 ALL_KNOWN_PARSER_TYPES
98 int main(int argc, char *argv[])
100 try {
101 bool vformat_mode = false;
103 vector<string> db_names;
104 vector<string> backup_files;
105 string iconvCharset;
107 // process command line options
108 for(;;) {
109 int cmd = getopt(argc, argv, "d:hi:V");
110 if( cmd == -1 )
111 break;
113 switch( cmd )
115 case 'd': // show dbname
116 db_names.push_back(string(optarg));
117 break;
119 case 'V': // vformat MIME mode
120 #ifdef __BARRY_SYNC_MODE__
121 vformat_mode = true;
122 #else
123 cerr << "-V option not supported - no Sync "
124 "library support available\n";
125 return 1;
126 #endif
127 break;
129 case 'i': // international charset (iconv)
130 iconvCharset = optarg;
131 break;
133 case 'h': // help
134 default:
135 Usage();
136 return 0;
140 // grab all backup filenames
141 while( optind < argc ) {
142 backup_files.push_back(string(argv[optind++]));
145 if( backup_files.size() == 0 ) {
146 Usage();
147 return 0;
152 Barry::Init();
154 // Create an IConverter object if needed
155 auto_ptr<IConverter> ic;
156 if( iconvCharset.size() ) {
157 ic.reset( new IConverter(iconvCharset.c_str(), true) );
160 // create the parser, and use stdout dump objects for output
161 AllRecordParser parser(cout,
162 new HexDumpParser(cout),
163 new MyAllRecordDumpStore(cout, vformat_mode));
165 for( size_t i = 0; i < backup_files.size(); i++ ) {
167 cout << "Reading file: " << backup_files[i] << endl;
169 Restore builder(backup_files[i]);
171 // add desired database names
172 for( size_t j = 0; j < db_names.size(); j++ ) {
173 builder.AddDB(db_names[i]);
176 // create the pipe to connect builder to parser and
177 // move the data
178 Pipe pipe(builder);
179 pipe.PumpFile(parser, ic.get());
183 catch( exception &e ) {
184 cerr << e.what() << endl;
185 return 1;
188 return 0;