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"
42 //////////////////////////////////////////////////////////////////////////////
43 // HexDumpParser class
45 HexDumpParser::HexDumpParser(std::ostream
&os
)
50 void HexDumpParser::ParseRecord(const Barry::DBData
&data
,
53 m_os
<< "Raw record dump for record: "
54 << std::hex
<< data
.GetUniqueId() << std::endl
;
55 m_os
<< data
.GetData() << std::endl
;
59 //////////////////////////////////////////////////////////////////////////////
60 // MultiRecordParser class
62 // takes ownership of default_parser!
63 MultiRecordParser::MultiRecordParser(Parser
*default_parser
)
64 : m_default(default_parser
)
68 MultiRecordParser::~MultiRecordParser()
70 map_type::iterator i
= m_parsers
.begin();
71 for( ; i
!= m_parsers
.end(); ++i
) {
75 // and the default parser
79 void MultiRecordParser::Add(const std::string
&dbname
, Parser
*parser
)
81 std::auto_ptr
<Parser
> p(parser
);
83 map_type::iterator i
= m_parsers
.find(dbname
);
84 if( i
!= m_parsers
.end() ) {
85 // found existing parser, so delete it first
89 i
->second
= p
.release();
92 m_parsers
[dbname
] = p
.get();
97 // takes ownership of parser!
98 void MultiRecordParser::Add(RecordParserBase
*parser
)
100 std::auto_ptr
<Parser
> p(parser
);
101 std::string name
= parser
->GetDBName();
102 Add(name
, p
.release());
105 bool MultiRecordParser::Add(const std::string
&dbname
,
108 std::auto_ptr
<Parser
> p
;
111 #define HANDLE_PARSER(tname) if( dbname == tname::GetDBName() ) { p.reset( new RecordParser<tname, DumpStore<tname> > (new DumpStore<tname>(os)) ); }
113 // check for recognized database names
114 ALL_KNOWN_PARSER_TYPES
121 Add(dbname
, p
.release());
125 bool MultiRecordParser::Add(const std::string
&dbname
, AllRecordStore
&store
)
128 #define HANDLE_PARSER(tname) \
129 if( dbname == tname::GetDBName() ) { \
130 Add(dbname, new RecordParser<tname, AllRecordStore>(store)); \
134 // check for recognized database names
135 ALL_KNOWN_PARSER_TYPES
137 // if we get here, record was not found
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 // Use the macro here to implement the overrides, so that
161 // the compiler will catch if we are missing any.
163 #define HANDLE_PARSER(tname) \
164 void AllRecordDumpStore::operator() (const Barry::tname &r) \
166 m_os << r << std::endl; \
169 ALL_KNOWN_PARSER_TYPES
172 //////////////////////////////////////////////////////////////////////////////
173 // AllRecordDumpParser class
175 AllRecordParser::AllRecordParser(std::ostream
&os
,
176 Parser
*default_parser
,
177 AllRecordStore
*store
)
178 : MultiRecordParser(default_parser
)
182 #define HANDLE_PARSER(tname) if( m_store ) { Add( new RecordParser<tname, AllRecordStore>(*m_store)); } else { Add(tname::GetDBName(), os); }
184 ALL_KNOWN_PARSER_TYPES
;
187 AllRecordParser::~AllRecordParser()
193 //////////////////////////////////////////////////////////////////////////////
196 TeeParser::TeeParser()
200 TeeParser::~TeeParser()
202 // free all the owned parser pointers
203 for( parser_list_type::iterator i
= m_owned_parsers
.begin();
204 i
!= m_owned_parsers
.end();
211 // takes ownership of the pointer!
212 void TeeParser::Add(Parser
*p
)
214 std::auto_ptr
<Parser
> ap(p
);
215 m_owned_parsers
.push_back(ap
.get());
219 // does NOT take ownership
220 void TeeParser::Add(Parser
&p
)
222 m_external_parsers
.push_back(&p
);
225 void TeeParser::ParseRecord(const DBData
&data
, const IConverter
*ic
)
227 // call all owned parsers
228 for( parser_list_type::iterator i
= m_owned_parsers
.begin();
229 i
!= m_owned_parsers
.end();
232 (*i
)->ParseRecord(data
, ic
);
235 // call all external parsers
236 for( parser_list_type::iterator i
= m_external_parsers
.begin();
237 i
!= m_external_parsers
.end();
240 (*i
)->ParseRecord(data
, ic
);