- added tools/bs11nread.cc to read Boost serialization file data
[barry.git] / tools / bs11nread.cc
blob042319ad63cf596230b31f92008f794492dbe954
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 <getopt.h>
33 using namespace std;
34 using namespace Barry;
36 void Usage()
38 int major, minor;
39 const char *Version = Barry::Version(major, minor);
41 cerr
42 << "bs11nread - Reads a boost serialization file (from btool)\n"
43 << " and dumps data to stdout\n"
44 << " Copyright 2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
45 << " Using: " << Version << "\n"
46 << "\n"
47 << " -f file Filename to save or load handheld data to/from\n"
48 << " -h This help\n"
49 << " -S Show list of supported database parsers\n"
50 << endl;
53 template <class Record>
54 bool Dump(const std::string &dbName, ifstream &ifs)
56 if( dbName != Record::GetDBName() )
57 return false;
59 std::vector<Record> records;
60 boost::archive::text_iarchive ia(ifs);
61 ia >> records;
62 cout << records.size()
63 << " records loaded" << endl;
64 sort(records.begin(), records.end());
66 typename std::vector<Record>::const_iterator
67 beg = records.begin(), end = records.end();
68 for( ; beg != end; beg++ ) {
69 cout << (*beg) << endl;
72 return true;
75 void DumpDB(const string &filename)
77 // filename is available, attempt to load
78 ifstream ifs(filename.c_str());
79 std::string dbName;
80 getline(ifs, dbName);
82 // check for recognized database names
83 Dump<Contact> (dbName, ifs) ||
84 Dump<Message> (dbName, ifs) ||
85 Dump<Calendar> (dbName, ifs) ||
86 Dump<ServiceBook> (dbName, ifs) ||
87 Dump<Memo> (dbName, ifs) ||
88 Dump<Task> (dbName, ifs) ||
89 Dump<PINMessage> (dbName, ifs) ||
90 Dump<SavedMessage> (dbName, ifs) ||
91 Dump<Folder> (dbName, ifs) ||
92 Dump<Timezone> (dbName, ifs) ||
93 cerr << "Unknown database name: " << dbName << endl;
96 void ShowParsers()
98 cout << "Supported Database parsers:\n"
99 << " Address Book\n"
100 << " Messages\n"
101 << " Calendar\n"
102 << " Service Book\n"
103 << " Memos\n"
104 << " Tasks\n"
105 << " PIN Messages\n"
106 << " Saved Email Messages\n"
107 << " Folders\n"
108 << " Time Zones\n"
109 << endl;
112 int main(int argc, char *argv[])
114 try {
115 string filename;
117 // process command line options
118 for(;;) {
119 int cmd = getopt(argc, argv, "f:hS");
120 if( cmd == -1 )
121 break;
123 switch( cmd )
125 case 'f': // filename
126 filename = optarg;
127 break;
129 case 'S': // show supported databases
130 ShowParsers();
131 return 0;
133 case 'h': // help
134 default:
135 Usage();
136 return 0;
140 // Initialize the barry library. Must be called before
141 // anything else.
142 Barry::Init();
144 if( !filename.size() ) {
145 cerr << "Filename must be specified" << endl;
146 return 1;
149 DumpDB(filename);
152 catch( boost::archive::archive_exception &ae ) {
153 cerr << "Archive exception: "
154 << ae.what() << endl;
155 return 1;
157 catch( Usb::Error &ue) {
158 std::cerr << "Usb::Error caught: " << ue.what() << endl;
159 return 1;
161 catch( Barry::Error &se ) {
162 std::cerr << "Barry::Error caught: " << se.what() << endl;
163 return 1;
165 catch( std::exception &e ) {
166 std::cerr << "std::exception caught: " << e.what() << endl;
167 return 1;
170 return 0;