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 #include <barry/barry.h>
00023 #include <iomanip>
00024 #include <iostream>
00025 #include <fstream>
00026 #include <vector>
00027 #include <string>
00028 #include <getopt.h>
00029 #include "i18n.h"
00030
00031 using namespace std;
00032 using namespace Barry;
00033
00034 void Usage()
00035 {
00036 cerr
00037 << "upldif - Command line LDIF uploader\n"
00038 << " Copyright 2006-2011, Net Direct Inc. (http://www.netdirect.ca/)\n\n"
00039 << " -p pin PIN of device to talk with\n"
00040 << " If only one device plugged in, this flag is optional\n"
00041 << " -P pass Simplistic method to specify device password\n"
00042 << " -u Do the upload. If not specified, only dumps parsed\n"
00043 << " LDIF data to stdout.\n"
00044 << " -v Dump protocol data during operation\n"
00045 << " -h This help output\n"
00046 << endl;
00047 }
00048
00049 template <class Record>
00050 struct Store
00051 {
00052 std::vector<Record> records;
00053 mutable typename std::vector<Record>::const_iterator rec_it;
00054 int count;
00055
00056 Barry::ContactLdif ldif;
00057
00058
00059
00060 Store(std::istream &is)
00061 : count(0),
00062 ldif("")
00063 {
00064 Record rec;
00065 while( is ) {
00066 if( ldif.ReadLdif(is, rec) ) {
00067 count++;
00068 records.push_back(rec);
00069 }
00070 }
00071
00072 rec_it = records.begin();
00073 }
00074
00075 ~Store()
00076 {
00077 cout << "Store counted " << dec << count << " records." << endl;
00078 }
00079
00080
00081
00082 bool operator()(Record &rec, Builder &builder) const
00083 {
00084 if( rec_it == records.end() )
00085 return false;
00086 rec = *rec_it;
00087 rec_it++;
00088 return true;
00089 }
00090
00091
00092 void Dump(std::ostream &os) const
00093 {
00094 typename std::vector<Record>::const_iterator b = records.begin();
00095 for( ; b != records.end(); ++b ) {
00096 os << *b << endl;
00097 }
00098 }
00099 };
00100
00101 template <class Record>
00102 std::ostream& operator<< (std::ostream &os, const Store<Record> &store)
00103 {
00104 store.Dump(os);
00105 return os;
00106 }
00107
00108 int main(int argc, char *argv[])
00109 {
00110 INIT_I18N(PACKAGE);
00111
00112 cout.sync_with_stdio(true);
00113
00114
00115 try {
00116
00117 uint32_t pin = 0;
00118 bool data_dump = false,
00119 do_upload = false;
00120 string password;
00121
00122
00123 for(;;) {
00124 int cmd = getopt(argc, argv, "hp:P:uv");
00125 if( cmd == -1 )
00126 break;
00127
00128 switch( cmd )
00129 {
00130 case 'p':
00131 pin = strtoul(optarg, NULL, 16);
00132 break;
00133
00134 case 'P':
00135 password = optarg;
00136 break;
00137
00138 case 'u':
00139 do_upload = true;
00140 break;
00141
00142 case 'v':
00143 data_dump = true;
00144 break;
00145
00146 case 'h':
00147 default:
00148 Usage();
00149 return 0;
00150 }
00151 }
00152
00153
00154 Store<Contact> contactStore(cin);
00155
00156
00157 if( !do_upload ) {
00158 cout << contactStore << endl;
00159 return 0;
00160 }
00161
00162
00163
00164 Barry::Init(data_dump);
00165
00166
00167
00168 Barry::Probe probe;
00169 int activeDevice = probe.FindActive(pin);
00170 if( activeDevice == -1 ) {
00171 cerr << "Device not found, or not specified" << endl;
00172 return 1;
00173 }
00174
00175
00176 Barry::Controller con(probe.Get(activeDevice));
00177
00178
00179 Barry::Mode::Desktop desktop(con);
00180 desktop.Open(password.c_str());
00181
00182
00183 desktop.SaveDatabaseByType<Barry::Contact>(contactStore);
00184
00185 }
00186 catch( Usb::Error &ue) {
00187 std::cerr << "Usb::Error caught: " << ue.what() << endl;
00188 }
00189 catch( Barry::Error &se ) {
00190 std::cerr << "Barry::Error caught: " << se.what() << endl;
00191 }
00192 catch( std::exception &e ) {
00193 std::cerr << "std::exception caught: " << e.what() << endl;
00194 return 1;
00195 }
00196
00197 return 0;
00198 }
00199