lib: added C-style BarryLogf() function for internal use
[barry.git] / tools / bs11nread.cc
blob9dd5cb0d6a7fe50ceebf49a8e6efdbdf1ac058d6
1 ///
2 /// \file bs11nread.cc
3 /// Reads an boost serialization file and dumps to stdout.
4 ///
6 /*
7 Copyright (C) 2008-2010, 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>
32 #include "i18n.h"
35 using namespace std;
36 using namespace Barry;
38 void Usage()
40 int major, minor;
41 const char *Version = Barry::Version(major, minor);
43 cerr
44 << "bs11nread - Reads a boost serialization file (from btool)\n"
45 << " and dumps data to stdout\n"
46 << " Copyright 2008-2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
47 << " Using: " << Version << "\n"
48 << "\n"
49 << " -f file Filename to save or load handheld data to/from\n"
50 << " -h This help\n"
51 << " -S Show list of supported database parsers\n"
52 << endl;
55 template <class Record>
56 bool Dump(const std::string &dbName, ifstream &ifs)
58 if( dbName != Record::GetDBName() )
59 return false;
61 std::vector<Record> records;
62 boost::archive::text_iarchive ia(ifs);
63 ia >> records;
64 cout << records.size()
65 << " records loaded" << endl;
66 sort(records.begin(), records.end());
68 typename std::vector<Record>::const_iterator
69 beg = records.begin(), end = records.end();
70 for( ; beg != end; beg++ ) {
71 cout << (*beg) << endl;
74 return true;
77 void DumpDB(const string &filename)
79 // filename is available, attempt to load
80 ifstream ifs(filename.c_str());
81 std::string dbName;
82 getline(ifs, dbName);
84 // check for recognized database names
85 Dump<Contact> (dbName, ifs) ||
86 Dump<Message> (dbName, ifs) ||
87 Dump<Calendar> (dbName, ifs) ||
88 Dump<CalendarAll> (dbName, ifs) ||
89 Dump<ServiceBook> (dbName, ifs) ||
90 Dump<Memo> (dbName, ifs) ||
91 Dump<Task> (dbName, ifs) ||
92 Dump<PINMessage> (dbName, ifs) ||
93 Dump<SavedMessage> (dbName, ifs) ||
94 Dump<Folder> (dbName, ifs) ||
95 Dump<Timezone> (dbName, ifs) ||
96 cerr << "Unknown database name: " << dbName << endl;
99 void ShowParsers()
101 cout << "Supported Database parsers:\n"
102 << " Address Book\n"
103 << " Messages\n"
104 << " Calendar\n"
105 << " Calendar - All\n"
106 << " Service Book\n"
107 << " Memos\n"
108 << " Tasks\n"
109 << " PIN Messages\n"
110 << " Saved Email Messages\n"
111 << " Folders\n"
112 << " Time Zones\n"
113 << endl;
116 int main(int argc, char *argv[])
118 INIT_I18N(PACKAGE);
120 try {
121 string filename;
123 // process command line options
124 for(;;) {
125 int cmd = getopt(argc, argv, "f:hS");
126 if( cmd == -1 )
127 break;
129 switch( cmd )
131 case 'f': // filename
132 filename = optarg;
133 break;
135 case 'S': // show supported databases
136 ShowParsers();
137 return 0;
139 case 'h': // help
140 default:
141 Usage();
142 return 0;
146 // Initialize the barry library. Must be called before
147 // anything else.
148 Barry::Init();
150 if( !filename.size() ) {
151 cerr << "Filename must be specified" << endl;
152 return 1;
155 DumpDB(filename);
158 catch( boost::archive::archive_exception &ae ) {
159 cerr << "Archive exception: "
160 << ae.what() << endl;
161 return 1;
163 catch( Usb::Error &ue) {
164 std::cerr << "Usb::Error caught: " << ue.what() << endl;
165 return 1;
167 catch( Barry::Error &se ) {
168 std::cerr << "Barry::Error caught: " << se.what() << endl;
169 return 1;
171 catch( std::exception &e ) {
172 std::cerr << "std::exception caught: " << e.what() << endl;
173 return 1;
176 return 0;