lib: added operator=() to DBData class
[barry.git] / src / parser.cc
blobc6743d4aca339de99134b9eff40349247bbc153e
1 ///
2 /// \file parser.cc
3 /// Virtual parser wrapper
4 ///
6 /*
7 Copyright (C) 2005-2010, 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 "parser.h"
23 #include "r_calendar.h"
24 #include "r_calllog.h"
25 #include "r_bookmark.h"
26 #include "r_contact.h"
27 #include "r_memo.h"
28 #include "r_message.h"
29 #include "r_servicebook.h"
30 #include "r_task.h"
31 #include "r_pin_message.h"
32 #include "r_saved_message.h"
33 #include "r_sms.h"
34 #include "r_folder.h"
35 #include "r_timezone.h"
37 #include <iostream>
38 #include <memory>
40 #define ALL_KNOWN_RECORD_TYPES \
41 HANDLE_TYPE(Contact) \
42 HANDLE_TYPE(Message) \
43 HANDLE_TYPE(Calendar) \
44 HANDLE_TYPE(CalendarAll) \
45 HANDLE_TYPE(CallLog) \
46 HANDLE_TYPE(Bookmark) \
47 HANDLE_TYPE(ServiceBook) \
48 HANDLE_TYPE(Memo) \
49 HANDLE_TYPE(Task) \
50 HANDLE_TYPE(PINMessage) \
51 HANDLE_TYPE(SavedMessage) \
52 HANDLE_TYPE(Sms) \
53 HANDLE_TYPE(Folder) \
54 HANDLE_TYPE(Timezone)
56 namespace Barry {
58 //////////////////////////////////////////////////////////////////////////////
59 // HexDumpParser class
61 HexDumpParser::HexDumpParser(std::ostream &os)
62 : m_os(os)
66 void HexDumpParser::ParseRecord(const Barry::DBData &data,
67 const IConverter *ic)
69 m_os << "Raw record dump for record: "
70 << std::hex << data.GetUniqueId() << std::endl;
71 m_os << data.GetData() << std::endl;
75 //////////////////////////////////////////////////////////////////////////////
76 // MultiRecordParser class
78 // takes ownership of default_parser!
79 MultiRecordParser::MultiRecordParser(Parser *default_parser)
80 : m_default(default_parser)
84 MultiRecordParser::~MultiRecordParser()
86 map_type::iterator i = m_parsers.begin();
87 for( ; i != m_parsers.end(); ++i ) {
88 delete i->second;
91 // and the default parser
92 delete m_default;
95 void MultiRecordParser::Add(const std::string &dbname, Parser *parser)
97 std::auto_ptr<Parser> p(parser);
99 map_type::iterator i = m_parsers.find(dbname);
100 if( i != m_parsers.end() ) {
101 // found existing parser, so delete it first
102 delete i->second;
104 // assign it
105 i->second = p.release();
107 else {
108 m_parsers[dbname] = p.get();
109 p.release();
113 // takes ownership of parser!
114 void MultiRecordParser::Add(RecordParserBase *parser)
116 std::auto_ptr<Parser> p(parser);
117 std::string name = parser->GetDBName();
118 Add(name, p.release());
121 bool MultiRecordParser::Add(const std::string &dbname,
122 std::ostream &os)
124 std::auto_ptr<Parser> p;
126 #undef HANDLE_TYPE
127 #define HANDLE_TYPE(tname) if( dbname == tname::GetDBName() ) { p.reset( new RecordParser<tname, DumpStore<tname> > (new DumpStore<tname>(os)) ); }
129 // check for recognized database names
130 ALL_KNOWN_RECORD_TYPES
132 if( !p.get() ) {
133 // name not known
134 return false;
137 Add(dbname, p.release());
138 return true;
141 // Parser overrides
142 void MultiRecordParser::ParseRecord(const DBData &data, const IConverter *ic)
144 // search for a named parser
145 map_type::iterator i = m_parsers.find(data.GetDBName());
146 if( i != m_parsers.end() ) {
147 // found one, use it
148 i->second->ParseRecord(data, ic);
150 else if( m_default ) {
151 // use default parser
152 m_default->ParseRecord(data, ic);
157 //////////////////////////////////////////////////////////////////////////////
158 // AllRecordDumpStore class
160 void AllRecordDumpStore::operator() (const Barry::Contact &r)
162 m_os << r << std::endl;
165 void AllRecordDumpStore::operator() (const Barry::Message &r)
167 m_os << r << std::endl;
170 void AllRecordDumpStore::operator() (const Barry::Calendar &r)
172 m_os << r << std::endl;
175 void AllRecordDumpStore::operator() (const Barry::CalendarAll &r)
177 m_os << r << std::endl;
180 void AllRecordDumpStore::operator() (const Barry::CallLog &r)
182 m_os << r << std::endl;
185 void AllRecordDumpStore::operator() (const Barry::Bookmark &r)
187 m_os << r << std::endl;
190 void AllRecordDumpStore::operator() (const Barry::ServiceBook &r)
192 m_os << r << std::endl;
195 void AllRecordDumpStore::operator() (const Barry::Memo &r)
197 m_os << r << std::endl;
200 void AllRecordDumpStore::operator() (const Barry::Task &r)
202 m_os << r << std::endl;
205 void AllRecordDumpStore::operator() (const Barry::PINMessage &r)
207 m_os << r << std::endl;
210 void AllRecordDumpStore::operator() (const Barry::SavedMessage &r)
212 m_os << r << std::endl;
215 void AllRecordDumpStore::operator() (const Barry::Sms &r)
217 m_os << r << std::endl;
220 void AllRecordDumpStore::operator() (const Barry::Folder &r)
222 m_os << r << std::endl;
225 void AllRecordDumpStore::operator() (const Barry::Timezone &r)
227 m_os << r << std::endl;
231 //////////////////////////////////////////////////////////////////////////////
232 // AllRecordDumpParser class
234 AllRecordParser::AllRecordParser(std::ostream &os,
235 Parser *default_parser,
236 AllRecordStore *store)
237 : MultiRecordParser(default_parser)
238 , m_store(store)
240 #undef HANDLE_TYPE
241 #define HANDLE_TYPE(tname) if( m_store ) { Add( new RecordParser<tname, AllRecordStore>(*m_store)); } else { Add(tname::GetDBName(), os); }
243 ALL_KNOWN_RECORD_TYPES;
246 AllRecordParser::~AllRecordParser()
248 delete m_store;
252 //////////////////////////////////////////////////////////////////////////////
253 // TeeParser class
255 TeeParser::TeeParser()
259 TeeParser::~TeeParser()
261 // free all the owned parser pointers
262 for( parser_list_type::iterator i = m_owned_parsers.begin();
263 i != m_owned_parsers.end();
264 ++i )
266 delete *i;
270 // takes ownership of the pointer!
271 void TeeParser::Add(Parser *p)
273 std::auto_ptr<Parser> ap(p);
274 m_owned_parsers.push_back(ap.get());
275 ap.release();
278 // does NOT take ownership
279 void TeeParser::Add(Parser &p)
281 m_external_parsers.push_back(&p);
284 void TeeParser::ParseRecord(const DBData &data, const IConverter *ic)
286 // call all owned parsers
287 for( parser_list_type::iterator i = m_owned_parsers.begin();
288 i != m_owned_parsers.end();
289 ++i )
291 (*i)->ParseRecord(data, ic);
294 // call all external parsers
295 for( parser_list_type::iterator i = m_external_parsers.begin();
296 i != m_external_parsers.end();
297 ++i )
299 (*i)->ParseRecord(data, ic);
303 } // namespace Barry