Barry debian version 0.18.5-1
[barry.git] / tools / bs11nread.cc
blob6d5fda13b0c399ec4aeac9b693de634dccc347b9
1 ///
2 /// \file bs11nread.cc
3 /// Reads an boost serialization file and dumps to stdout.
4 ///
6 /*
7 Copyright (C) 2008-2013, 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 // Boost is required, but since the real Boost serialization code is in
23 // util.cc, we don't need it here.
24 #undef __BARRY_BOOST_MODE__
26 #include <barry/barry.h>
27 #include <iomanip>
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <vector>
32 #include <string>
33 #include <algorithm>
34 #include "i18n.h"
36 #include "barrygetopt.h"
37 #include "util.h"
38 #include "boostwrap.h"
40 using namespace std;
41 using namespace Barry;
43 void Usage()
45 int logical, major, minor;
46 const char *Version = Barry::Version(logical, major, minor);
48 cerr << string_vprintf(
49 _("bs11nread - Reads a boost serialization file (from btool)\n"
50 " and dumps data to stdout\n"
51 " Copyright 2008-2013, Net Direct Inc. (http://www.netdirect.ca/)\n"
52 " Using: %s\n"
53 "\n"
54 " -f file Filename to save or load handheld data to/from\n"
55 " -h This help\n"
56 " -S Show list of supported database parsers\n"),
57 Version)
58 << endl;
61 template <class Record>
62 bool Dump(const std::string &dbName, const std::string &filename)
64 if( dbName != Record::GetDBName() )
65 return false;
67 std::vector<Record> records;
68 std::string junk, errmsg;
70 if( !LoadBoostFile(filename, records, junk, errmsg) ) {
71 cerr << errmsg << endl;
72 return false;
75 cout << records.size()
76 << _(" records loaded") << endl;
77 sort(records.begin(), records.end());
79 typename std::vector<Record>::const_iterator
80 beg = records.begin(), end = records.end();
81 for( ; beg != end; beg++ ) {
82 cout << (*beg) << endl;
85 return true;
88 void DumpDB(const string &filename)
90 // filename is available, attempt to load
91 ifstream ifs(filename.c_str());
92 std::string dbName;
93 getline(ifs, dbName);
94 ifs.close();
96 // check for recognized database names
97 #undef HANDLE_PARSER
98 #define HANDLE_PARSER(tname) Dump<tname>(dbName, filename) ||
99 ALL_KNOWN_PARSER_TYPES
100 cerr << _("Unknown database name: ") << dbName << endl;
103 int main(int argc, char *argv[])
105 INIT_I18N(PACKAGE);
107 try {
108 string filename;
110 // process command line options
111 for(;;) {
112 int cmd = getopt(argc, argv, "f:hS");
113 if( cmd == -1 )
114 break;
116 switch( cmd )
118 case 'f': // filename
119 filename = optarg;
120 break;
122 case 'S': // show supported databases
123 ShowParsers(false, false);
124 return 0;
126 case 'h': // help
127 default:
128 Usage();
129 return 0;
133 // Initialize the barry library. Must be called before
134 // anything else.
135 Barry::Init();
137 if( !filename.size() ) {
138 cerr << _("Filename must be specified") << endl;
139 return 1;
142 DumpDB(filename);
145 catch( Usb::Error &ue) {
146 std::cerr << _("Usb::Error caught: ") << ue.what() << endl;
147 return 1;
149 catch( Barry::Error &se ) {
150 std::cerr << _("Barry::Error caught: ") << se.what() << endl;
151 return 1;
153 catch( std::exception &e ) {
154 std::cerr << _("std::exception caught: ") << e.what() << endl;
155 return 1;
158 return 0;