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