- added src/endian.h... still need to add configure support to
[barry.git] / src / upldif.cc
blob1a09724928aff8363c7eea370f441dfeaf558a09
1 ///
2 /// \file upldif.cc
3 /// LDIF contact uploader
4 ///
6 /*
7 Copyright (C) 2006, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "barry.h"
23 #include <iomanip>
24 #include <iostream>
25 #include <fstream>
26 #include <vector>
27 #include <string>
28 #include <getopt.h>
31 using namespace std;
32 using namespace Barry;
34 void Usage()
36 cerr
37 << "upldif - Command line LDIF uploader\n"
38 << " Copyright 2006, Net Direct Inc. (http://www.netdirect.ca/)\n\n"
39 << " -p pin PIN of device to talk with\n"
40 << " If only one device plugged in, this flag is optional\n"
41 << " -u Do the upload. If not specified, only dumps parsed\n"
42 << " LDIF data to stdout.\n"
43 << " -v Dump protocol data during operation\n"
44 << endl;
47 class Ldif2Contact
49 public:
50 Ldif2Contact() {}
51 bool operator()(Contact &rec)
53 return rec.ReadLdif(cin);
57 template <class Record>
58 struct Store
60 std::vector<Record> records;
61 mutable typename std::vector<Record>::const_iterator rec_it;
62 int count;
64 Store(std::istream &is)
65 : count(0)
67 Record rec;
68 while( is ) {
69 if( rec.ReadLdif(is) ) {
70 count++;
71 records.push_back(rec);
75 rec_it = records.begin();
77 ~Store()
79 cout << "Store counted " << dec << count << " records." << endl;
82 // retrieval operator
83 bool operator()(Record &rec, unsigned int databaseId) const
85 if( rec_it == records.end() )
86 return false;
87 rec = *rec_it;
88 rec_it++;
89 return true;
92 void Dump(std::ostream &os) const
94 typename std::vector<Record>::const_iterator b = records.begin();
95 for( ; b != records.end(); ++b ) {
96 os << *b << endl;
101 template <class Record>
102 std::ostream& operator<< (std::ostream &os, const Store<Record> &store)
104 store.Dump(os);
105 return os;
108 int main(int argc, char *argv[])
110 cout.sync_with_stdio(true); // leave this on, since libusb uses
111 // stdio for debug messages
113 try {
115 uint32_t pin = 0;
116 bool data_dump = false,
117 do_upload = false;
119 // process command line options
120 for(;;) {
121 int cmd = getopt(argc, argv, "hp:uv");
122 if( cmd == -1 )
123 break;
125 switch( cmd )
127 case 'p': // Blackberry PIN
128 pin = strtoul(optarg, NULL, 16);
129 break;
131 case 'u': // do upload
132 do_upload = true;
133 break;
135 case 'v': // data dump on
136 data_dump = true;
137 break;
139 case 'h': // help
140 default:
141 Usage();
142 return 0;
146 // Read all contacts from stdin
147 Store<Contact> contactStore(cin);
148 if( !do_upload) {
149 // only dump to stdout
150 cout << contactStore << endl;
151 return 0;
154 // Initialize the barry library. Must be called before
155 // anything else.
156 Barry::Init(data_dump);
158 // Probe the USB bus for Blackberry devices
159 // If user has specified a PIN, search for it
160 Barry::Probe probe;
161 int activeDevice = probe.FindActive(pin);
162 if( activeDevice == -1 ) {
163 cerr << "Device not found, or not specified" << endl;
164 return 1;
167 // Create our controller object
168 Barry::Controller con(probe.Get(activeDevice));
170 // Create our builder object
171 RecordBuilder<Contact, Store<Contact> > build(contactStore);
173 // make sure we're in desktop mode
174 con.OpenMode(Controller::Desktop);
176 // upload all records to device
177 con.SaveDatabaseByType<Barry::Contact>(contactStore);
180 catch( Barry::BError &se ) {
181 std::cerr << "BError caught: " << se.what() << endl;
183 catch( Usb::UsbError &ue) {
184 std::cerr << "UsbError caught: " << ue.what() << endl;
186 catch( std::runtime_error &re ) {
187 std::cerr << "std::runtime_error caught: " << re.what() << endl;
188 return 1;
190 catch( std::exception &e ) {
191 std::cerr << "std::exception caught: " << e.what() << endl;
192 return 1;
195 return 0;