3 /// Virtual parser wrapper
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.
23 #include "r_calendar.h"
24 #include "r_calllog.h"
25 #include "r_bookmark.h"
26 #include "r_contact.h"
28 #include "r_message.h"
29 #include "r_servicebook.h"
31 #include "r_pin_message.h"
32 #include "r_saved_message.h"
35 #include "r_timezone.h"
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) \
50 HANDLE_TYPE(PINMessage) \
51 HANDLE_TYPE(SavedMessage) \
58 //////////////////////////////////////////////////////////////////////////////
59 // HexDumpParser class
61 HexDumpParser::HexDumpParser(std::ostream
&os
)
66 void HexDumpParser::ParseRecord(const Barry::DBData
&data
,
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
) {
91 // and the default parser
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
105 i
->second
= p
.release();
108 m_parsers
[dbname
] = p
.get();
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
,
124 std::auto_ptr
<Parser
> p
;
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
137 Add(dbname
, p
.release());
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() ) {
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
)
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()
252 //////////////////////////////////////////////////////////////////////////////
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();
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());
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();
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();
299 (*i
)->ParseRecord(data
, ic
);