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
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-2008, 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 << " -u Do the upload. If not specified, only dumps parsed\n"
00042 << " LDIF data to stdout.\n"
00043 << " -v Dump protocol data during operation\n"
00044 << " -h This help output\n"
00045 << endl;
00046 }
00047
00048 template <class Record>
00049 struct Store
00050 {
00051 std::vector<Record> records;
00052 mutable typename std::vector<Record>::const_iterator rec_it;
00053 int count;
00054
00055 Barry::ContactLdif ldif;
00056
00057
00058
00059 Store(std::istream &is)
00060 : count(0),
00061 ldif("")
00062 {
00063 Record rec;
00064 while( is ) {
00065 if( ldif.ReadLdif(is, rec) ) {
00066 count++;
00067 records.push_back(rec);
00068 }
00069 }
00070
00071 rec_it = records.begin();
00072 }
00073
00074 ~Store()
00075 {
00076 cout << "Store counted " << dec << count << " records." << endl;
00077 }
00078
00079
00080
00081 bool operator()(Record &rec, unsigned int databaseId) const
00082 {
00083 if( rec_it == records.end() )
00084 return false;
00085 rec = *rec_it;
00086 rec_it++;
00087 return true;
00088 }
00089
00090
00091 void Dump(std::ostream &os) const
00092 {
00093 typename std::vector<Record>::const_iterator b = records.begin();
00094 for( ; b != records.end(); ++b ) {
00095 os << *b << endl;
00096 }
00097 }
00098 };
00099
00100 template <class Record>
00101 std::ostream& operator<< (std::ostream &os, const Store<Record> &store)
00102 {
00103 store.Dump(os);
00104 return os;
00105 }
00106
00107 int main(int argc, char *argv[])
00108 {
00109 cout.sync_with_stdio(true);
00110
00111
00112 try {
00113
00114 uint32_t pin = 0;
00115 bool data_dump = false,
00116 do_upload = false;
00117
00118
00119 for(;;) {
00120 int cmd = getopt(argc, argv, "hp:uv");
00121 if( cmd == -1 )
00122 break;
00123
00124 switch( cmd )
00125 {
00126 case 'p':
00127 pin = strtoul(optarg, NULL, 16);
00128 break;
00129
00130 case 'u':
00131 do_upload = true;
00132 break;
00133
00134 case 'v':
00135 data_dump = true;
00136 break;
00137
00138 case 'h':
00139 default:
00140 Usage();
00141 return 0;
00142 }
00143 }
00144
00145
00146 Store<Contact> contactStore(cin);
00147
00148
00149 if( !do_upload ) {
00150 cout << contactStore << endl;
00151 return 0;
00152 }
00153
00154
00155
00156 Barry::Init(data_dump);
00157
00158
00159
00160 Barry::Probe probe;
00161 int activeDevice = probe.FindActive(pin);
00162 if( activeDevice == -1 ) {
00163 cerr << "Device not found, or not specified" << endl;
00164 return 1;
00165 }
00166
00167
00168 Barry::Controller con(probe.Get(activeDevice));
00169
00170
00171 Barry::Mode::Desktop desktop(con);
00172 desktop.Open();
00173
00174
00175 desktop.SaveDatabaseByType<Barry::Contact>(contactStore);
00176
00177 }
00178 catch( Usb::Error &ue) {
00179 std::cerr << "Usb::Error caught: " << ue.what() << endl;
00180 }
00181 catch( Barry::Error &se ) {
00182 std::cerr << "Barry::Error caught: " << se.what() << endl;
00183 }
00184 catch( std::exception &e ) {
00185 std::cerr << "std::exception caught: " << e.what() << endl;
00186 return 1;
00187 }
00188
00189 return 0;
00190 }
00191