lib: fixed parsing of recurring VEVENTS: DAILY and interval support
[barry/progweb.git] / src / parser.h
blob47562e5c3101f4ad2578420514dab25d5f7edcd2
1 ///
2 /// \file parser.h
3 /// Virtual parser wrapper
4 ///
6 /*
7 Copyright (C) 2005-2012, 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 "dll.h"
26 #include "data.h"
27 #include "protocol.h"
28 #include <stdint.h> // for uint32_t
29 #include <iosfwd>
30 #include <map>
32 // forward declarations
33 namespace Barry {
34 class IConverter;
35 class Contact;
36 class Message;
37 class Calendar;
38 class CalendarAll;
39 class CallLog;
40 class Bookmark;
41 class ServiceBook;
42 class Memo;
43 class Task;
44 class PINMessage;
45 class SavedMessage;
46 class Sms;
47 class Folder;
48 class TimeZone;
49 class ContentStore;
50 class HandheldAgent;
54 // This macro can be used to automatically generate code for all known
55 // record types. Just #undef HANDLE_PARSER, then #define it to whatever
56 // you need, then use ALL_KNOWN_PARSER_TYPES. See parser.cc for
57 // various examples.
59 // These are sorted so their GetDBName()'s will display in alphabetical order.
61 #define ALL_KNOWN_PARSER_TYPES \
62 HANDLE_PARSER(Contact) \
63 HANDLE_PARSER(Bookmark) \
64 HANDLE_PARSER(Calendar) \
65 HANDLE_PARSER(CalendarAll) \
66 HANDLE_PARSER(ContentStore) \
67 HANDLE_PARSER(Folder) \
68 HANDLE_PARSER(HandheldAgent) \
69 HANDLE_PARSER(Memo) \
70 HANDLE_PARSER(Message) \
71 HANDLE_PARSER(CallLog) \
72 HANDLE_PARSER(PINMessage) \
73 HANDLE_PARSER(SavedMessage) \
74 HANDLE_PARSER(ServiceBook) \
75 HANDLE_PARSER(Sms) \
76 HANDLE_PARSER(Task) \
77 HANDLE_PARSER(TimeZone)
79 namespace Barry {
82 // Parser class
84 /// Base class for the parser hierarchy.
85 ///
86 /// This class provides the interface that the Controller class uses
87 /// to pass raw data it reads from the device. The Controller, along
88 /// with the Packet class, calls each of the virtual functions below
89 /// in the same order.
90 ///
91 /// This class is kept as a pure abstract class, in order to make sure
92 /// that the compiler will catch any API changes, for code derived
93 /// from it.
94 ///
95 class BXEXPORT Parser
97 public:
98 Parser() {}
99 virtual ~Parser() {}
101 /// Called to parse sub fields in the raw data packet.
102 virtual void ParseRecord(const DBData &data, const IConverter *ic) = 0;
107 // NullParser class
109 /// If in debug mode, this class can be used as a null parser.
110 /// Call Init() and the protocol will be dumped to stdout and
111 /// no parsing will be done.
113 /// Do NOT derive your own personal parser classes from this,
114 /// unless you are perfectly confident that you will catch
115 /// future API changes on the devel tree without the compiler's
116 /// help.
118 class BXEXPORT NullParser : public Parser
120 public:
121 NullParser() {}
122 virtual ~NullParser() {}
124 virtual void ParseRecord(const DBData &data, const IConverter *ic) {}
128 // HexDumpParser
130 /// Dumps raw hex of the given DBData to the given stream.
132 /// Do NOT derive your own personal parser classes from this,
133 /// unless you are perfectly confident that you will catch
134 /// future API changes on the devel tree without the compiler's
135 /// help.
137 class BXEXPORT HexDumpParser : public Parser
139 std::ostream &m_os;
140 std::string m_last_dbname;
142 public:
143 explicit HexDumpParser(std::ostream &os);
145 virtual void ParseRecord(const Barry::DBData &data,
146 const IConverter *ic);
150 // RecordParserBase
152 /// Abstract base class for the following RecordParser template, that exposes
153 /// some information on the specifics that the record parser can handle.
154 /// Specifically, it exposes the database name it is able to parse
156 class BXEXPORT RecordParserBase : public Parser
158 public:
159 // These functions are always valid, regardless of the
160 // state of the parser.
161 virtual const char * GetDBName() const = 0;
162 virtual uint8_t GetDefaultRecType() const = 0;
164 // These functions depend on the parser having just parsed
165 // a record successfully.
166 virtual bool IsRecordValid() const = 0;
167 virtual uint8_t GetRecType() const = 0;
168 virtual uint32_t GetUniqueId() const = 0;
169 virtual void Dump(std::ostream &os) const = 0;
174 // Note: Store classes take parsed Record objects as a functor.
175 // Parser classes deal with raw data, while Store classes deal with
176 // parsed Record objects.
180 // NullStore
182 /// A Storage class for RecordParser<> that does nothing, for the cases
183 /// where you only want to dump parsed record data to a stream.
185 template <class RecordT>
186 class NullStore
188 public:
189 void operator() (const RecordT &r)
195 // DumpStore
197 /// A Storage class for RecordParser<> that dumps the parsed record data
198 /// to the given stream.
200 template <class RecordT>
201 class DumpStore
203 std::ostream &m_os;
205 public:
206 explicit DumpStore(std::ostream &os)
207 : m_os(os)
211 void operator() (const RecordT &r)
213 r.Dump(m_os);
218 // RecordStore
220 /// A Storage class for RecordParser that stores a copy of the parsed record.
222 template <class RecordT>
223 class RecordStore
225 public:
226 RecordT m_rec;
228 void operator() (const RecordT &r)
230 m_rec = r;
235 // ParseDBData
237 /// Contains the proper way to convert a DBData object into a record.
239 template <class RecordT>
240 void ParseDBData(const DBData &data, RecordT &rec, const IConverter *ic)
242 // start fresh
243 rec = RecordT();
245 // parse
246 rec.SetIds(data.GetRecType(), data.GetUniqueId());
247 size_t offset = data.GetOffset();
248 rec.ParseHeader(data.GetData(), offset);
249 rec.ParseFields(data.GetData(), offset, ic);
253 // RecordParser template class
255 /// Template class for easy creation of specific parser objects. This template
256 /// takes the following template arguments:
258 /// - RecordT: One of the record parser classes in record.h
259 /// - StorageT: A custom storage functor class. An object of this type
260 /// will be called as a function with parsed Record as an
261 /// argument. This happens on the fly as the data is retrieved
262 /// from the device over USB, so it should not block forever.
264 /// Example LoadDatabase() call:
266 /// <pre>
267 /// struct StoreContact
268 /// {
269 /// std::vector<Contact> &amp;array;
270 /// StoreContact(std::vector<Contact> &amp;a) : array(a) {}
271 /// void operator() (const Contact &amp;c)
272 /// {
273 /// array.push_back(c);
274 /// }
275 /// };
277 /// Controller con(probeResult);
278 /// con.OpenMode(Controller::Desktop);
279 /// std::vector<Contact> contactList;
280 /// StoreContact storage(contactList);
281 /// RecordParser<Contact, StoreContact> parser(storage);
282 /// con.LoadDatabase(con.GetDBID("Address Book"), parser);
283 /// </pre>
285 template <class RecordT, class StorageT>
286 class RecordParser : public RecordParserBase
288 StorageT *m_store;
289 bool m_owned;
290 RecordT m_rec;
291 bool m_record_valid;
293 public:
294 /// Constructor that references an externally managed storage object.
295 RecordParser(StorageT &storage)
296 : m_store(&storage)
297 , m_owned(false)
298 , m_record_valid(false)
302 /// Constructor that references a locally managed storage object.
303 /// The pointer passed in will be stored, and freed when this class
304 /// is destroyed. It is safe to call this constructor with
305 /// a 'new'ly created storage object.
306 RecordParser(StorageT *storage = 0)
307 : m_store(storage)
308 , m_owned(true)
309 , m_record_valid(false)
313 ~RecordParser()
315 if( this->m_owned )
316 delete m_store;
319 virtual StorageT* GetStore()
321 return m_store;
324 virtual const StorageT* GetStore() const
326 return m_store;
329 virtual void ParseRecord(const DBData &data, const IConverter *ic)
331 m_record_valid = false;
332 ParseDBData(data, m_rec, ic);
333 m_record_valid = true;
335 if( m_store )
336 (*m_store)(m_rec);
340 // RecordParserBase overrides
343 // These functions are always valid, regardless of the
344 // state of the parser.
345 virtual const char * GetDBName() const
347 return RecordT::GetDBName();
350 virtual uint8_t GetDefaultRecType() const
352 return RecordT::GetDefaultRecType();
355 // These functions depend on the parser having just parsed
356 // a record successfully.
357 virtual bool IsRecordValid() const
359 return m_record_valid;
362 virtual const RecordT& GetRecord() const
364 return m_rec;
367 virtual uint8_t GetRecType() const
369 return m_rec.GetRecType();
372 virtual uint32_t GetUniqueId() const
374 return m_rec.GetUniqueId();
377 virtual void Dump(std::ostream &os) const
379 m_rec.Dump(os);
384 // AllRecordStore
386 /// Base class with overloaded functor behaviour for all available
387 /// record classes. To be used with AllRecordParser.
389 class BXEXPORT AllRecordStore
391 public:
392 AllRecordStore() {}
393 virtual ~AllRecordStore() {}
395 #undef HANDLE_PARSER
396 #define HANDLE_PARSER(tname) \
397 virtual void operator() (const Barry::tname &) = 0;
399 ALL_KNOWN_PARSER_TYPES
403 // MultiRecordParser
405 /// Container parser class that accepts multiple Parser objects
406 /// (often RecordParser<> objects but they don't have to be) and
407 /// automatically routes incoming records to the appropriate parser.
408 /// Note that this container owns *all* Parser objects, and will
409 /// free them upon destruction.
411 /// Incoming records that have no matching parser are passed to the
412 /// default parser object, if one exists, otherwise they are dropped
413 /// silently. The default parser object is also owned by the container,
414 /// and will be freed on destruction.
416 /// Do NOT derive your own personal parser classes from this,
417 /// unless you are perfectly confident that you will catch
418 /// future API changes on the devel tree without the compiler's
419 /// help.
421 class BXEXPORT MultiRecordParser : public Parser
423 typedef std::map<std::string, Parser*> map_type;
425 Parser *m_delete_default; // if set, will be freed
426 Parser *m_default; // used by all code for actual work
427 // and may or may not be "owned" by us
428 map_type m_parsers;
430 public:
431 // takes ownership of default_parser!
432 explicit MultiRecordParser(Parser *default_parser = 0);
434 // does not take ownership of the default_parser
435 explicit MultiRecordParser(Parser &default_parser);
437 ~MultiRecordParser();
439 /// Adds given parser to list and takes ownership of it
440 void Add(const std::string &dbname, Parser *parser);
442 /// Adds given parser to list and takes ownership of it
443 void Add(RecordParserBase *parser);
445 /// Creates a RecordParser<> object using the given record
446 /// type and AllRecordStore. Does NOT take ownership of the
447 /// store object, since it can be used multiple times for
448 /// multiple records.
449 template <class RecordT>
450 void Add(AllRecordStore &store)
452 Add( RecordT::GetDBName(),
453 new RecordParser<RecordT, AllRecordStore>(store) );
456 /// Two helper template functions that create the RecordParser<>
457 /// automatically based on the function call. Both pointer and
458 /// reference versions.
459 template <class RecordT, class StorageT>
460 void Add(StorageT *store)
462 Add( RecordT::GetDBName(),
463 new RecordParser<RecordT, StorageT>(store) );
466 template <class RecordT, class StorageT>
467 void Add(StorageT &store)
469 Add( RecordT::GetDBName(),
470 new RecordParser<RecordT, StorageT>(store) );
473 /// Creates a RecordParser<> object for the given database name,
474 /// using DumpStore<> with the given stream for the output,
475 /// and adds it to list.
476 /// Returns false if there is no known Record class for dbname.
477 bool Add(const std::string &dbname, std::ostream &os);
479 /// Creates a RecordParser<> object for the given database name,
480 /// using the given store object.
481 /// Returns false if there is no known Record class for dbname.
482 bool Add(const std::string &dbname, AllRecordStore &store);
484 // Parser overrides
485 virtual void ParseRecord(const DBData &data, const IConverter *ic);
489 // AllRecordDumpStore
491 /// Derived from AllRecordStore, which just calls each record's
492 /// Dump() member with the given stream.
494 class BXEXPORT AllRecordDumpStore : public AllRecordStore
496 protected:
497 std::ostream &m_os;
499 public:
500 explicit AllRecordDumpStore(std::ostream &os)
501 : m_os(os)
505 #undef HANDLE_PARSER
506 #define HANDLE_PARSER(tname) \
507 virtual void operator() (const Barry::tname &);
509 ALL_KNOWN_PARSER_TYPES
513 // AllRecordParser
515 /// Convenience parser that creates a MultiRecordParser with all known
516 /// record parsers added. If an AllRecordStore pointer is passed in,
517 /// this class takes ownership of it, and uses it as the store object
518 /// for all the RecordParser<> objects it creates. If not, then
519 /// a custom DumpStore<> object is created with the given stream
520 /// for each RecordParser<> added.
522 /// The default parser object behaves just like MultiRecordParser
524 /// This class takes ownership of all pointers passed in.
526 class BXEXPORT AllRecordParser : public MultiRecordParser
528 AllRecordStore *m_store;
530 protected:
531 // does not take ownership of store, by itself,
532 // but the constructor that calls it might
533 void AddRecords(std::ostream *os, AllRecordStore *store);
535 public:
536 // takes ownership of default_parser and store!
537 explicit AllRecordParser(std::ostream &os,
538 Parser *default_parser = 0,
539 AllRecordStore *store = 0);
541 // does not take ownership of default_parser or store
542 AllRecordParser(Parser &default_parser, AllRecordStore &store);
544 ~AllRecordParser();
548 // TeeParser
550 /// Sends incoming DBData objects to all the parsers in its list.
551 /// This parser container does NOT own the parsers added.
553 class BXEXPORT TeeParser : public Parser
555 typedef std::vector<Parser*> parser_list_type;
557 parser_list_type m_external_parsers, m_owned_parsers;
559 public:
560 TeeParser();
561 ~TeeParser();
563 /// Adds parser to internal list, and takes ownership of the
564 /// pointer.
565 void Add(Parser *p);
567 /// Adds parser to internal list. Does NOT own the parser reference.
568 void Add(Parser &p);
570 void ParseRecord(const DBData &data, const IConverter *ic);
573 } // namespace Barry
575 #endif