Added ppp and test directories to Makefile.am EXTRA_DIST
[barry/pauldeden.git] / tools / bs11nread.cc
blobee622d7534d684fd33b5cf25624343abee0790ae
1 ///
2 /// \file bs11nread.cc
3 /// Reads an boost serialization file and dumps to stdout.
4 ///
6 /*
7 Copyright (C) 2008, 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 #define __BARRY_BOOST_MODE__ // this program always requires BOOST
23 #include <barry/barry.h>
24 #include <iomanip>
25 #include <iostream>
26 #include <fstream>
27 #include <sstream>
28 #include <vector>
29 #include <string>
30 #include <algorithm>
31 #include <getopt.h>
34 using namespace std;
35 using namespace Barry;
37 void Usage()
39 int major, minor;
40 const char *Version = Barry::Version(major, minor);
42 cerr
43 << "bs11nread - Reads a boost serialization file (from btool)\n"
44 << " and dumps data to stdout\n"
45 << " Copyright 2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
46 << " Using: " << Version << "\n"
47 << "\n"
48 << " -f file Filename to save or load handheld data to/from\n"
49 << " -h This help\n"
50 << " -S Show list of supported database parsers\n"
51 << endl;
54 template <class Record>
55 bool Dump(const std::string &dbName, ifstream &ifs)
57 if( dbName != Record::GetDBName() )
58 return false;
60 std::vector<Record> records;
61 boost::archive::text_iarchive ia(ifs);
62 ia >> records;
63 cout << records.size()
64 << " records loaded" << endl;
65 sort(records.begin(), records.end());
67 typename std::vector<Record>::const_iterator
68 beg = records.begin(), end = records.end();
69 for( ; beg != end; beg++ ) {
70 cout << (*beg) << endl;
73 return true;
76 void DumpDB(const string &filename)
78 // filename is available, attempt to load
79 ifstream ifs(filename.c_str());
80 std::string dbName;
81 getline(ifs, dbName);
83 // check for recognized database names
84 Dump<Contact> (dbName, ifs) ||
85 Dump<Message> (dbName, ifs) ||
86 Dump<Calendar> (dbName, ifs) ||
87 Dump<ServiceBook> (dbName, ifs) ||
88 Dump<Memo> (dbName, ifs) ||
89 Dump<Task> (dbName, ifs) ||
90 Dump<PINMessage> (dbName, ifs) ||
91 Dump<SavedMessage> (dbName, ifs) ||
92 Dump<Folder> (dbName, ifs) ||
93 Dump<Timezone> (dbName, ifs) ||
94 cerr << "Unknown database name: " << dbName << endl;
97 void ShowParsers()
99 cout << "Supported Database parsers:\n"
100 << " Address Book\n"
101 << " Messages\n"
102 << " Calendar\n"
103 << " Service Book\n"
104 << " Memos\n"
105 << " Tasks\n"
106 << " PIN Messages\n"
107 << " Saved Email Messages\n"
108 << " Folders\n"
109 << " Time Zones\n"
110 << endl;
113 int main(int argc, char *argv[])
115 try {
116 string filename;
118 // process command line options
119 for(;;) {
120 int cmd = getopt(argc, argv, "f:hS");
121 if( cmd == -1 )
122 break;
124 switch( cmd )
126 case 'f': // filename
127 filename = optarg;
128 break;
130 case 'S': // show supported databases
131 ShowParsers();
132 return 0;
134 case 'h': // help
135 default:
136 Usage();
137 return 0;
141 // Initialize the barry library. Must be called before
142 // anything else.
143 Barry::Init();
145 if( !filename.size() ) {
146 cerr << "Filename must be specified" << endl;
147 return 1;
150 DumpDB(filename);
153 catch( boost::archive::archive_exception &ae ) {
154 cerr << "Archive exception: "
155 << ae.what() << endl;
156 return 1;
158 catch( Usb::Error &ue) {
159 std::cerr << "Usb::Error caught: " << ue.what() << endl;
160 return 1;
162 catch( Barry::Error &se ) {
163 std::cerr << "Barry::Error caught: " << se.what() << endl;
164 return 1;
166 catch( std::exception &e ) {
167 std::cerr << "std::exception caught: " << e.what() << endl;
168 return 1;
171 return 0;