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"
44 //////////////////////////////////////////////////////////////////////////////
45 // HexDumpParser class
47 HexDumpParser::HexDumpParser(std::ostream
&os
)
52 void HexDumpParser::ParseRecord(const Barry::DBData
&data
,
55 if( m_last_dbname
!= data
.GetDBName() ) {
56 m_os
<< "Records for database: " << data
.GetDBName() << endl
;
57 m_last_dbname
= data
.GetDBName();
60 m_os
<< "Raw record dump for record: 0x"
61 << hex
<< data
.GetUniqueId()
62 << ", type: 0x" << hex
<< (unsigned int) data
.GetRecType()
63 << ", offset: 0x" << hex
<< data
.GetOffset()
65 m_os
<< data
.GetData() << endl
;
69 //////////////////////////////////////////////////////////////////////////////
70 // MultiRecordParser class
72 // takes ownership of default_parser!
73 MultiRecordParser::MultiRecordParser(Parser
*default_parser
)
74 : m_default(default_parser
)
78 MultiRecordParser::~MultiRecordParser()
80 map_type::iterator i
= m_parsers
.begin();
81 for( ; i
!= m_parsers
.end(); ++i
) {
85 // and the default parser
89 void MultiRecordParser::Add(const std::string
&dbname
, Parser
*parser
)
91 std::auto_ptr
<Parser
> p(parser
);
93 map_type::iterator i
= m_parsers
.find(dbname
);
94 if( i
!= m_parsers
.end() ) {
95 // found existing parser, so delete it first
99 i
->second
= p
.release();
102 m_parsers
[dbname
] = p
.get();
107 // takes ownership of parser!
108 void MultiRecordParser::Add(RecordParserBase
*parser
)
110 std::auto_ptr
<Parser
> p(parser
);
111 std::string name
= parser
->GetDBName();
112 Add(name
, p
.release());
115 bool MultiRecordParser::Add(const std::string
&dbname
,
118 std::auto_ptr
<Parser
> p
;
121 #define HANDLE_PARSER(tname) if( dbname == tname::GetDBName() ) { p.reset( new RecordParser<tname, DumpStore<tname> > (new DumpStore<tname>(os)) ); }
123 // check for recognized database names
124 ALL_KNOWN_PARSER_TYPES
131 Add(dbname
, p
.release());
135 bool MultiRecordParser::Add(const std::string
&dbname
, AllRecordStore
&store
)
138 #define HANDLE_PARSER(tname) \
139 if( dbname == tname::GetDBName() ) { \
140 Add(dbname, new RecordParser<tname, AllRecordStore>(store)); \
144 // check for recognized database names
145 ALL_KNOWN_PARSER_TYPES
147 // if we get here, record was not found
152 void MultiRecordParser::ParseRecord(const DBData
&data
, const IConverter
*ic
)
154 // search for a named parser
155 map_type::iterator i
= m_parsers
.find(data
.GetDBName());
156 if( i
!= m_parsers
.end() ) {
158 i
->second
->ParseRecord(data
, ic
);
160 else if( m_default
) {
161 // use default parser
162 m_default
->ParseRecord(data
, ic
);
167 //////////////////////////////////////////////////////////////////////////////
168 // AllRecordDumpStore class
170 // Use the macro here to implement the overrides, so that
171 // the compiler will catch if we are missing any.
173 #define HANDLE_PARSER(tname) \
174 void AllRecordDumpStore::operator() (const Barry::tname &r) \
176 m_os << r << std::endl; \
179 ALL_KNOWN_PARSER_TYPES
182 //////////////////////////////////////////////////////////////////////////////
183 // AllRecordDumpParser class
185 AllRecordParser::AllRecordParser(std::ostream
&os
,
186 Parser
*default_parser
,
187 AllRecordStore
*store
)
188 : MultiRecordParser(default_parser
)
192 #define HANDLE_PARSER(tname) if( m_store ) { Add( new RecordParser<tname, AllRecordStore>(*m_store)); } else { Add(tname::GetDBName(), os); }
194 ALL_KNOWN_PARSER_TYPES
;
197 AllRecordParser::~AllRecordParser()
203 //////////////////////////////////////////////////////////////////////////////
206 TeeParser::TeeParser()
210 TeeParser::~TeeParser()
212 // free all the owned parser pointers
213 for( parser_list_type::iterator i
= m_owned_parsers
.begin();
214 i
!= m_owned_parsers
.end();
221 // takes ownership of the pointer!
222 void TeeParser::Add(Parser
*p
)
224 std::auto_ptr
<Parser
> ap(p
);
225 m_owned_parsers
.push_back(ap
.get());
229 // does NOT take ownership
230 void TeeParser::Add(Parser
&p
)
232 m_external_parsers
.push_back(&p
);
235 void TeeParser::ParseRecord(const DBData
&data
, const IConverter
*ic
)
237 // call all owned parsers
238 for( parser_list_type::iterator i
= m_owned_parsers
.begin();
239 i
!= m_owned_parsers
.end();
242 (*i
)->ParseRecord(data
, ic
);
245 // call all external parsers
246 for( parser_list_type::iterator i
= m_external_parsers
.begin();
247 i
!= m_external_parsers
.end();
250 (*i
)->ParseRecord(data
, ic
);