3 /// Utility to dump tarball backup records to stdout.
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>
26 #include <barry/barrybackup.h>
32 using namespace Barry
;
37 const char *Version
= Barry::Version(major
, minor
);
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"
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"
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"
55 << " [files...] Backup file(s), created by btool or the backup GUI.\n"
59 #ifdef __BARRY_SYNC_MODE__
60 template <class Record
>
64 static void Dump(std::ostream
&os
, const Record
&rec
)
69 static bool Supported() { return false; }
73 class MimeDump
<Contact
>
76 static void Dump(std::ostream
&os
, const Contact
&rec
)
79 os
<< vcard
.ToVCard(rec
) << endl
;
82 static bool Supported() { return true; }
86 class MimeDump
<Calendar
>
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; }
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; }
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; }
127 class MyAllRecordDumpStore
: public AllRecordStore
133 explicit MyAllRecordDumpStore(std::ostream
&os
, bool vformat_mode
=false)
134 : m_vformat_mode(vformat_mode
)
140 #define HANDLE_PARSER(tname) \
141 void operator() (const Barry::tname &r) \
143 if( m_vformat_mode ) \
144 MimeDump<tname>::Dump(m_os, r); \
146 m_os << r << std::endl; \
149 ALL_KNOWN_PARSER_TYPES
152 int main(int argc
, char *argv
[])
155 bool vformat_mode
= false;
157 vector
<string
> db_names
;
158 vector
<string
> backup_files
;
161 // process command line options
163 int cmd
= getopt(argc
, argv
, "d:hi:V");
169 case 'd': // show dbname
170 db_names
.push_back(string(optarg
));
173 case 'V': // vformat MIME mode
174 #ifdef __BARRY_SYNC_MODE__
177 cerr
<< "-V option not supported - no Sync "
178 "library support available\n";
183 case 'i': // international charset (iconv)
184 iconvCharset
= optarg
;
194 // grab all backup filenames
195 while( optind
< argc
) {
196 backup_files
.push_back(string(argv
[optind
++]));
199 if( backup_files
.size() == 0 ) {
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
233 pipe
.PumpFile(parser
, ic
.get());
237 catch( exception
&e
) {
238 cerr
<< e
.what() << endl
;