tools: added exception handling to balxparse.cc
[barry.git] / tools / btardump.cc
blob0d7a62b56713108db75688cefbef700fd50c36c2
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 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 void Dump(std::ostream &os, const Contact &rec)
78 Sync::vCard vcard;
79 os << vcard.ToVCard(rec) << endl;
82 static bool Supported() { return true; }
84 #endif
86 class MyAllRecordDumpStore : public AllRecordDumpStore
88 bool vformat_mode;
90 public:
91 explicit MyAllRecordDumpStore(std::ostream &os, bool vformat_mode=false)
92 : AllRecordDumpStore(os)
93 , vformat_mode(vformat_mode)
96 virtual void operator() (const Barry::Contact &);
97 virtual void operator() (const Barry::Calendar &);
98 virtual void operator() (const Barry::CalendarAll &);
99 virtual void operator() (const Barry::Memo &);
100 virtual void operator() (const Barry::Task &);
103 void MyAllRecordDumpStore::operator() (const Barry::Contact &rec)
105 if( vformat_mode ) {
106 #ifdef __BARRY_SYNC_MODE__
107 Sync::vCard vcard;
108 m_os << vcard.ToVCard(rec) << endl;
109 #endif
111 else {
112 m_os << rec << std::endl;
116 void MyAllRecordDumpStore::operator() (const Barry::Calendar &rec)
118 if( vformat_mode ) {
119 #ifdef __BARRY_SYNC_MODE__
120 Sync::vTimeConverter vtc;
121 Sync::vCalendar vcal(vtc);
122 m_os << vcal.ToVCal(rec) << endl;
123 #endif
125 else {
126 m_os << rec << std::endl;
130 void MyAllRecordDumpStore::operator() (const Barry::CalendarAll &rec)
132 if( vformat_mode ) {
133 #ifdef __BARRY_SYNC_MODE__
134 Sync::vTimeConverter vtc;
135 Sync::vCalendar vcal(vtc);
136 m_os << vcal.ToVCal(rec) << endl;
137 #endif
139 else {
140 m_os << rec << std::endl;
144 void MyAllRecordDumpStore::operator() (const Barry::Memo &rec)
146 if( vformat_mode ) {
147 #ifdef __BARRY_SYNC_MODE__
148 Sync::vJournal vjournal;
149 m_os << vjournal.ToMemo(rec) << endl;
150 #endif
152 else {
153 m_os << rec << std::endl;
157 void MyAllRecordDumpStore::operator() (const Barry::Task &rec)
159 if( vformat_mode ) {
160 #ifdef __BARRY_SYNC_MODE__
161 Sync::vTimeConverter vtc;
162 Sync::vTodo vtodo(vtc);
163 m_os << vtodo.ToTask(rec) << endl;
164 #endif
166 else {
167 m_os << rec << std::endl;
171 int main(int argc, char *argv[])
173 try {
174 bool vformat_mode = false;
176 vector<string> db_names;
177 vector<string> backup_files;
178 string iconvCharset;
180 // process command line options
181 for(;;) {
182 int cmd = getopt(argc, argv, "d:hi:V");
183 if( cmd == -1 )
184 break;
186 switch( cmd )
188 case 'd': // show dbname
189 db_names.push_back(string(optarg));
190 break;
192 case 'V': // vformat MIME mode
193 #ifdef __BARRY_SYNC_MODE__
194 vformat_mode = true;
195 #else
196 cerr << "-V option not supported - no Sync "
197 "library support available\n";
198 return 1;
199 #endif
200 break;
202 case 'i': // international charset (iconv)
203 iconvCharset = optarg;
204 break;
206 case 'h': // help
207 default:
208 Usage();
209 return 0;
213 // grab all backup filenames
214 while( optind < argc ) {
215 backup_files.push_back(string(argv[optind++]));
218 if( backup_files.size() == 0 ) {
219 Usage();
220 return 0;
225 Barry::Init();
227 // Create an IConverter object if needed
228 auto_ptr<IConverter> ic;
229 if( iconvCharset.size() ) {
230 ic.reset( new IConverter(iconvCharset.c_str(), true) );
233 // create the parser, and use stdout dump objects for output
234 AllRecordParser parser(cout,
235 new HexDumpParser(cout),
236 new MyAllRecordDumpStore(cout, vformat_mode));
238 for( size_t i = 0; i < backup_files.size(); i++ ) {
240 cout << "Reading file: " << backup_files[i] << endl;
242 Restore builder(backup_files[i]);
244 // add desired database names
245 for( size_t j = 0; j < db_names.size(); j++ ) {
246 builder.AddDB(db_names[i]);
249 // create the pipe to connect builder to parser and
250 // move the data
251 Pipe pipe(builder);
252 pipe.PumpFile(parser, ic.get());
256 catch( exception &e ) {
257 cerr << e.what() << endl;
258 return 1;
261 return 0;