bs11nread.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #define __BARRY_BOOST_MODE__ // this program always requires BOOST
00023 #include <barry/barry.h>
00024 #include <iomanip>
00025 #include <iostream>
00026 #include <fstream>
00027 #include <sstream>
00028 #include <vector>
00029 #include <string>
00030 #include <algorithm>
00031 #include <getopt.h>
00032 #include "i18n.h"
00033
00034
00035 using namespace std;
00036 using namespace Barry;
00037
00038 void Usage()
00039 {
00040 int major, minor;
00041 const char *Version = Barry::Version(major, minor);
00042
00043 cerr
00044 << "bs11nread - Reads a boost serialization file (from btool)\n"
00045 << " and dumps data to stdout\n"
00046 << " Copyright 2008-2011, Net Direct Inc. (http://www.netdirect.ca/)\n"
00047 << " Using: " << Version << "\n"
00048 << "\n"
00049 << " -f file Filename to save or load handheld data to/from\n"
00050 << " -h This help\n"
00051 << " -S Show list of supported database parsers\n"
00052 << endl;
00053 }
00054
00055 template <class Record>
00056 bool Dump(const std::string &dbName, ifstream &ifs)
00057 {
00058 if( dbName != Record::GetDBName() )
00059 return false;
00060
00061 std::vector<Record> records;
00062 boost::archive::text_iarchive ia(ifs);
00063 ia >> records;
00064 cout << records.size()
00065 << " records loaded" << endl;
00066 sort(records.begin(), records.end());
00067
00068 typename std::vector<Record>::const_iterator
00069 beg = records.begin(), end = records.end();
00070 for( ; beg != end; beg++ ) {
00071 cout << (*beg) << endl;
00072 }
00073
00074 return true;
00075 }
00076
00077 void DumpDB(const string &filename)
00078 {
00079
00080 ifstream ifs(filename.c_str());
00081 std::string dbName;
00082 getline(ifs, dbName);
00083
00084
00085 #undef HANDLE_PARSER
00086 #define HANDLE_PARSER(tname) Dump<tname>(dbName, ifs) ||
00087 ALL_KNOWN_PARSER_TYPES
00088 cerr << "Unknown database name: " << dbName << endl;
00089 }
00090
00091 void ShowParsers()
00092 {
00093 cout << "Supported Database parsers:\n"
00094 #undef HANDLE_PARSER
00095 #define HANDLE_PARSER(tname) << " " << tname::GetDBName() << "\n"
00096 ALL_KNOWN_PARSER_TYPES
00097 << endl;
00098 }
00099
00100 int main(int argc, char *argv[])
00101 {
00102 INIT_I18N(PACKAGE);
00103
00104 try {
00105 string filename;
00106
00107
00108 for(;;) {
00109 int cmd = getopt(argc, argv, "f:hS");
00110 if( cmd == -1 )
00111 break;
00112
00113 switch( cmd )
00114 {
00115 case 'f':
00116 filename = optarg;
00117 break;
00118
00119 case 'S':
00120 ShowParsers();
00121 return 0;
00122
00123 case 'h':
00124 default:
00125 Usage();
00126 return 0;
00127 }
00128 }
00129
00130
00131
00132 Barry::Init();
00133
00134 if( !filename.size() ) {
00135 cerr << "Filename must be specified" << endl;
00136 return 1;
00137 }
00138
00139 DumpDB(filename);
00140
00141 }
00142 catch( boost::archive::archive_exception &ae ) {
00143 cerr << "Archive exception: "
00144 << ae.what() << endl;
00145 return 1;
00146 }
00147 catch( Usb::Error &ue) {
00148 std::cerr << "Usb::Error caught: " << ue.what() << endl;
00149 return 1;
00150 }
00151 catch( Barry::Error &se ) {
00152 std::cerr << "Barry::Error caught: " << se.what() << endl;
00153 return 1;
00154 }
00155 catch( std::exception &e ) {
00156 std::cerr << "std::exception caught: " << e.what() << endl;
00157 return 1;
00158 }
00159
00160 return 0;
00161 }
00162