lib+tools: updated strings to support i18n translations
[barry.git] / tools / btardump.cc
blob13ad41f6008f41659a0befa0e9a89fdfe97b9a26
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"
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-2012, 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 try {
108 bool vformat_mode = false;
110 vector<string> db_names;
111 vector<string> backup_files;
112 string iconvCharset;
114 // process command line options
115 for(;;) {
116 int cmd = getopt(argc, argv, "d:hi:V");
117 if( cmd == -1 )
118 break;
120 switch( cmd )
122 case 'd': // show dbname
123 db_names.push_back(string(optarg));
124 break;
126 case 'V': // vformat MIME mode
127 #ifdef __BARRY_SYNC_MODE__
128 vformat_mode = true;
129 #else
130 cerr << _("-V option not supported - no Sync library support available\n");
131 return 1;
132 #endif
133 break;
135 case 'i': // international charset (iconv)
136 iconvCharset = optarg;
137 break;
139 case 'h': // help
140 default:
141 Usage();
142 return 0;
146 // grab all backup filenames
147 while( optind < argc ) {
148 backup_files.push_back(string(argv[optind++]));
151 if( backup_files.size() == 0 ) {
152 Usage();
153 return 0;
158 Barry::Init();
160 // Create an IConverter object if needed
161 auto_ptr<IConverter> ic;
162 if( iconvCharset.size() ) {
163 ic.reset( new IConverter(iconvCharset.c_str(), true) );
166 // create the parser, and use stdout dump objects for output
167 AllRecordParser parser(cout,
168 new HexDumpParser(cout),
169 new MyAllRecordDumpStore(cout, vformat_mode));
171 for( size_t i = 0; i < backup_files.size(); i++ ) {
173 cout << _("Reading file: ") << backup_files[i] << endl;
175 Restore builder(backup_files[i]);
177 // add desired database names
178 for( size_t j = 0; j < db_names.size(); j++ ) {
179 builder.AddDB(db_names[i]);
182 // create the pipe to connect builder to parser and
183 // move the data
184 Pipe pipe(builder);
185 pipe.PumpFile(parser, ic.get());
189 catch( exception &e ) {
190 cerr << e.what() << endl;
191 return 1;
194 return 0;