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-2009, 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 
00033 
00034 using namespace std;
00035 using namespace Barry;
00036 
00037 void Usage()
00038 {
00039    int major, minor;
00040    const char *Version = Barry::Version(major, minor);
00041 
00042    cerr
00043    << "bs11nread - Reads a boost serialization file (from btool)\n"
00044    << "            and dumps data to stdout\n"
00045    << "        Copyright 2008-2009, Net Direct Inc. (http://www.netdirect.ca/)\n"
00046    << "        Using: " << Version << "\n"
00047    << "\n"
00048    << "   -f file   Filename to save or load handheld data to/from\n"
00049    << "   -h        This help\n"
00050    << "   -S        Show list of supported database parsers\n"
00051    << endl;
00052 }
00053 
00054 template <class Record>
00055 bool Dump(const std::string &dbName, ifstream &ifs)
00056 {
00057         if( dbName != Record::GetDBName() )
00058                 return false;
00059 
00060         std::vector<Record> records;
00061         boost::archive::text_iarchive ia(ifs);
00062         ia >> records;
00063         cout << records.size()
00064              << " records loaded" << endl;
00065         sort(records.begin(), records.end());
00066 
00067         typename std::vector<Record>::const_iterator
00068                 beg = records.begin(), end = records.end();
00069         for( ; beg != end; beg++ ) {
00070                 cout << (*beg) << endl;
00071         }
00072 
00073         return true;
00074 }
00075 
00076 void DumpDB(const string &filename)
00077 {
00078         // filename is available, attempt to load
00079         ifstream ifs(filename.c_str());
00080         std::string dbName;
00081         getline(ifs, dbName);
00082 
00083         // check for recognized database names
00084         Dump<Contact>           (dbName, ifs) ||
00085         Dump<Message>           (dbName, ifs) ||
00086         Dump<Calendar>          (dbName, ifs) ||
00087         Dump<ServiceBook>       (dbName, ifs) ||
00088         Dump<Memo>              (dbName, ifs) ||
00089         Dump<Task>              (dbName, ifs) ||
00090         Dump<PINMessage>        (dbName, ifs) ||
00091         Dump<SavedMessage>      (dbName, ifs) ||
00092         Dump<Folder>            (dbName, ifs) ||
00093         Dump<Timezone>          (dbName, ifs) ||
00094                 cerr << "Unknown database name: " << dbName << endl;
00095 }
00096 
00097 void ShowParsers()
00098 {
00099         cout << "Supported Database parsers:\n"
00100         << "   Address Book\n"
00101         << "   Messages\n"
00102         << "   Calendar\n"
00103         << "   Service Book\n"
00104         << "   Memos\n"
00105         << "   Tasks\n"
00106         << "   PIN Messages\n"
00107         << "   Saved Email Messages\n"
00108         << "   Folders\n"
00109         << "   Time Zones\n"
00110         << endl;
00111 }
00112 
00113 int main(int argc, char *argv[])
00114 {
00115         try {
00116                 string filename;
00117 
00118                 // process command line options
00119                 for(;;) {
00120                         int cmd = getopt(argc, argv, "f:hS");
00121                         if( cmd == -1 )
00122                                 break;
00123 
00124                         switch( cmd )
00125                         {
00126                         case 'f':       // filename
00127                                 filename = optarg;
00128                                 break;
00129 
00130                         case 'S':       // show supported databases
00131                                 ShowParsers();
00132                                 return 0;
00133 
00134                         case 'h':       // help
00135                         default:
00136                                 Usage();
00137                                 return 0;
00138                         }
00139                 }
00140 
00141                 // Initialize the barry library.  Must be called before
00142                 // anything else.
00143                 Barry::Init();
00144 
00145                 if( !filename.size() ) {
00146                         cerr << "Filename must be specified" << endl;
00147                         return 1;
00148                 }
00149 
00150                 DumpDB(filename);
00151 
00152         }
00153         catch( boost::archive::archive_exception &ae ) {
00154                 cerr << "Archive exception: "
00155                      << ae.what() << endl;
00156                 return 1;
00157         }
00158         catch( Usb::Error &ue) {
00159                 std::cerr << "Usb::Error caught: " << ue.what() << endl;
00160                 return 1;
00161         }
00162         catch( Barry::Error &se ) {
00163                 std::cerr << "Barry::Error caught: " << se.what() << endl;
00164                 return 1;
00165         }
00166         catch( std::exception &e ) {
00167                 std::cerr << "std::exception caught: " << e.what() << endl;
00168                 return 1;
00169         }
00170 
00171         return 0;
00172 }
00173 

Generated on Mon Jan 12 10:51:12 2009 for Barry by  doxygen 1.5.7.1