lib: changed asserts to exceptions, to guarantee these are never used
[barry.git] / tools / bs11nread.cc
blob31bbc2fdcd672533820dfcb53ee2630894f40e6c
1 ///
2 /// \file bs11nread.cc
3 /// Reads an boost serialization file and dumps to stdout.
4 ///
6 /*
7 Copyright (C) 2008-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 // 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
49 << "bs11nread - Reads a boost serialization file (from btool)\n"
50 << " and dumps data to stdout\n"
51 << " Copyright 2008-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
52 << " Using: " << Version << "\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 << endl;
60 template <class Record>
61 bool Dump(const std::string &dbName, const std::string &filename)
63 if( dbName != Record::GetDBName() )
64 return false;
66 std::vector<Record> records;
67 std::string junk, errmsg;
69 if( !LoadBoostFile(filename, records, junk, errmsg) ) {
70 cerr << errmsg << endl;
71 return false;
74 cout << records.size()
75 << " records loaded" << endl;
76 sort(records.begin(), records.end());
78 typename std::vector<Record>::const_iterator
79 beg = records.begin(), end = records.end();
80 for( ; beg != end; beg++ ) {
81 cout << (*beg) << endl;
84 return true;
87 void DumpDB(const string &filename)
89 // filename is available, attempt to load
90 ifstream ifs(filename.c_str());
91 std::string dbName;
92 getline(ifs, dbName);
93 ifs.close();
95 // check for recognized database names
96 #undef HANDLE_PARSER
97 #define HANDLE_PARSER(tname) Dump<tname>(dbName, filename) ||
98 ALL_KNOWN_PARSER_TYPES
99 cerr << "Unknown database name: " << dbName << endl;
102 int main(int argc, char *argv[])
104 INIT_I18N(PACKAGE);
106 try {
107 string filename;
109 // process command line options
110 for(;;) {
111 int cmd = getopt(argc, argv, "f:hS");
112 if( cmd == -1 )
113 break;
115 switch( cmd )
117 case 'f': // filename
118 filename = optarg;
119 break;
121 case 'S': // show supported databases
122 ShowParsers(false, false);
123 return 0;
125 case 'h': // help
126 default:
127 Usage();
128 return 0;
132 // Initialize the barry library. Must be called before
133 // anything else.
134 Barry::Init();
136 if( !filename.size() ) {
137 cerr << "Filename must be specified" << endl;
138 return 1;
141 DumpDB(filename);
144 catch( Usb::Error &ue) {
145 std::cerr << "Usb::Error caught: " << ue.what() << endl;
146 return 1;
148 catch( Barry::Error &se ) {
149 std::cerr << "Barry::Error caught: " << se.what() << endl;
150 return 1;
152 catch( std::exception &e ) {
153 std::cerr << "std::exception caught: " << e.what() << endl;
154 return 1;
157 return 0;