bs11nread.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       bs11nread.cc
00003 ///             Reads an boost serialization file and dumps to stdout.
00004 ///
00005 
00006 /*
00007     Copyright (C) 2008-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 // Boost is required, but since the real Boost serialization code is in
00023 // util.cc, we don't need it here.
00024 #undef __BARRY_BOOST_MODE__
00025 
00026 #include <barry/barry.h>
00027 #include <iomanip>
00028 #include <iostream>
00029 #include <fstream>
00030 #include <sstream>
00031 #include <vector>
00032 #include <string>
00033 #include <algorithm>
00034 #include "i18n.h"
00035 
00036 #include "barrygetopt.h"
00037 #include "util.h"
00038 #include "boostwrap.h"
00039 
00040 using namespace std;
00041 using namespace Barry;
00042 
00043 void Usage()
00044 {
00045    int logical, major, minor;
00046    const char *Version = Barry::Version(logical, major, minor);
00047 
00048    cerr
00049    << "bs11nread - Reads a boost serialization file (from btool)\n"
00050    << "            and dumps data to stdout\n"
00051    << "        Copyright 2008-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
00052    << "        Using: " << Version << "\n"
00053    << "\n"
00054    << "   -f file   Filename to save or load handheld data to/from\n"
00055    << "   -h        This help\n"
00056    << "   -S        Show list of supported database parsers\n"
00057    << endl;
00058 }
00059 
00060 template <class Record>
00061 bool Dump(const std::string &dbName, const std::string &filename)
00062 {
00063         if( dbName != Record::GetDBName() )
00064                 return false;
00065 
00066         std::vector<Record> records;
00067         std::string junk, errmsg;
00068 
00069         if( !LoadBoostFile(filename, records, junk, errmsg) ) {
00070                 cerr << errmsg << endl;
00071                 return false;
00072         }
00073 
00074         cout << records.size()
00075              << " records loaded" << endl;
00076         sort(records.begin(), records.end());
00077 
00078         typename std::vector<Record>::const_iterator
00079                 beg = records.begin(), end = records.end();
00080         for( ; beg != end; beg++ ) {
00081                 cout << (*beg) << endl;
00082         }
00083 
00084         return true;
00085 }
00086 
00087 void DumpDB(const string &filename)
00088 {
00089         // filename is available, attempt to load
00090         ifstream ifs(filename.c_str());
00091         std::string dbName;
00092         getline(ifs, dbName);
00093         ifs.close();
00094 
00095         // check for recognized database names
00096 #undef HANDLE_PARSER
00097 #define HANDLE_PARSER(tname) Dump<tname>(dbName, filename) ||
00098         ALL_KNOWN_PARSER_TYPES
00099                 cerr << "Unknown database name: " << dbName << endl;
00100 }
00101 
00102 int main(int argc, char *argv[])
00103 {
00104         INIT_I18N(PACKAGE);
00105 
00106         try {
00107                 string filename;
00108 
00109                 // process command line options
00110                 for(;;) {
00111                         int cmd = getopt(argc, argv, "f:hS");
00112                         if( cmd == -1 )
00113                                 break;
00114 
00115                         switch( cmd )
00116                         {
00117                         case 'f':       // filename
00118                                 filename = optarg;
00119                                 break;
00120 
00121                         case 'S':       // show supported databases
00122                                 ShowParsers(false, false);
00123                                 return 0;
00124 
00125                         case 'h':       // help
00126                         default:
00127                                 Usage();
00128                                 return 0;
00129                         }
00130                 }
00131 
00132                 // Initialize the barry library.  Must be called before
00133                 // anything else.
00134                 Barry::Init();
00135 
00136                 if( !filename.size() ) {
00137                         cerr << "Filename must be specified" << endl;
00138                         return 1;
00139                 }
00140 
00141                 DumpDB(filename);
00142 
00143         }
00144         catch( Usb::Error &ue) {
00145                 std::cerr << "Usb::Error caught: " << ue.what() << endl;
00146                 return 1;
00147         }
00148         catch( Barry::Error &se ) {
00149                 std::cerr << "Barry::Error caught: " << se.what() << endl;
00150                 return 1;
00151         }
00152         catch( std::exception &e ) {
00153                 std::cerr << "std::exception caught: " << e.what() << endl;
00154                 return 1;
00155         }
00156 
00157         return 0;
00158 }
00159