tools: let MimeDump<> template overrides do the work for us in btardump
[barry.git] / tools / btardump.cc
blobfcaab444c6c79d14c3270b5f484526af2a8f0e36
1 ///
2 /// \file tardump.cc
3 /// Utility to dump tarball backup records to stdout.
4 ///
6 /*
7 Copyright (C) 2010, 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>
29 #include <getopt.h>
31 using namespace std;
32 using namespace Barry;
34 void Usage()
36 int major, minor;
37 const char *Version = Barry::Version(major, minor);
39 cerr
40 << "btardump - Command line parser for Barry backup files\n"
41 << " Copyright 2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
42 << " Using: " << Version << "\n"
43 << "\n"
44 << " -d db Name of database to dump. Can be used multiple times\n"
45 << " to parse multiple databases at once. If not specified\n"
46 << " at all, all available databases from the backup are\n"
47 << " dumped.\n"
48 << " -h This help\n"
49 << " -i cs International charset for string conversions\n"
50 << " Valid values here are available with 'iconv --list'\n"
51 #ifdef __BARRY_SYNC_MODE__
52 << " -V Dump records using MIME vformats where possible\n"
53 #endif
54 << "\n"
55 << " [files...] Backup file(s), created by btool or the backup GUI.\n"
56 << endl;
59 #ifdef __BARRY_SYNC_MODE__
60 template <class Record>
61 class MimeDump
63 public:
64 static void Dump(std::ostream &os, const Record &rec)
66 os << rec << endl;
69 static bool Supported() { return false; }
72 template <>
73 class MimeDump<Contact>
75 public:
76 static void Dump(std::ostream &os, const Contact &rec)
78 Sync::vCard vcard;
79 os << vcard.ToVCard(rec) << endl;
82 static bool Supported() { return true; }
85 template <>
86 class MimeDump<Calendar>
88 public:
89 static void Dump(std::ostream &os, const Calendar &rec)
91 Sync::vTimeConverter vtc;
92 Sync::vCalendar vcal(vtc);
93 os << vcal.ToVCal(rec) << endl;
96 static bool Supported() { return true; }
99 template <>
100 class MimeDump<Memo>
102 public:
103 static void Dump(std::ostream &os, const Memo &rec)
105 Sync::vJournal vjournal;
106 os << vjournal.ToMemo(rec) << endl;
109 static bool Supported() { return true; }
112 template <>
113 class MimeDump<Task>
115 public:
116 static void Dump(std::ostream &os, const Task &rec)
118 Sync::vTimeConverter vtc;
119 Sync::vTodo vtodo(vtc);
120 os << vtodo.ToTask(rec) << endl;
123 static bool Supported() { return true; }
125 #endif
127 class MyAllRecordDumpStore : public AllRecordStore
129 bool m_vformat_mode;
130 std::ostream &m_os;
132 public:
133 explicit MyAllRecordDumpStore(std::ostream &os, bool vformat_mode=false)
134 : m_vformat_mode(vformat_mode)
135 , m_os(os)
139 #undef HANDLE_PARSER
140 #define HANDLE_PARSER(tname) \
141 void operator() (const Barry::tname &r) \
143 if( m_vformat_mode ) \
144 MimeDump<tname>::Dump(m_os, r); \
145 else \
146 m_os << r << std::endl; \
149 ALL_KNOWN_PARSER_TYPES
152 int main(int argc, char *argv[])
154 try {
155 bool vformat_mode = false;
157 vector<string> db_names;
158 vector<string> backup_files;
159 string iconvCharset;
161 // process command line options
162 for(;;) {
163 int cmd = getopt(argc, argv, "d:hi:V");
164 if( cmd == -1 )
165 break;
167 switch( cmd )
169 case 'd': // show dbname
170 db_names.push_back(string(optarg));
171 break;
173 case 'V': // vformat MIME mode
174 #ifdef __BARRY_SYNC_MODE__
175 vformat_mode = true;
176 #else
177 cerr << "-V option not supported - no Sync "
178 "library support available\n";
179 return 1;
180 #endif
181 break;
183 case 'i': // international charset (iconv)
184 iconvCharset = optarg;
185 break;
187 case 'h': // help
188 default:
189 Usage();
190 return 0;
194 // grab all backup filenames
195 while( optind < argc ) {
196 backup_files.push_back(string(argv[optind++]));
199 if( backup_files.size() == 0 ) {
200 Usage();
201 return 0;
206 Barry::Init();
208 // Create an IConverter object if needed
209 auto_ptr<IConverter> ic;
210 if( iconvCharset.size() ) {
211 ic.reset( new IConverter(iconvCharset.c_str(), true) );
214 // create the parser, and use stdout dump objects for output
215 AllRecordParser parser(cout,
216 new HexDumpParser(cout),
217 new MyAllRecordDumpStore(cout, vformat_mode));
219 for( size_t i = 0; i < backup_files.size(); i++ ) {
221 cout << "Reading file: " << backup_files[i] << endl;
223 Restore builder(backup_files[i]);
225 // add desired database names
226 for( size_t j = 0; j < db_names.size(); j++ ) {
227 builder.AddDB(db_names[i]);
230 // create the pipe to connect builder to parser and
231 // move the data
232 Pipe pipe(builder);
233 pipe.PumpFile(parser, ic.get());
237 catch( exception &e ) {
238 cerr << e.what() << endl;
239 return 1;
242 return 0;