- updated time conversion calls to match opensync's latest SVN
[barry.git] / src / parser.h
blob00b4d0def821ff4275acda9655c3c077f780191c
1 ///
2 /// \file parser.h
3 /// Virtual parser wrapper
4 ///
6 /*
7 Copyright (C) 2005-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 #ifndef __BARRY_PARSER_H__
23 #define __BARRY_PARSER_H__
25 #include "data.h"
26 #include "protocol.h"
27 #include "debug.h"
29 namespace Barry {
31 // also acts as a null parser
33 // Parser class
35 /// Base class for the parser functor hierarchy. If in debug mode, this
36 /// class can be used as a null parser. Call Init() and the protocol
37 /// will be dumped to stdout and no parsing will be done.
38 ///
39 class Parser
41 public:
42 Parser() {}
43 virtual ~Parser() {}
45 virtual bool operator()(const Data &data, size_t offset) { return true; }
47 virtual bool GetOperation(const Data &data, unsigned int &operation);
52 // RecordParser template class
54 /// Template class for easy creation of specific parser objects. This template
55 /// takes the following template arguments:
56 ///
57 /// - Record: One of the record parser classes in record.h
58 /// - Storage: A custom storage functor class. An object of this type
59 /// will be called as a function with parsed Record as an
60 /// argument. This happens on the fly as the data is retrieved
61 /// from the device over USB, so it should not block forever.
62 ///
63 /// Example LoadDatabase() call:
64 ///
65 /// <pre>
66 /// struct StoreContact
67 /// {
68 /// std::vector<Contact> &amp;array;
69 /// StoreContact(std::vector<Contact> &amp;a) : array(a) {}
70 /// void operator() (const Contact &amp;c)
71 /// {
72 /// array.push_back(c);
73 /// }
74 /// };
75 ///
76 /// Controller con(probeResult);
77 /// con.OpenMode(Controller::Desktop);
78 /// std::vector<Contact> contactList;
79 /// StoreContact storage(contactList);
80 /// RecordParser<Contact, StoreContact> parser(storage);
81 /// con.LoadDatabase(con.GetDBID("Address Book"), parser);
82 /// </pre>
83 ///
84 template <class Record, class Storage>
85 class RecordParser : public Parser
87 Storage *m_store;
88 bool m_owned;
90 public:
91 /// Constructor that references an externally managed storage object.
92 RecordParser(Storage &storage)
93 : m_store(&storage), m_owned(false) {}
95 /// Constructor that references a locally managed storage object.
96 /// The pointer passed in will be stored, and freed when this class
97 /// is destroyed. It is safe to call this constructor with
98 /// a 'new'ly created storage object.
99 RecordParser(Storage *storage)
100 : m_store(storage), m_owned(true) {}
102 ~RecordParser()
104 if( this->m_owned )
105 delete m_store;
108 virtual bool CheckHeaderSize(const Data &data, size_t offset, unsigned int operation)
110 size_t recordsize;
111 switch( operation )
113 case SB_DBOP_GET_RECORDS:
114 // using the new protocol
115 recordsize = Record::GetProtocolRecordSize();
116 break;
118 case SB_DBOP_OLD_GET_RECORDS_REPLY:
119 // using the old protocol
120 recordsize = Record::GetOldProtocolRecordSize();
121 break;
123 default:
124 // unknown protocol
125 dout("Unknown protocol");
126 return false;
129 // return true if header is ok
130 return data.GetSize() > (offset + recordsize);
133 /// Functor member called by Controller::LoadDatabase() during
134 /// processing.
135 virtual bool operator()(const Data &data, size_t offset)
137 unsigned int operation;
138 if( !GetOperation(data, operation) )
139 return false;
140 if( !CheckHeaderSize(data, offset, operation) )
141 return false;
143 Record rec;
144 rec.Parse(data, offset, operation);
145 (*m_store)(rec);
146 return true;
150 } // namespace Barry
152 #endif