lib: Fixing checking for incorrect return value in while loop.
[barry.git] / src / s11n-boost.h
blobf68906e9e0ef7b4e2314a21bad0c068ecf73164c
1 ///
2 /// \file s11n-boost.h
3 /// Non-intrusive versions of serialization functions for the
4 /// record classes. These template functions make it possible
5 /// to use the record classes with the Boost::Serialization
6 /// library.
7 ///
9 /*
10 Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU General Public License in the COPYING file at the
22 root directory of this project for more details.
25 #ifndef __BARRY_S11N_BOOST_H__
26 #define __BARRY_S11N_BOOST_H__
28 #include "dll.h"
29 #include "record.h"
30 #include <boost/serialization/vector.hpp>
32 ///////////////////////////////////////////////////////////////////////////////
33 // special versions
35 // BARRY_BASE_S11N_VERSION - the base version where all record data is
36 // stored so it can be fully retrieved and
37 // uploaded to the handheld device later.
38 // BARRY_POD_MAP_VERSION - if these templates are called with a version
39 // equal or greater than this, only mappable,
40 // POD data is included in the serialization
42 #define BARRY_BASE_S11N_VERSION 0
43 #define BARRY_POD_MAP_VERSION 1000
45 // namespace boost::serialization, for the non-intrusive version
46 namespace boost {
47 namespace serialization {
49 template <class ArchiveT>
50 void serialize(ArchiveT &ar, Barry::TimeT &t, const unsigned int ver)
52 ar & make_nvp("Barry::TimeT::Time", t.Time);
55 template <class ArchiveT>
56 void serialize(ArchiveT &ar, Barry::UnknownField &uf, const unsigned int ver)
58 ar & make_nvp("type", uf.type);
59 ar & make_nvp("data", uf.data.raw_data);
62 template <class ArchiveT>
63 void serialize(ArchiveT &ar, Barry::Contact::GroupLink &g, const unsigned int ver)
65 ar & make_nvp("Link", g.Link);
66 ar & make_nvp("Unknown", g.Unknown);
69 template <class ArchiveT>
70 void serialize(ArchiveT &ar, Barry::CategoryList &cl, const unsigned int ver)
72 std::vector<std::string> &sl = cl;
73 ar & make_nvp("CategoryList", sl);
76 template <class ArchiveT>
77 void serialize(ArchiveT &ar, Barry::EmailList &el, const unsigned int ver)
79 std::vector<std::string> &sl = el;
80 ar & make_nvp("EmailList", sl);
83 template <class ArchiveT>
84 void serialize(ArchiveT &ar, Barry::Contact &c, const unsigned int ver)
86 ar & make_nvp("RecType", c.RecType);
87 ar & make_nvp("RecordId", c.RecordId);
89 ar & make_nvp("EmailAddresses", c.EmailAddresses);
90 ar & make_nvp("Phone", c.Phone);
91 ar & make_nvp("Fax", c.Fax);
92 ar & make_nvp("HomeFax", c.HomeFax);
93 ar & make_nvp("WorkPhone", c.WorkPhone);
94 ar & make_nvp("HomePhone", c.HomePhone);
95 ar & make_nvp("MobilePhone", c.MobilePhone);
96 ar & make_nvp("MobilePhone2", c.MobilePhone2);
97 ar & make_nvp("Pager", c.Pager);
98 ar & make_nvp("PIN", c.PIN);
99 ar & make_nvp("Radio", c.Radio);
100 ar & make_nvp("WorkPhone2", c.WorkPhone2);
101 ar & make_nvp("HomePhone2", c.HomePhone2);
102 ar & make_nvp("OtherPhone", c.OtherPhone);
103 ar & make_nvp("FirstName", c.FirstName);
104 ar & make_nvp("LastName", c.LastName);
105 ar & make_nvp("Company", c.Company);
106 ar & make_nvp("DefaultCommunicationsMethod", c.DefaultCommunicationsMethod);
107 ar & make_nvp("Address1", c.WorkAddress.Address1);
108 ar & make_nvp("Address2", c.WorkAddress.Address2);
109 ar & make_nvp("Address3", c.WorkAddress.Address3);
110 ar & make_nvp("City", c.WorkAddress.City);
111 ar & make_nvp("Province", c.WorkAddress.Province);
112 ar & make_nvp("PostalCode", c.WorkAddress.PostalCode);
113 ar & make_nvp("Country", c.WorkAddress.Country);
114 ar & make_nvp("JobTitle", c.JobTitle);
115 ar & make_nvp("PublicKey", c.PublicKey);
116 ar & make_nvp("URL", c.URL);
117 ar & make_nvp("Prefix", c.Prefix);
118 ar & make_nvp("Categories", c.Categories);
119 ar & make_nvp("HomeAddress1", c.HomeAddress.Address1);
120 ar & make_nvp("HomeAddress2", c.HomeAddress.Address2);
121 ar & make_nvp("HomeAddress3", c.HomeAddress.Address3);
122 ar & make_nvp("Notes", c.Notes);
123 ar & make_nvp("UserDefined1", c.UserDefined1);
124 ar & make_nvp("UserDefined2", c.UserDefined2);
125 ar & make_nvp("UserDefined3", c.UserDefined3);
126 ar & make_nvp("UserDefined4", c.UserDefined4);
127 ar & make_nvp("HomeCity", c.HomeAddress.City);
128 ar & make_nvp("HomeProvince", c.HomeAddress.Province);
129 ar & make_nvp("HomePostalCode", c.HomeAddress.PostalCode);
130 ar & make_nvp("HomeCountry", c.HomeAddress.Country);
131 ar & make_nvp("Image", c.Image);
132 ar & make_nvp("Nickname", c.Nickname);
134 if( ver < BARRY_POD_MAP_VERSION ) {
135 ar & make_nvp("GroupLinks", c.GroupLinks);
136 ar & make_nvp("Unknowns", c.Unknowns);
140 template <class ArchiveT>
141 void serialize(ArchiveT &ar, Barry::EmailAddress &a, const unsigned int ver)
143 ar & make_nvp("Name", a.Name);
144 ar & make_nvp("Email", a.Email);
147 template <class ArchiveT>
148 void serialize(ArchiveT &ar, Barry::EmailAddressList &eal, const unsigned int ver)
150 std::vector<Barry::EmailAddress> &v = eal;
151 ar & make_nvp("EmailAddressList", v);
154 template <class ArchiveT>
155 void serialize(ArchiveT &ar, Barry::Message &m, const unsigned int ver)
157 ar & make_nvp("From", m.From);
158 ar & make_nvp("To", m.To);
159 ar & make_nvp("Cc", m.Cc);
160 ar & make_nvp("Sender", m.Sender);
161 ar & make_nvp("ReplyTo", m.ReplyTo);
162 ar & make_nvp("Subject", m.Subject);
163 ar & make_nvp("Body", m.Body);
164 ar & make_nvp("Attachment", m.Attachment);
165 ar & make_nvp("MessageRecordId", m.MessageRecordId);
166 ar & make_nvp("MessageReplyTo", m.MessageReplyTo);
167 ar & make_nvp("MessageDateSent", m.MessageDateSent.Time);
168 ar & make_nvp("MessageDateReceived", m.MessageDateReceived.Time);
170 ar & make_nvp("MessageTruncated", m.MessageTruncated);
171 ar & make_nvp("MessageRead", m.MessageRead);
172 ar & make_nvp("MessageReply", m.MessageReply);
173 ar & make_nvp("MessageSaved", m.MessageSaved);
174 ar & make_nvp("MessageSavedDeleted", m.MessageSavedDeleted);
176 ar & make_nvp("MessagePriority", m.Priority);
177 ar & make_nvp("MessageSensitivity", m.Sensitivity);
179 if( ver < BARRY_POD_MAP_VERSION ) {
180 ar & make_nvp("Unknowns", m.Unknowns);
184 template <class ArchiveT>
185 void serialize(ArchiveT &ar, Barry::Calendar &c, const unsigned int ver)
187 ar & make_nvp("RecType", c.RecType);
188 ar & make_nvp("RecordId", c.RecordId);
190 ar & make_nvp("AllDayEvent", c.AllDayEvent);
192 ar & make_nvp("Subject", c.Subject);
193 ar & make_nvp("Notes", c.Notes);
194 ar & make_nvp("Location", c.Location);
196 ar & make_nvp("NotificationTime", c.NotificationTime.Time);
197 ar & make_nvp("StartTime", c.StartTime.Time);
198 ar & make_nvp("EndTime", c.EndTime.Time);
200 ar & make_nvp("Organizer", c.Organizer);
201 ar & make_nvp("AcceptedBy", c.AcceptedBy);
202 ar & make_nvp("Invited", c.Invited);
204 ar & make_nvp("FreeBusyFlag", c.FreeBusyFlag);
205 ar & make_nvp("ClassFlag", c.ClassFlag);
207 ar & make_nvp("Recurring", c.Recurring);
208 ar & make_nvp("RecurringType", c.RecurringType);
209 ar & make_nvp("Interval", c.Interval);
210 ar & make_nvp("RecurringEndTime", c.RecurringEndTime.Time);
211 ar & make_nvp("Perpetual", c.Perpetual);
212 ar & make_nvp("CalendarID", c.CalendarID);
213 ar & make_nvp("TimeZoneCode", c.TimeZoneCode);
214 ar & make_nvp("TimeZoneValid", c.TimeZoneValid);
216 ar & make_nvp("DayOfWeek", c.DayOfWeek);
217 ar & make_nvp("WeekOfMonth", c.WeekOfMonth);
218 ar & make_nvp("DayOfMonth", c.DayOfMonth);
219 ar & make_nvp("MonthOfYear", c.MonthOfYear);
220 ar & make_nvp("WeekDays", c.WeekDays);
222 if( ver < BARRY_POD_MAP_VERSION ) {
223 ar & make_nvp("Unknowns", c.Unknowns);
227 template <class ArchiveT>
228 void serialize(ArchiveT &ar, Barry::CalendarAll &c, const unsigned int ver)
230 serialize(ar, static_cast<Barry::Calendar&>(c), ver);
232 ar & make_nvp("MailAccount", c.MailAccount);
235 template <class ArchiveT>
236 void serialize(ArchiveT &ar, Barry::CallLog &c, const unsigned int ver)
238 ar & make_nvp("RecType", c.RecType);
239 ar & make_nvp("RecordId", c.RecordId);
241 ar & make_nvp("Duration", c.Duration);
242 ar & make_nvp("Timestamp", c.Timestamp);
243 ar & make_nvp("ContactName", c.ContactName);
244 ar & make_nvp("PhoneNumber", c.PhoneNumber);
246 ar & make_nvp("DirectionFlag", c.DirectionFlag);
247 ar & make_nvp("StatusFlag", c.StatusFlag);
248 ar & make_nvp("PhoneTypeFlag", c.PhoneTypeFlag);
249 ar & make_nvp("PhoneInfoFlag", c.PhoneInfoFlag);
251 if( ver < BARRY_POD_MAP_VERSION ) {
252 ar & make_nvp("Unknowns", c.Unknowns);
256 template <class ArchiveT>
257 void serialize(ArchiveT &ar, Barry::Bookmark &c, const unsigned int ver)
259 ar & make_nvp("RecType", c.RecType);
260 ar & make_nvp("RecordId", c.RecordId);
261 ar & make_nvp("Index", c.Index);
263 ar & make_nvp("Name", c.Name);
264 ar & make_nvp("Icon", c.Icon);
265 ar & make_nvp("Url", c.Url);
267 ar & make_nvp("BrowserIdentity", c.BrowserIdentity);
268 ar & make_nvp("DisplayMode", c.DisplayMode);
269 ar & make_nvp("JavaScriptMode", c.JavaScriptMode);
271 if( ver < BARRY_POD_MAP_VERSION ) {
272 ar & make_nvp("Unknowns", c.Unknowns);
276 template <class ArchiveT>
277 void serialize(ArchiveT &ar, Barry::ServiceBookConfig &c, const unsigned int ver)
279 ar & make_nvp("Format", c.Format);
281 if( ver < BARRY_POD_MAP_VERSION ) {
282 ar & make_nvp("Unknowns", c.Unknowns);
286 template <class ArchiveT>
287 void serialize(ArchiveT &ar, Barry::ServiceBook &c, const unsigned int ver)
289 ar & make_nvp("RecType", c.RecType);
290 ar & make_nvp("RecordId", c.RecordId);
292 ar & make_nvp("Name", c.Name);
293 ar & make_nvp("HiddenName", c.HiddenName);
294 ar & make_nvp("Description", c.Description);
295 ar & make_nvp("DSID", c.DSID);
296 ar & make_nvp("BesDomain", c.BesDomain);
297 ar & make_nvp("UniqueId", c.UniqueId);
298 ar & make_nvp("ContentId", c.ContentId);
299 ar & make_nvp("Config", c.Config);
301 if( ver < BARRY_POD_MAP_VERSION ) {
302 ar & make_nvp("Unknowns", c.Unknowns);
306 template <class ArchiveT>
307 void serialize(ArchiveT &ar, Barry::Memo &m, const unsigned int ver)
309 ar & make_nvp("RecType", m.RecType);
310 ar & make_nvp("RecordId", m.RecordId);
312 ar & make_nvp("Title", m.Title);
313 ar & make_nvp("Body", m.Body);
314 ar & make_nvp("Categories", m.Categories);
316 if( ver < BARRY_POD_MAP_VERSION ) {
317 ar & make_nvp( "Unknowns", m.Unknowns);
321 template <class ArchiveT>
322 void serialize(ArchiveT &ar, Barry::Task &t, const unsigned int ver)
324 ar & make_nvp("RecType", t.RecType);
325 ar & make_nvp("RecordId", t.RecordId);
327 ar & make_nvp("Summary", t.Summary);
328 ar & make_nvp("Notes", t.Notes);
329 ar & make_nvp("Categories", t.Categories);
330 ar & make_nvp("UID", t.UID);
332 ar & make_nvp("StartTime", t.StartTime.Time);
333 ar & make_nvp("DueTime", t.DueTime.Time);
334 ar & make_nvp("AlarmTime", t.AlarmTime.Time);
336 ar & make_nvp("TimeZoneCode", t.TimeZoneCode);
337 ar & make_nvp("TimeZoneValid", t.TimeZoneValid);
339 ar & make_nvp("AlarmType", t.AlarmType);
340 ar & make_nvp("Interval", t.Interval);
341 ar & make_nvp("RecurringType", t.RecurringType);
342 ar & make_nvp("RecurringEndTime", t.RecurringEndTime.Time);
343 ar & make_nvp("DayOfWeek", t.DayOfWeek);
344 ar & make_nvp("WeekOfMonth", t.WeekOfMonth);
345 ar & make_nvp("DayOfMonth", t.DayOfMonth);
346 ar & make_nvp("MonthOfYear", t.MonthOfYear);
347 ar & make_nvp("WeekDays", t.WeekDays);
349 ar & make_nvp("PriorityFlag", t.PriorityFlag);
350 ar & make_nvp("StatusFlag", t.StatusFlag);
351 ar & make_nvp("Recurring", t.Recurring);
352 ar & make_nvp("Perpetual", t.Perpetual);
354 if( ver < BARRY_POD_MAP_VERSION ) {
355 ar & make_nvp( "Unknowns", t.Unknowns);
359 template<class ArchiveT>
360 void serialize(ArchiveT &ar, Barry::PINMessage &p, const unsigned int ver)
362 ar & make_nvp("RecType", p.RecType);
363 ar & make_nvp("RecordId", p.RecordId);
365 ar & make_nvp("From", p.From);
366 ar & make_nvp("To", p.To);
367 ar & make_nvp("Cc", p.Cc);
368 ar & make_nvp("Bcc", p.Bcc);
369 ar & make_nvp("Subject", p.Subject);
370 ar & make_nvp("Body", p.Body);
371 ar & make_nvp("MessageRecordId", p.MessageRecordId);
372 ar & make_nvp("MessageReplyTo", p.MessageReplyTo);
373 ar & make_nvp("MessageDateSent", p.MessageDateSent.Time);
374 ar & make_nvp("MessageDateReceived", p.MessageDateReceived.Time);
376 ar & make_nvp("MessageTruncated", p.MessageTruncated);
377 ar & make_nvp("MessageRead", p.MessageRead);
378 ar & make_nvp("MessageReply", p.MessageReply);
379 ar & make_nvp("MessageSaved", p.MessageSaved);
380 ar & make_nvp("MessageSavedDeleted", p.MessageSavedDeleted);
382 ar & make_nvp("MessagePriority", p.Priority);
383 ar & make_nvp("MessageSensitivity", p.Sensitivity);
385 if(ver < BARRY_POD_MAP_VERSION) {
386 ar & make_nvp("Unknowns", p.Unknowns);
390 template <class ArchiveT>
391 void serialize(ArchiveT &ar, Barry::SavedMessage &m, const unsigned int ver)
393 ar & make_nvp("RecType", m.RecType);
394 ar & make_nvp("RecordId", m.RecordId);
396 ar & make_nvp("From", m.From);
397 ar & make_nvp("To", m.To);
398 ar & make_nvp("Cc", m.Cc);
399 ar & make_nvp("Bcc", m.Bcc);
400 ar & make_nvp("Sender", m.Sender);
401 ar & make_nvp("ReplyTo", m.ReplyTo);
402 ar & make_nvp("Subject", m.Subject);
403 ar & make_nvp("Body", m.Body);
404 ar & make_nvp("Attachment", m.Attachment);
405 ar & make_nvp("MessageRecordId", m.MessageRecordId);
406 ar & make_nvp("MessageReplyTo", m.MessageReplyTo);
407 ar & make_nvp("MessageDateSent", m.MessageDateSent.Time);
408 ar & make_nvp("MessageDateReceived", m.MessageDateReceived.Time);
410 ar & make_nvp("MessageTruncated", m.MessageTruncated);
411 ar & make_nvp("MessageRead", m.MessageRead);
412 ar & make_nvp("MessageReply", m.MessageReply);
413 ar & make_nvp("MessageSaved", m.MessageSaved);
414 ar & make_nvp("MessageSavedDeleted", m.MessageSavedDeleted);
416 ar & make_nvp("MessagePriority", m.Priority);
417 ar & make_nvp("MessageSensitivity", m.Sensitivity);
419 if( ver < BARRY_POD_MAP_VERSION ) {
420 ar & make_nvp("Unknowns", m.Unknowns);
424 template <class ArchiveT>
425 void serialize(ArchiveT &ar, Barry::Sms &m, const unsigned int ver)
427 ar & make_nvp("RecType", m.RecType);
428 ar & make_nvp("RecordId", m.RecordId);
430 ar & make_nvp("MessageStatus", m.MessageStatus);
431 ar & make_nvp("DeliveryStatus", m.DeliveryStatus);
433 ar & make_nvp("IsNew", m.IsNew);
434 ar & make_nvp("NewConversation", m.NewConversation);
435 ar & make_nvp("Saved", m.Saved);
436 ar & make_nvp("Deleted", m.Deleted);
437 ar & make_nvp("Opened", m.Opened);
439 ar & make_nvp("Timestamp", m.Timestamp);
440 ar & make_nvp("ServiceCenterTimestamp", m.ServiceCenterTimestamp);
442 ar & make_nvp("DataCodingScheme", m.DataCodingScheme);
443 ar & make_nvp("ErrorId", m.ErrorId);
445 ar & make_nvp("Addresses", m.Addresses);
446 ar & make_nvp("Body", m.Body);
448 if( ver < BARRY_POD_MAP_VERSION ) {
449 ar & make_nvp("Unknowns", m.Unknowns);
453 template <class ArchiveT>
454 void serialize(ArchiveT &ar, Barry::Folder &f, const unsigned int ver)
456 ar & make_nvp("RecType", f.RecType);
457 ar & make_nvp("RecordId", f.RecordId);
459 ar & make_nvp("FolderName", f.Name);
460 ar & make_nvp("FolderNumber", f.Number);
461 ar & make_nvp("FolderLevel", f.Level);
462 ar & make_nvp("FolderType", f.Type);
464 if( ver < BARRY_POD_MAP_VERSION ) {
465 ar & make_nvp( "Unknowns", f.Unknowns);
469 template <class ArchiveT>
470 void serialize(ArchiveT &ar, Barry::TimeZone &t, const unsigned int ver)
472 ar & make_nvp("RecType", t.RecType);
473 ar & make_nvp("RecordId", t.RecordId);
475 ar & make_nvp("Name", t.Name);
476 ar & make_nvp("Index", t.Index);
477 ar & make_nvp("UTCOffset", t.UTCOffset);
479 ar & make_nvp("UseDST", t.UseDST);
480 ar & make_nvp("DSTOffset", t.DSTOffset);
481 ar & make_nvp("StartMonth", t.StartMonth);
482 ar & make_nvp("EndMonth", t.EndMonth);
484 ar & make_nvp("TZType", t.TZType);
486 if( ver < BARRY_POD_MAP_VERSION ) {
487 ar & make_nvp("Unknowns", t.Unknowns);
491 template <class ArchiveT>
492 void serialize(ArchiveT &ar, Barry::ContentStore &c, const unsigned int ver)
494 ar & make_nvp("RecType", c.RecType);
495 ar & make_nvp("RecordId", c.RecordId);
497 ar & make_nvp("Filename", c.Filename);
498 ar & make_nvp("FolderFlag", c.FolderFlag);
499 ar & make_nvp("FileContent", c.FileContent);
500 ar & make_nvp("FileDescriptor", c.FileDescriptor);
502 // FileSize is not used for building, so no need to save it here
504 if( ver < BARRY_POD_MAP_VERSION ) {
505 ar & make_nvp("Unknowns", c.Unknowns);
509 template <class ArchiveT>
510 void serialize(ArchiveT &ar, Barry::HandheldAgent &h, const unsigned int ver)
512 ar & make_nvp("RecType", h.RecType);
513 ar & make_nvp("RecordId", h.RecordId);
515 ar & make_nvp("MEID", h.MEID);
516 ar & make_nvp("Model", h.Model);
517 ar & make_nvp("Bands", h.Bands);
518 ar & make_nvp("Pin", h.Pin);
519 ar & make_nvp("Version", h.Version);
520 ar & make_nvp("PlatformVersion", h.PlatformVersion);
521 ar & make_nvp("Manufacturer", h.Manufacturer);
522 ar & make_nvp("Network", h.Network);
524 if( ver < BARRY_POD_MAP_VERSION ) {
525 ar & make_nvp("Unknowns", h.Unknowns);
529 }} // namespace boost::serialization
532 //////////////////////////////////////////////////////////////////////////////
533 // Helper wrapper templates for loading and saving records to an iostream
535 namespace Barry {
537 // Can be used as a Storage class for RecordBuilder<>
538 template <class RecordT>
539 class BoostLoader
541 public:
542 typedef RecordT rec_type;
543 typedef std::vector<rec_type> list_type;
545 private:
546 list_type m_records;
547 typename list_type::iterator rec_it;
549 public:
550 explicit BoostLoader(std::istream &is)
552 boost::archive::text_iarchive ia(is);
553 ia >> m_records;
554 rec_it = m_records.begin();
557 list_type& GetRecords() { return m_records; }
558 const list_type& GetRecords() const { return m_records; }
560 // retrieval operator
561 bool operator()(RecordT &rec, Builder &builder)
563 if( rec_it == m_records.end() )
564 return false;
565 rec = *rec_it;
566 ++rec_it;
567 return true;
571 // Can be used as a Storage class for RecordParser<>
572 template <class RecordT>
573 class BoostSaver
575 public:
576 typedef RecordT rec_type;
577 typedef std::vector<rec_type> list_type;
579 private:
580 std::ostream &m_os;
581 list_type m_records;
582 typename list_type::iterator rec_it;
584 public:
585 explicit BoostSaver(std::ostream &os)
586 : m_os(os)
590 ~BoostSaver()
592 WriteArchive();
595 void WriteArchive() const
597 // write dbname first, so parsing is possible
598 m_os << RecordT::GetDBName() << std::endl;
600 // write boost archive of all records
601 boost::archive::text_oarchive oa(m_os);
603 // boost is fussy that the vector must be const
604 // we do this explicitly, for documentation's sake
605 const list_type &recs = m_records;
606 oa << recs;
607 m_os << std::endl;
610 list_type& GetRecords() { return m_records; }
611 const list_type& GetRecords() const { return m_records; }
613 // storage operator
614 void operator()(const RecordT &rec)
616 m_records.push_back(rec);
621 // BoostParser
623 /// This Parser turns incoming records (which can be of any record type
624 /// included in ALL_KNOWN_PARSER_TYPES) into a Boost Serialization stream
625 /// on the given iostream.
627 /// This class is defined completely in the header, so that it is
628 /// optional for applications to link against the boost libraries.
630 class BXEXPORT BoostParser : public Barry::Parser
632 std::auto_ptr<Barry::Parser> m_parser;
633 std::ofstream *m_ofs;
634 std::ostream &m_os; // references either an external object,
635 // or *m_ifs... this is the reference to
636 // use in the entire class... the constructor
637 // sets it up
639 std::string m_current_db;
641 public:
642 explicit BoostParser(const std::string &filename)
643 : m_ofs( new std::ofstream(filename.c_str()) )
644 , m_os(*m_ofs)
648 explicit BoostParser(std::ostream &os)
649 : m_ofs(0)
650 , m_os(os)
654 ~BoostParser()
656 // flush any remaining parser output
657 // (note this still potentially uses m_ofs, so do this first)
658 m_parser.reset();
660 // cleanup the stream
661 delete m_ofs;
664 void StartDB(const std::string &dbname)
666 // done with current parser, flush it's output
667 m_parser.reset();
669 #undef HANDLE_PARSER
670 #define HANDLE_PARSER(tname) \
671 if( dbname == tname::GetDBName() ) { \
672 m_parser.reset( \
673 new RecordParser<tname, BoostSaver<tname> >( \
674 new BoostSaver<tname>(m_os) ) ); \
675 return; \
678 ALL_KNOWN_PARSER_TYPES
680 // if we make it here, we don't have a record parser
681 // for this dbname, so just dump it to stderr (not stdout,
682 // since the user might be sending normal output there)
683 m_parser.reset( new HexDumpParser(std::cerr) );
686 void ParseRecord(const DBData &data, const IConverter *ic)
688 if( m_current_db != data.GetDBName() ) {
689 StartDB(data.GetDBName());
690 m_current_db = data.GetDBName();
693 m_parser->ParseRecord(data, ic);
698 // BoostBuilder
700 /// This Builder class reads a boost serialization stream, and converts
701 /// them into DBData records. Can only produce records for record types
702 /// in ALL_KNOWN_BUILDER_TYPES.
704 class BXEXPORT BoostBuilder : public Barry::Builder
706 std::auto_ptr<Builder> m_builder;
707 std::ifstream *m_ifs;
709 std::istream &m_is; // references either an external object,
710 // or *m_ifs... this is the reference to
711 // use in the entire class... the constructor
712 // sets it up
714 public:
715 explicit BoostBuilder(const std::string &filename)
716 : m_ifs( new std::ifstream(filename.c_str()) )
717 , m_is(*m_ifs)
719 FinishDB();
722 explicit BoostBuilder(std::istream &is)
723 : m_ifs(0)
724 , m_is(is)
726 FinishDB();
729 ~BoostBuilder()
731 delete m_ifs;
734 void FinishDB()
736 // done with current builder
737 m_builder.reset();
739 // read the next DBName
740 std::string dbName;
741 while( getline(m_is, dbName) ) {
743 #undef HANDLE_BUILDER
744 #define HANDLE_BUILDER(tname) \
745 if( dbName == tname::GetDBName() ) { \
746 m_builder.reset( \
747 new RecordBuilder<tname, BoostLoader<tname> >( \
748 new BoostLoader<tname>(m_is) ) ); \
749 return; \
752 ALL_KNOWN_BUILDER_TYPES
756 bool BuildRecord(DBData &data, size_t &offset, const IConverter *ic)
758 if( !m_builder.get() )
759 return false;
761 bool ret = m_builder->BuildRecord(data, offset, ic);
762 if( !ret )
763 FinishDB();
764 return ret;
767 bool FetchRecord(DBData &data, const IConverter *ic)
769 if( !m_builder.get() )
770 return false;
772 bool ret = m_builder->FetchRecord(data, ic);
773 if( !ret )
774 FinishDB();
775 return ret;
778 bool EndOfFile() const
780 return m_builder.get() ? false : true;
785 } // namespace Barry
787 #endif