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-2010, 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 #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-2010, 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         // filename is available, attempt to load
00080         ifstream ifs(filename.c_str());
00081         std::string dbName;
00082         getline(ifs, dbName);
00083 
00084         // check for recognized database names
00085         Dump<Contact>           (dbName, ifs) ||
00086         Dump<Message>           (dbName, ifs) ||
00087         Dump<Calendar>          (dbName, ifs) ||
00088         Dump<ServiceBook>       (dbName, ifs) ||
00089         Dump<Memo>              (dbName, ifs) ||
00090         Dump<Task>              (dbName, ifs) ||
00091         Dump<PINMessage>        (dbName, ifs) ||
00092         Dump<SavedMessage>      (dbName, ifs) ||
00093         Dump<Folder>            (dbName, ifs) ||
00094         Dump<Timezone>          (dbName, ifs) ||
00095                 cerr << "Unknown database name: " << dbName << endl;
00096 }
00097 
00098 void ShowParsers()
00099 {
00100         cout << "Supported Database parsers:\n"
00101         << "   Address Book\n"
00102         << "   Messages\n"
00103         << "   Calendar\n"
00104         << "   Service Book\n"
00105         << "   Memos\n"
00106         << "   Tasks\n"
00107         << "   PIN Messages\n"
00108         << "   Saved Email Messages\n"
00109         << "   Folders\n"
00110         << "   Time Zones\n"
00111         << endl;
00112 }
00113 
00114 int main(int argc, char *argv[])
00115 {
00116         INIT_I18N(PACKAGE);
00117 
00118         try {
00119                 string filename;
00120 
00121                 // process command line options
00122                 for(;;) {
00123                         int cmd = getopt(argc, argv, "f:hS");
00124                         if( cmd == -1 )
00125                                 break;
00126 
00127                         switch( cmd )
00128                         {
00129                         case 'f':       // filename
00130                                 filename = optarg;
00131                                 break;
00132 
00133                         case 'S':       // show supported databases
00134                                 ShowParsers();
00135                                 return 0;
00136 
00137                         case 'h':       // help
00138                         default:
00139                                 Usage();
00140                                 return 0;
00141                         }
00142                 }
00143 
00144                 // Initialize the barry library.  Must be called before
00145                 // anything else.
00146                 Barry::Init();
00147 
00148                 if( !filename.size() ) {
00149                         cerr << "Filename must be specified" << endl;
00150                         return 1;
00151                 }
00152 
00153                 DumpDB(filename);
00154 
00155         }
00156         catch( boost::archive::archive_exception &ae ) {
00157                 cerr << "Archive exception: "
00158                      << ae.what() << endl;
00159                 return 1;
00160         }
00161         catch( Usb::Error &ue) {
00162                 std::cerr << "Usb::Error caught: " << ue.what() << endl;
00163                 return 1;
00164         }
00165         catch( Barry::Error &se ) {
00166                 std::cerr << "Barry::Error caught: " << se.what() << endl;
00167                 return 1;
00168         }
00169         catch( std::exception &e ) {
00170                 std::cerr << "std::exception caught: " << e.what() << endl;
00171                 return 1;
00172         }
00173 
00174         return 0;
00175 }
00176 

Generated on 29 Mar 2010 for Barry by  doxygen 1.6.1