menu: added new Keywords tag to .desktop files
[barry.git] / examples / pipedump.cc
blobdcb34243a192e03b67359e7708789f90f4749a31
1 ///
2 /// \file dumpall.cc
3 /// Example code using the Barry library to dump all
4 /// device databases to stdout, using all known parsers,
5 /// and hex dumps for the rest.
6 ///
8 /*
9 Copyright (C) 2011-2013, Net Direct Inc. (http://www.netdirect.ca/)
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 See the GNU General Public License in the COPYING file at the
21 root directory of this project for more details.
24 #include <barry/barry.h>
25 #include <iostream>
26 #include <iomanip>
28 using namespace std;
29 using namespace Barry;
31 template <class RecordT>
32 class Store
34 public:
35 void operator() (const RecordT &rec)
37 cout << RecordT::GetDBName() << ": "
38 << hex << rec.GetUniqueId() << endl;
42 class UnknownParser : public Parser
44 public:
45 void ParseRecord(const DBData &data, const IConverter *ic)
47 cout << "Unknown record of "
48 << dec << data.GetData().GetSize()
49 << " bytes from: "
50 << data.GetDBName()
51 << endl;
55 int main(int argc, char *argv[])
57 try {
59 Barry::Init();
60 Probe probe;
61 int i = probe.FindActive();
62 if( i == -1 ) {
63 cout << "No device available" << endl;
64 return 1;
67 // open Desktop mode
68 Controller con(probe.Get(i));
69 Mode::Desktop desktop(con);
70 desktop.Open(); // specify password here if needed
72 // create builder object to extract records from the
73 // device, and add all database names to the builder
74 DeviceBuilder builder(desktop);
75 const DatabaseDatabase &dbdb = desktop.GetDBDB();
76 DatabaseDatabase::DatabaseArrayType::const_iterator
77 b = dbdb.Databases.begin(), e = dbdb.Databases.end();
78 for( ; b != e; ++b ) {
79 builder.Add( b->Name );
82 // create the parser, and use stdout dump objects for output
83 AllRecordParser parser(cout,
84 new HexDumpParser(cout),
85 new AllRecordDumpStore(cout));
87 // create the pipe to connect builder to parser and
88 // move the data
89 Pipe pipe(builder);
90 pipe.PumpFile(parser);
92 cout << "\n\n\nStarting again....................." << endl;
94 // run it again, but this time with custom set of
95 // record parsers and default parser
97 MultiRecordParser mrp( new UnknownParser );
99 // add a few known record types... first, the manual way
100 mrp.Add( Contact::GetDBName(),
101 new RecordParser<Contact, Store<Contact> >(
102 new Store<Contact>));
103 // and with the template member (does the same thing)
104 mrp.Add<Calendar>( new Store<Calendar> );
105 mrp.Add<Sms>( new Store<Sms> );
106 mrp.Add<TimeZone>( new Store<TimeZone> );
108 builder.Restart();
109 pipe.PumpFile(mrp);
111 // and one more time, with a tee
112 cout << "\n\n\nStarting again with a tee............" << endl;
114 TeeParser tee;
115 tee.Add( mrp );
116 tee.Add( parser );
118 builder.Restart();
119 pipe.PumpFile(tee);
122 catch( exception &e ) {
123 cerr << e.what() << endl;
124 return 1;
127 return 0;